telnet& amp;的基本python脚本从路由器读取

时间:2017-04-09 18:59:42

标签: python python-3.x sockets

import telnetlib
import os
import sys
import getpass

file=input("Enter Filename:")
user=input("enter username:")
password=getpass.getpass()

with open(file) as f:
    for line in f:
        IP=line
        print(IP)
        tn=telnetlib.Telnet(IP)
        tn.read_until(b"Username:")
        tn.write(user.encode('ascii')+b"\n")
        if True:
            tn.read_until(b"Password:")
        tn.write(b"en\n")
        tn.write(b"cisco\n")
        tn.write(b"show ip int brief\n")
        tn.write(b" exit\n")
        print(IP)

        new=tn.read_all().decode('ascii')
        y=new
        print(y)

        x=open(IP+"result.txt","w")
        x.write(y)
        sys.stdout.flush()

当我在TXT文件中添加1个以上的IP时,它会给出以下错误。

Enter Filename:input.txt
enter username:cisco
Warning: Password input may be echoed.
Password: cisco
10.0.0.1

Traceback (most recent call last):
  File "<tmp 1>", line 21, in <module>
    tn=telnetlib.Telnet(IP)
  File "c:\users\lalit\appdata\local\programs\python\python35-32\lib\telnetlib.py", line 218, in __init__
    self.open(host, port, timeout)
  File "c:\users\lalit\appdata\local\programs\python\python35-32\lib\telnetlib.py", line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "c:\users\lalit\appdata\local\programs\python\python35-32\lib\socket.py", line 693, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "c:\users\lalit\appdata\local\programs\python\python35-32\lib\socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

请帮助解决我对python的新问题并坚持这一过去一周。

0 个答案:

没有答案