Hello everyone. In my path to learning Python I want to use the code mentioned to telnet into 3 different switches with different IP addresses and add vlan 5,6 and name those vlans.
The IP addresses are saved in a separate file called ips.txt which is saved in the same folder as the code itself and the IPs are written under each other with no empty line in between. This is in GNS3 and from my virtual Windows I can ping switches with the IPs mentioned in ips.txt. So connectivity is not an issue. I can execute other version of this code where I added the IP addresses inside the code without using any extra file (ips.txt)but if the IPs are not sequential it is easier to make a file and call it in your code. By the way I am running Python 2.7
The IP addresses are saved in a separate file called ips.txt which is saved in the same folder as the code itself and the IPs are written under each other with no empty line in between. This is in GNS3 and from my virtual Windows I can ping switches with the IPs mentioned in ips.txt. So connectivity is not an issue. I can execute other version of this code where I added the IP addresses inside the code without using any extra file (ips.txt)but if the IPs are not sequential it is easier to make a file and call it in your code. By the way I am running Python 2.7
import getpass import sys import telnetlib user = raw_input("Enter your telnet username: ") password = getpass.getpass() file = 'ips.txt' with open(file) as f: for line in f: print " configuring the host " + line HOST = line tn = telnetlib.Telnet(HOST) tn.read_until("Username: ") tn.write(user + "\n") if password: tn.read_until("Password: ") tn.write(password + "\n") tn.write("enable\n") tn.write("Cisco\n") tn.write("conf t\n") for x in range(5, 7): tn.write("vlan " + str(x) + "\n") tn.write("name python_vlan " + str(x) + "\n") tn.write("end\n") tn.write("wr\n") tn.write("exit\n") print tn.read_all()
Error:Traceback (most recent call last):
File "multiple_switches_ver2.py" , line 12, in <module>
tn = telnetlib.Telnet(HOST)
File "c:\Python27\lib\telnetlib.py", line 211, in __int__
self.open(host, port, timeout)
File "c:\Python27\lib\telnetlib.py", line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "c:\Python27\lib\telnetlib.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Output:Enter your telnet username: john
password:
configuring the host 192.168.122.11
Right after the output comes the error mentioned above. The output shows that the code through the ips.txt and 192.168.122.11 is the first IP in the file and it stops there without doing anything on any of the switches.