Open interactive telnet session from Python

前端 未结 1 974
渐次进展
渐次进展 2021-01-25 01:50

I am currently writing a small script in Python which reads some possible connections from a database (triple of node name, telnet IP address and telnet port) and present that t

相关标签:
1条回答
  • 2021-01-25 02:42

    It's python, everything is included. There is a telnet module in the standard library.

    import getpass
    import telnetlib
    
    HOST = "localhost"
    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")
    
    print(tn.read_all().decode('ascii'))
    
    0 讨论(0)
提交回复
热议问题