0

Why cant I call the following in python using os?

import os
os.system('telnet 1.1.1.1')

yet when I open a terminal and use the exact command, I can telnet. I get the following when running the code:

'telnet' is not recognized as an internal or external command, operable program or batch file.

I have enabled TelnetClient and TelnetServer

poke
  • 369,085
  • 72
  • 557
  • 602
JonStanley91
  • 19
  • 1
  • 6
  • 1
    so if you open a shell and type `which telnet` what do you get? (on windows use `where telnet.exe`) – Joran Beasley Jul 23 '13 at 17:48
  • windows 7 - C:\Windows\System32\telnet.exe – JonStanley91 Jul 23 '13 at 17:56
  • try printing `sys.path` is `C:\Windows\System32` included in the path? – Joran Beasley Jul 23 '13 at 18:23
  • As a side note, you better use Popen to interact with telnet: http://docs.python.org/2/library/subprocess.html#popen-constructor . Unless your idea is just let the user interact with it and not your python script. – snf Jul 23 '13 at 18:30
  • Is it possible to use python to send login info to telnet? For example, send a username (user) and password(pass) to the host (1.1.1.1) so that it auto logs in? somehow send a string to a process that just called telnet? I want to avoid using external programs, but methods or hard-coded solutions work – JonStanley91 Jul 23 '13 at 18:34
  • Check that Popen function, it will let you control stdin, stderr and stdout. Also the @mK7 method using the telnet lib is cleaner. – snf Jul 23 '13 at 18:39

2 Answers2

0

Just try os.system('C:/Windows/System32/telnet.exe 1.1.1.1 <port number>') and see what happens?

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
user2547836
  • 89
  • 1
  • 7
  • 'C:\Windows\System32\telnet.exe' is not recognized as an internal or external command, operable program or batch file. – JonStanley91 Jul 23 '13 at 18:02
  • The port is 23 - shouldn't that be fine since it is set as default? – JonStanley91 Jul 23 '13 at 18:03
  • Okay so I tried calling the following: os.startfile("C:/Windows/System32/telnet.exe") and got an interesting result: No such file: C:\\Windows\\System32\telnet.exe Why is it adding additional slashes even though I am not supplying them? – JonStanley91 Jul 23 '13 at 18:17
  • The first slash is to escape the second. In strings `\ ` is used to create escape characters like newline (`\n`) or tab (`\t`), so you have to add two of them to say it's just an slash. – snf Jul 23 '13 at 18:32
0

@Jon S.: Programs like Telnet and SSH are interactive in nature. So when you open a telnet it will return something from the shell it is running to handle that particular connection. I haven't tried running the command like you mentioned above but I have used 'telnetlib' of python in Windows successfully in the past and still using it. Here is a snippet that can be of help.

import telnetlib
import os

host_ip = "1.1.1.1"
user = "user"
password = "password"

tnet_hndl = telnetlib.Telnet(host_ip)
print (tnet_hndl.read_until(b"login: "))
tnet_hndl.write(user.encode('ascii') + b"\n")
print (tnet_hndl.read_until(b"Password: "))
tnet_hndl.write(password.encode('ascii') + b"\n")
print (tnet_hndl.read_until(b"# "))
#tnet_hndl.set_debuglevel(1) #enable this if you want to debug more
tnet_hndl.write(b"<enter some windows cmd here>" + b"\n")
print (tnet_hndl.read_until(b"# "))
tnet_hndl.close()

Hope this helps.

mK7
  • 76
  • 2
  • this helps a little - I've already took a look at stuff like this. My issue with this type of code is that my code gets suck while trying to read - I just executed the above and it hung up at print tnet_hndl.read_until(b"login: "). I'm not sure why, but I cant get past any of the .read_until functions – JonStanley91 Jul 23 '13 at 18:40
  • You will have to replace the b"login: " with whatever prompt you get while you use telnet from Windows to your destination machine. – mK7 Jul 23 '13 at 18:53
  • Does spacing matter? When I telnet normally, I get "User Name: a" where the a represents the start of where i can type the username. I assume I would want to do this? print (tnet_hndl.read_until(b"User Name: ")) with one space? I've tried 1 and 2 spaces but still no luck... – JonStanley91 Jul 23 '13 at 18:59
  • Amazing what a little debugging can do... my issue was the '#' was actually a '>' thanks for the help! – JonStanley91 Jul 23 '13 at 19:03
  • Don't forget to 1-up the answer if you found it useful. :-) – mK7 Jul 23 '13 at 19:23
  • i need 15 rep... or i totally would! >. – JonStanley91 Jul 23 '13 at 19:55