3

I would like to execute a command on a remote machine using telnet as,

telnet x.x.x.x command or similar

I want to include this as part of a script in Python ( subprocess ) and hence need it in one line. Or, are there any other ways to do the same?

Any help is appreciated.

Thanks.

2
  • 1
    Is this useful? To send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. docs.python.org/library/subprocess.html#popen-objects Commented Apr 24, 2012 at 11:28
  • Yes, you could use both stdin and stdout to communicate with a Telnet binary. But why not just use telnetlib instead, as I suggested? Commented Apr 30, 2012 at 11:28

3 Answers 3

1

Rather than relying on subprocess, you could try Python's built-in telnetlib instead.

A complete example that does almost exactly what you want is available as an example in the documentation: http://docs.python.org/library/telnetlib.html#telnet-example

I would personally also see if SSH is available on the target system. Not only will you be using a secure connection, but you can also set up SSH keys and use SSH's built-in support for executing a single command (e.g. ssh [email protected] ls -l).

Sign up to request clarification or add additional context in comments.

1 Comment

Are we able to run all types of commands on a remote server using telnetlib that you suggested here ?
1
#!/bin/sh empty -f -i in -o out telnet foo.bar.com empty -w -i out -o in "ogin:" "luser\n" empty -w -i out -o in "assword:" "TopSecret\n" empty -s -o in "who am i\n" empty -s -o in "exit\n" 

http://empty.sourceforge.net/

Comments

0

Maybe this solution will be useful, but as stated in there, it won't work in case you need user/password authentication.

In order to skip manual user/password authentication, setting up ssh keys is the way to go. There is a short explanation about this here: SSH to a server without password for Admin Ease

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.