Bottom Page

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.
Quote
#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?
Quote
#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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Server and Network (socket) [WinError 10053] Timxxx 1 152 Yesterday, 10:03 AM
Last Post: iMuny
  Soft Access Point & Socket Data Streaming groger57 1 142 Aug-01-2019, 02:53 PM
Last Post: groger57
  Send raw string via socket with out any changes john36 18 503 Aug-01-2019, 07:54 AM
Last Post: john36
  BrokenPipeError: [Errno 32] Broken pipe Pflaumboy 3 4,045 Jul-29-2019, 07:48 PM
Last Post: Skaperen
  Question concerning function of a a socket pkm 4 1,143 Jun-04-2019, 07:48 AM
Last Post: DeaD_EyE
  socket.gaierror: [Errno -2] Name or service not known seokangwoo99 5 512 May-25-2019, 02:36 PM
Last Post: DeaD_EyE
  socket programming ConnectionRefusedError error srm 3 370 May-16-2019, 08:07 PM
Last Post: LavaCreeperKing
  Socket reading script Will86 4 413 Apr-17-2019, 06:57 AM
Last Post: Will86
  Server and Network (socket) [WinError 10053] SheeppOSU 2 593 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Async socket server and ports Pengwyn 1 446 Feb-28-2019, 12:13 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)