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!!!
So here's the solution: