Saturday, March 30, 2019

Python with telnet

There are many resources related to telnet lib from the net. I'm just going to show you a few of them

1) Just enter the host ip adress and username and password

import telnetlib
HOST = "192.168.1.253"
user = "root"
password = "arrisc4"

def command(con, flag, str_=""):
    data = con.read_until(flag.encode())
    print(data.decode(errors='ignore'))
    con.write(str_.encode() + b"\n")
    return data
tn = telnetlib.Telnet(HOST)
command(tn, "Login:", user)
if password:
    command(tn, "Password:", password)
command(tn, "#", "en")
#command(tn, "$", " exit")
#command(tn, "$", "")
tn.close()

2)user just enter in their username and password

import getpass
import telnetlib
HOST = "192.168.1.253"
user = input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Login:")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password:")
    tn.write(password.encode('ascii') + b"\n")
#tn.write(b"ls\n")
#tn.write(b"exit\n")
tn.write(b"en\n")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"show running-config \n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))

No comments:

Post a Comment