Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tn.write not working?
#1
Hi, I have this program that access devices and change its hostname based from csv file but after entering the user/password script doesn't write any or seem like its tn.write is not running.

While logs from device shows that the python script can access the device. Also no error when running this script.

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()
##### OUTPUT ######
Input the CSV filename: iphost.csv
LIST OF DEVICES
192.168.137.50 lab-sw01
Host: ['192.168.137.50']
Enter your username: cisco

Warning (from warnings module):
File "C:\Users\GlobalNOC\AppData\Local\Programs\Python\Python37-32\lib\getpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter Your Password Here: cisco
Now accessing the devices 192.168.137.50
host: 192.168.137.50
>>>


Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020