1

I'm trying til load a file with IP addresses, telnet to them, send some commands and save the output.I got it working and the output looks as expected.

My problem is, if there is an IP address in the file that in unreachable and telnetlib times out. Then the complete script stops. I would like to ignore the IP address that timed out and continue with the rest of the file.

#!/usr/bin/env python3

import pexpect
import getpass
import telnetlib
import socket

ipfile = input("Enter site (IP address file): ")
user = input("Enter username: ")
password = getpass.getpass("Enter password")
outputfile = ((ipfile)+".output")

f = open(outputfile, 'w')
f.write("")
f.close()

with open(ipfile) as ips:
   all_ips = [x.rstrip() for x in ips] # get all ips in a list and strip newline
for ip in all_ips:
   tn = telnetlib.Telnet(ip,23,2)
   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.write(b"term len 0\n")
     tn.write(b"sh inven\n")
     tn.write(b"logout\n")
#    print(tn.read_all().decode('ascii'))
     with open(outputfile,"ab") as f: #write to a file
       f.write(tn.read_all())                     

The error I get is

    Traceback (most recent call last):
  File "./test4.py", line 22, in <module>
    tn = telnetlib.Telnet(ip,  23,2)
  File "/usr/lib/python3.5/telnetlib.py", line 218, in __init__
    self.open(host, port, timeout)
  File "/usr/lib/python3.5/telnetlib.py", line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
    sock.connect(sa)
socket.timeout: timed out
SIMC
  • 13
  • 4

2 Answers2

1

If you specifically want to catch a socket timeout you can do the following...

import socket
import telnetlib

ip = '127.0.0.1'

try:
    tn = telnetlib.Telnet(ip, 23, 2)
except socket.timeout:
    print("connection time out caught.")
    # handle error cases here...
Kyle
  • 1,056
  • 5
  • 15
  • The only thing you need to do it 1. import the socket module. 2. put a "try" "except" block around your code that is triggering the socket timeout (looks like line #22 from looking at the stack trace). – Kyle Sep 28 '17 at 20:35
  • 1: import socket. Done. 2: I can put the "try" in line 22, and it makes sense. The except i can't figure. If I put it in line 24 below "tn = telnetlib.Telnet(ip,23,2)" it works fine if there are no timeouts. I a timeout happens it fals again – SIMC Sep 28 '17 at 20:50
0

I think I got it.

#!/usr/bin/env python3

import pexpect
import getpass
import telnetlib
import socket

ipfile = input("Enter site (IP address file): ")
user = input("Enter username: ")
password = getpass.getpass("Enter password: ")
outputfile = ((ipfile)+".output")

f = open(outputfile, 'w')
f.write("")
f.close()

with open(ipfile) as ips:
   all_ips = [x.rstrip() for x in ips] # get all ips in a list and strip newline
for ip in all_ips:
 try:
    tn = telnetlib.Telnet(ip,23,2)
    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.write(b"term len 0\n")
     tn.write(b"sh inven\n")
     tn.write(b"logout\n")
#    print(tn.read_all().decode('ascii'))
     with open(outputfile,"ab") as f: #write to a fil
       f.write(tn.read_all())
 except socket.timeout:
    print((ip)+" timeout")
SIMC
  • 13
  • 4