Reputation: 327
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.
Upvotes: 3
Views: 15187
Reputation: 323
#!/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"
Upvotes: 1
Reputation: 6661
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
Upvotes: 0
Reputation: 21269
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
).
Upvotes: 1