Python Forum

Full Version: telnetlib. configure hostnames
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone

I'm new in Python networking world. So I need you help. My goal is to configure different Hostnames for devices.

for example

Device_IP Hostname
192.168.1.1 TEST1
192.168.1.2 TEST2

I made excel document with IP address and Hostnames and wrote some script, but something is not work((
import telnetlib
import pandas as pd

df1 = pd.read_excel("nodes.xlsx",sheet_name='Sheet1')
df1list=df1.values.tolist()

for a in df1list:
    HOSTNAME=a[0]
    DEVICE_IP=a[1]

    try:
            HOST = DEVICE_IP
            user = "root"
            password = ""
            tn = telnetlib.Telnet(HOST,timeout=3)
            tn.read_until(b"Login: ")
            tn.write(user.encode('ascii') + b"\n")
            tn.read_until(b"Password: ")
            tn.write(password.encode('ascii') + b"\n")
            tn.write(b"/system identity user= + HOSTNAME \n")
            tn.write(b"exit \n")
            router_output = tn.read_all().decode('ascii')
            print(HOSTNAME+' '+DEVICE_IP,"Success")
            print(router_output)
            tn.close()
    except Exception as e:
        print(DEVICE_IP,"FAILED")
Can you help me to understand what is wrong?

Thank you


..
tn.read_until(b"Login: ") - check this (b"username") , may this is username