I am having some issues trying to connect through telnet to a mail server.The main problem that I am having is that I need to create a script that logs me to the destination and I can't find a way to echo the password.
What I tried:
telnet host -l username ; echo 'password'
And still it asks for my password.Is there any way to fix this or I am doing something wrong?
Try
eval
:In this example, my remote command is '?' (help).
The
sleep
s (maybe not all of them nor these times; trial-error...) are needed to avoid telnet misses some inputs.The user and password are passed as variables (
$user
and$pass
). Take into account security recommendations to store the password if you are scripting.AFAIK, you won't be able to automate telnet that way. But it is still possible - even if it is a very bad idea (I'll elaborate on that later).
First why does your try fail :
password
on your own terminalWhat should you do instead :
telnet -X SRA
)Here is an example that allowed me to telnet to my own machine :
It correctly logs me into my box, executes
echo foo and bar
(essential command :-) ) and disconnectsNow why you should never do that :
If you really want to pass command in a batch way to a remote server, you should instead try to use
ssh
which :If you cannot use
ssh
(some sysadmin do not like to have uncontrolled input ssh connections) you could also try to usersh
. It is older, far less secure, but at least was designed for batch usage.Have you tried using the expect command ?? You will have to create a script where you identify the 'expected' response from the server e.g. 'Password:' and then supply the password in the script. The following will explain: https://en.wikipedia.org/wiki/Expect - A good example is also shown here: http://en.kioskea.net/faq/4736-shell-script-for-telnet-and-run-commands
First of all, you can use
eval
:Make sure to replace
user_name
,pass
,?
which is the command you want to run andhost_address
where your telnet host is listening; for me it is a local IP.It’s surprisingly easy to script a set of command and pipe them into the telnet application. All you need to do is something like this:
The only problem is the nagging login that you have to get through… it doesn’t show up right away. So if you pipe in an “echo admin” and then “echo password,” it will happen too quickly and won’t be sent to the server. The solution? Use the sleep command!
Adding in a couple of sleep 3 commands, to wait three seconds, solves the problem. First we’ll echo the username and password, and then we’ll echo the reboot command, and each time we’ll wait three seconds between. The final command will reboot the server immediately:
You can put this into a shell script and run it whenever you want. Or you can add it to your cron like this (on OS X or Linux):
Add this line somewhere:
This will reboot your router at 7:01 AM each morning.