Hello,
I need to write a python script for performing telnet between two windows machine, that is Host and Client. After doing Telnet Successfully I need to run some application on other machine and there in that application I need to give commands via my script aswell after giving command I need to receive feedback in my script.

I am Good in C/C++ but novice in Python, So Please tell me how to Begin. Any good link or good book would help.
Below is the code which I found on several websites but its not working.
Thanks

#!usr/bin/python
import getpass
import sys
import telnetlib

HOST = "192.168.10.15"
PORT=22
TIMEOUT=10
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

COuld you please give us some more information about the way the program is failing, e.g., error messages, the stack trace, and similar information? Also, what version of Python are you using? The code you posted is for Python 2.x; if you are running Python 3.x, it would fail, because several things have been changed or removed since the older version.

Also, are you certain that the host you areconnecting to has a Telnet service running? Most desktop or workstation systems have Telnet disabled by default.

Thanks for the reply Schol-R-LEA :)

The script asks for login and password and then hung up.

Python version is 2.7

Yes telnet is enabled both side. I have tried doing telnet manually, its Successful.

Hmmn, here's an old trick that may be appropriate here: drop the first letter of the words you are matching against, such that you are waiting for 'ogin:' and 'assword:'. The reason this may work is because the first letters may or may not be capitalized; if you assume capitalization when there is none, or vice versa, you may end up waiting indefinitely.

I tried removing first letter but still its not working.
Here is another script for same purpose. Its successfully doing the telnet login but after that its not taking the command, that is the last third line is not getting executed.
Thanks

import telnetlib

tn = telnetlib.Telnet('192.168.10.15')

tn.set_debuglevel(9)

tn.read_until('login: ', 5)

tn.write('vishal\n')

tn.read_until('Password: ', 5)

tn.write('password\n')

tn.read_until('cmd.exe ', 5)

tn.write('dir\n')

print "Read",tn.read_until('cmd.exe ', 5)

Unfortunately, I don't have any site to try telnetting into, so I can't reproduce your problem. If I knew where you were trying to get to (the address you are using is a local one, not a global Intenet address), I could try telnetting into the same site, but unfortunately that probably isn't possible to do from outside of your local network.

Thanks Schol-R-LEA for the reply
I got it working. Seems to be the error was because of the double inverted comma. :)

import telnetlib
command = 'cd /home/ram/Desktop/'

tn= telnetlib.Telnet('192.168.10.170')

tn.set_debuglevel(9)

tn.read_until('login: ', 5)
tn.write('ram\n')

tn.read_until('Password: ', 5)
tn.write('password\n')

tn.write(command+'\n' )
tn.write('python f_run.py\n')

tn.write('password\n')
tn.read_until('bash-2.05$ ',5)

tn.write('mkdir ram\n')

print "read",tn.read_until('Bash-2.05$ ', 5)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.