IP2Location + Netmiko to SSH to VM's to MTR and print results (If, elif, else statements issues) PYTHON3

267 views Asked by At

Im new on asking a question, always reading answers to improve my codes but never asked, so this is my first time here.

Im trying to make a simple scenario implementing some network automation.

Scenario: I Have 3 VM's (1st is in Argentina, 2nd is in Brazil, 3rd is in Miami) Need to connect to this machines through SSH to perform an MTR test over the recently filled IP address in the IP2Location module. The thing is that i need to perform this test through the most near point of the IP. Example: If i have a Chile IP Address i need to make this test over the VM in Argentina, if the IP is from Mexico i need to tests it through my VM in Miami, etc. For this to work i use IP2Location (I simply modified the base code to fill the IP manually) After the IP is placed, IP2Location prints the Country full name. Once i have the country full name i use the (if, elif, else) statements to connect to the different VM's i have with Netmiko, perform the tests and print the results.

But, this is not working as i expected, it always goes to the "else" statement overlooking the IF and ELIF.

Here's the Code:

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)

You can download the bin file for IP2location DB from here https://www.ip2location.com/developers/python

(i don't show the IP's because these are Public and everyone can enter) but this can be simply emulated on GNS3.

Thanks and Regards!!!

1

There are 1 answers

0
Nico Postumo On BEST ANSWER

So here's the solution:

#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)