IP2Location + Netmiko 到 SSH 到 VM 到 MTR 并打印结果(If、elif、else 语句问题)PYTHON3
IP2Location + Netmiko to SSH to VM's to MTR and print results (If, elif, else statements issues) PYTHON3
我刚开始问问题,总是阅读答案以改进我的代码,但从未问过,所以这是我第一次来这里。
我正在尝试创建一个简单的场景来实现一些网络自动化。
场景:
我有 3 个虚拟机(第一个在阿根廷,第二个在巴西,第三个在迈阿密)
需要通过 SSH 连接到这台机器,以对 IP2Location 模块中最近填充的 IP 地址执行 MTR 测试。
问题是我需要通过 IP 的最近点执行此测试。
示例:如果我有一个智利 IP 地址,我需要在阿根廷的 VM 上进行此测试,如果 IP 来自墨西哥,我需要通过我在迈阿密的 VM 等进行测试。
为此,我使用 IP2Location(我只是修改了基本代码以手动填充 IP)
放置 IP 后,IP2Location 打印国家全名。
一旦我有了国家全名,我就使用(if、elif、else)语句连接到我使用 Netmiko 的不同虚拟机,执行测试并打印结果。
但是,这并没有像我预期的那样工作,它总是转到忽略 IF 和 ELIF 的 "else" 语句。
代码如下:
import IP2Location
from netmiko import ConnectHandler
from datetime import datetime
import subprocess
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("/Volumes/DATA/nico/Desktop/ServDeg/IP2LOCATION-LITE-DB1.BIN"); #Path to the Database BIN
rec = IP2LocObj.get_all((str(input("IP: "))));
print(rec.country_long)
if rec.country_long is 'Brazil':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Argentina':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Chile':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Uruguay':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
else:
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
您可以从这里下载 IP2location DB 的 bin 文件 https://www.ip2location.com/developers/python
(我没有显示 IP,因为这些是 Public,每个人都可以进入)但这可以简单地在 GNS3 上模拟。
感谢和问候!!!
所以这是解决方案:
#Serv_deg_without_Demark_device by NMorra
from netmiko import ConnectHandler
from datetime import datetime
import IP2Location
start_time = datetime.now()
IP2LocObj = IP2Location.IP2Location() #Modulo IP-GEOLOCATION
IP2LocObj.open("/Volumes/DATA/nico/Desktop/ServDeg/IP2LOCATION-LITE-DB1.BIN");
#Bin Location
country = IP2LocObj.get_all(ipaddr) # ('mtr ' + str(var) + ' -c 10')
print("", ipaddr) #
country.country_long = str(country.country_long)[2:-1]
print(country.country_long)
print('Test in progress...')
if country.country_long == ('Argentina'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Chile'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Uruguay'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Brazil'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
else:
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
print('')
print('>'*10, 'MTR', '<'*10)
print(mtr)
end_time = datetime.now()
time = end_time-start_time
print('_'*5)
print('Time: ', time)
我刚开始问问题,总是阅读答案以改进我的代码,但从未问过,所以这是我第一次来这里。
我正在尝试创建一个简单的场景来实现一些网络自动化。
场景: 我有 3 个虚拟机(第一个在阿根廷,第二个在巴西,第三个在迈阿密) 需要通过 SSH 连接到这台机器,以对 IP2Location 模块中最近填充的 IP 地址执行 MTR 测试。 问题是我需要通过 IP 的最近点执行此测试。 示例:如果我有一个智利 IP 地址,我需要在阿根廷的 VM 上进行此测试,如果 IP 来自墨西哥,我需要通过我在迈阿密的 VM 等进行测试。 为此,我使用 IP2Location(我只是修改了基本代码以手动填充 IP) 放置 IP 后,IP2Location 打印国家全名。 一旦我有了国家全名,我就使用(if、elif、else)语句连接到我使用 Netmiko 的不同虚拟机,执行测试并打印结果。
但是,这并没有像我预期的那样工作,它总是转到忽略 IF 和 ELIF 的 "else" 语句。
代码如下:
import IP2Location
from netmiko import ConnectHandler
from datetime import datetime
import subprocess
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("/Volumes/DATA/nico/Desktop/ServDeg/IP2LOCATION-LITE-DB1.BIN"); #Path to the Database BIN
rec = IP2LocObj.get_all((str(input("IP: "))));
print(rec.country_long)
if rec.country_long is 'Brazil':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Argentina':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Chile':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
elif rec.country_long is 'Uruguay':
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
else:
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx', username='xxx', password='xxx')
output = net_connect.send_command('mtr ' + IP2LocObj + ' -c 10')
print(output)
您可以从这里下载 IP2location DB 的 bin 文件 https://www.ip2location.com/developers/python
(我没有显示 IP,因为这些是 Public,每个人都可以进入)但这可以简单地在 GNS3 上模拟。
感谢和问候!!!
所以这是解决方案:
#Serv_deg_without_Demark_device by NMorra
from netmiko import ConnectHandler
from datetime import datetime
import IP2Location
start_time = datetime.now()
IP2LocObj = IP2Location.IP2Location() #Modulo IP-GEOLOCATION
IP2LocObj.open("/Volumes/DATA/nico/Desktop/ServDeg/IP2LOCATION-LITE-DB1.BIN");
#Bin Location
country = IP2LocObj.get_all(ipaddr) # ('mtr ' + str(var) + ' -c 10')
print("", ipaddr) #
country.country_long = str(country.country_long)[2:-1]
print(country.country_long)
print('Test in progress...')
if country.country_long == ('Argentina'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Chile'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Uruguay'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
elif country.country_long == ('Brazil'):
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
else:
net_connect = ConnectHandler(device_type='linux', ip='xxx.xxx.xxx.xxx',
username='xxx', password='xxx')
mtr = net_connect.send_command('mtr ' + str(ipaddr) + ' -c 10 -r')
net_connect.disconnect()
print('')
print('>'*10, 'MTR', '<'*10)
print(mtr)
end_time = datetime.now()
time = end_time-start_time
print('_'*5)
print('Time: ', time)