Python Forum
socket.gaierror: [Errno 11001]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.gaierror: [Errno 11001]
#1
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()
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.
Reply
#2
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?
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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.
Reply
#5
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
Error:
Please Enter the Username: cisco Password: Configuring Switch with IP : 10.10.10.50 [error]Traceback (most recent call last): File "c:/Users/Nemat/Python/openswitches.py", line 13, in <module> tel=telnetlib.Telnet(HOST) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\telnetlib.py", line 218, in __init__ self.open(host, port, timeout) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\telnetlib.py", line 234, in open self.sock = socket.create_connection((host, port), timeout) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\socket.py", line 707, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\socket.py", line 748, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed
can some one help in this matter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.gaierror: [Errno -2] Name or service not known seokangwoo99 5 15,650 May-25-2019, 02:36 PM
Last Post: DeaD_EyE
  socket.gaierror: [Errno -2] Name or Service not known ItsAGuyaneseTing 1 11,614 Mar-02-2018, 07:14 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020