socket.gaierror: [Errno 11001] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: socket.gaierror: [Errno 11001] (/thread-20354.html) |
socket.gaierror: [Errno 11001] - Johnygo - Aug-06-2019 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 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()
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.
RE: socket.gaierror: [Errno 11001] - Johnygo - Aug-06-2019 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.strip() if HOST: 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()I was able to make the code to work by adding strip() to the line 11 and adding "if HOST:" after line 11. I added if HOST: because after adding strip it starts working but every time it after configuring the last switch, was giving an error it couldn't find the next HOST (The host which doesn't exist) so by adding the if line it won't search for any more hosts. I still don't know why strip() is necessary because there is no space between lines of IPs in ips.txt. it is like : 192.168.122.11 192.168.122.12 192.168.122.13 Anyone any idea? RE: socket.gaierror: [Errno 11001] - DeaD_EyE - Aug-07-2019 Iterating over a file-like-object, gives you lines back + the newline char. The strip strips the newline away. In real the content is: 192.168.122.11\n 192.168.122.12\n 192.168.122.13\n If you take one line and trying to look up for this host, it will look up for: 192.168.122.11\n and not for 192.168.122.11 RE: socket.gaierror: [Errno 11001] - Johnygo - Aug-07-2019 Thank you DeaD_EyE for the explanation. So although the file is a text file, when python goes through the lines of it, it sees a new line at the end of each line. That's why the for loop can't find an IP address in the Python created empty, second line. RE: socket.gaierror: [Errno 11001] - nemat - Nov-03-2019 Hi i am still facing the same kind of issue. Below is my code import telnetlib import getpass p=open("ipadd.txt") user=input("Please Enter the Username: ") password=getpass.getpass() for line in p: print("Configuring Switch with IP : "+line) HOST=str(line.split()) tel=telnetlib.Telnet(HOST) tel.read_until(b"Username:") tel.write(user.encode("ascii")+b"\n") if password: tel.read_until(b"Password:") tel.write(password.encode("ascii")+b"\n") tel.write(b"conf t \n") for x in range(10,20): tel.write(b"vlan "+str(x).encode("ascii")+b"\n") tel.write(b"exit \n") tel.write(b"end \n") tel.write(b"wr \n") tel.write(b"exit \n") print(tel.read_all().decode("ascii")) p.close()Here is the output, with error can some one help in this matter
|