簡體   English   中英

IP2Location + Netmiko到SSH到VM到MTR並打印結果(如果出現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與我擁有的其他VM,執行測試並打印結果。

但是,這不符合我的預期,它總是轉到忽略IF和ELIF的“其他”語句。

這是代碼:

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)

您可以從此處https://www.ip2location.com/developers/python下載IP2location DB的bin文件。

(我不顯示IP,因為這些IP是公開的,每個人都可以輸入),但這可以在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM