telnet写入的脚本无法正常运行

时间:2018-12-28 11:04:05

标签: python python-3.x network-programming

我有这个程序可以访问设备并根据csv文件更改其主机名,但是在输入用户/密码脚本后没有写入任何内容或tn.write似乎没有运行。

虽然来自设备的日志显示python脚本可以访问设备。运行此脚本时也没有错误。

import csv
import telnetlib
import getpass
import sys
import time

##from prettytable import PrettyTable
def main():


#open csv file, then put to variable csvfile.
    with open(input("Input the CSV filename: ")) as csvfile:
        riphostCSV = csv.reader(csvfile, delimiter=',')

#Put the data as list from excel.
        ipaddress = []
        nhostname = []
##        Menu = PrettyTable()
##        Menu.field_names=['IPadd','Nhostname']

#Action - Put the data from excel to variable[list]
        for col in riphostCSV:
            ipadr = col[0]
            nhost = col[1]

            ipaddress.append(ipadr)
            nhostname.append(nhost)

#List of devices from CSV file
        print("LIST OF DEVICES")
        for i in ipaddress: 
            print(i, nhostname[ipaddress.index(i)])

            dev = ipaddress
            print ("Host: ",dev)
            user = input("Enter your username: ")
            password=getpass.getpass("Enter Your Password Here: ")
    ##        password = getpass.getpass()

            print ("Now accessing the devices " + i)
            dev = i.strip()
            tn = telnetlib.Telnet(dev)
            print("host:",dev)
            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"#")
            tn.write(b"conf t \n")
            time.sleep(1)
            tn.write(b"hostname test33 \n")
            tn.write(b"exit \n")
            tn.write(b"wr mem \n")
##                tn.close()
##                break
main()

输出

输入CSV文件名:iphost.csv 设备清单 192.168.137.50 lab-sw01 主持人:['192.168.137.50'] 输入您的用户名:cisco

警告(来自警告模块): 文件“ C:\ Users \ GlobalNOC \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ getpass.py”,第100行 返回fallback_getpass(提示,流) GetPassWarning:无法控制终端上的回显。 警告:可能会回显密码输入。 在这里输入您的密码:cisco 现在访问设备192.168.137.50 主机:192.168.137.50

  
    
      

    
  

谢谢

0 个答案:

没有答案