knight
knight

Reputation: 39

Python for loop: telnetlib into multiple hosts from a text file not working

I'm trying to use telnetlib to telnet into multiple devices from a text file to perform commands. When I connect to the host outside of the for loop, it works fine, but when I put it all in the for loop I get the following error:

Traceback (most recent call last):
  File "telnettest.py", line 16, in <module>
    tn = telnetlib.Telnet(HOST)
  File "/usr/local/lib/python3.6/telnetlib.py", line 218, in __init__
    self.open(host, port, timeout)
  File "/usr/local/lib/python3.6/telnetlib.py", line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "/usr/local/lib/python3.6/socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

My code is as follows:

import getpass
import telnetlib
import sys

user = input("Enter your Username: ")
password = getpass.getpass()
enablepassword = getpass.getpass()

hosts = open('hosts.txt', 'r')
for HOST in hosts:
    print(HOST)
    tn = telnetlib.Telnet(HOST)

    tn.read_until(b"Username: ")
    tn.write(user.encode('ascii') + b"\n")

    if password:
        tn.read_until(b"Password: ")
        tn.write(password.encode('ascii') + b"\n")

    tn.read_until(b"hostname>")
    tn.write(b"enable\n")
    tn.write(enablepassword.encode('ascii') + b"\n")
    tn.write(b"show version\n")
    tn.write(b"exit\n")
    tn.write(b"exit\n")

output = tn.read_all().decode('ascii')
print(output)

with open("telnetfile.txt", "a") as f:
    f.write(output)

Like I said, this all works fine outside of a for loop and when I've defined the HOST value without a file. However it seems to error trying to telnet, when I put it in a for loop, even though the print(HOST) prints the correct hostname. Even when there is only one host name in the file, it still errors.

Am I missing something blatantly obvious here?

Any help would be greatly appreciated.

Cheers.

Upvotes: 1

Views: 505

Answers (1)

Cow
Cow

Reputation: 3030

You need to strip each line when opening the file. This is because the return character is included (\n) for each line.

import getpass
import telnetlib
import sys

user = input("Enter your Username: ")
password = getpass.getpass()
enablepassword = getpass.getpass()

with open('hosts.txt') as hosts:
    for HOST in hosts:
        print(HOST)
        tn = telnetlib.Telnet(HOST.strip())

        tn.read_until(b"Username: ")
        tn.write(user.encode('ascii') + b"\n")

        if password:
            tn.read_until(b"Password: ")
            tn.write(password.encode('ascii') + b"\n")

        tn.read_until(b"hostname>")
        tn.write(b"enable\n")
        tn.write(enablepassword.encode('ascii') + b"\n")
        tn.write(b"show version\n")
        tn.write(b"exit\n")
        tn.write(b"exit\n")

output = tn.read_all().decode('ascii')
print(output)

with open("telnetfile.txt", "a") as f:
    f.write(output)

Upvotes: 1

Related Questions