0

I want to write code to execute some commands through telnet connection so i can get data from server. But i should be connected to server via ssh first, then connect via telnet. I'm already trying implement python script using paramiko & telnet lib. But i don't know why my telnet connection always failed with operation timed out exception.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=usr, password=pswd)
rc = ssh.invoke_shell()
output = rc.recv(65535)
print output
tn = telnetlib.Telnet(iptelnet)
tn.read_until("Username:")
tn.write(usr + "\n")
tn.read_until("Password:")
tn.write(pswd + "\n")
tn.write("some command")
result = tn.read_all()
print result

I'm new in python so i am a bit confused about it, would very much appreciate for your help.

  • 1
    do you know you can run remote commands over ssh? I'm not sure what's the role of telnet here – Marat Apr 30 '20 at 19:37
  • Aside from Marat's fine point, if you really want to use telnet for some reason, presumably you want to connect through ssh, then telnet to _localhost_, with telnet listening only on a loopback interface. Your `iptelnet` makes me think that's not what's happening. – kojiro Apr 30 '20 at 19:38

0 Answers0