简体   繁体   English

在 telnetlib 超时后让 python 3 代码继续

[英]Let python 3 code continue after telnetlib timeout

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.我正在尝试加载一个带有 IP 地址的文件,telnet 到它们,发送一些命令并保存输出。我让它工作了,输出看起来像预期的那样。

My problem is, if there is an IP address in the file that in unreachable and telnetlib times out.我的问题是,如果文件中有一个 IP 地址无法访问且 telnetlib 超时。 Then the complete script stops.然后完整的脚本停止。 I would like to ignore the IP address that timed out and continue with the rest of the file.我想忽略超时的 IP 地址并继续处理文件的其余部分。

#!/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

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...

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM