I have been tasked at work with automating a command to be run on a remote system. Thus far, I have most of the ssh command syntax worked out, but the problem is, when I add the remote command to the end, it will connect, but then freeze without running the remote command. I suspect this is because the command is being run before the login has fully completed. The remote system is not a normal Linux system. It's a Watchguard firewall, so it's sort of a proprietary OS, which makes things more difficult. Is there a way for me to make a script that pauses for five or ten seconds before inputting the command? My boss mentioned an application they used to use which automated the process of SSHing into the system and running the command, but he can't remember what it was, so if anyone knows of an application that can do that in Linux, that would work, too.


Use the following expect script (you probably need to install expect first):

spawn ssh -l myuser
expect "assword:"
send "mypassword\r"
expect "#"
sleep 5
send "command\r"
expect "#"
send "exit\r"

This script runs the command ssh -l myuser and waits until the string assword: is displayed in ssh session, then types the password ending it with \r, that is the Enter key (I purposely omitted "p" from the "password:" prompt to accommodate for both upper- and lowercase version). Then it waits until OS prompt appears (if OS prompt is not #, you need to change that appropriately), waits another 5 seconds and types command, followed by Enter. Waits again until the prompt appears (ie. command has finished) and types exit followed by Enter to end the session and disconnect.

  • You are the freaking man! Thank you! It worked perfectly. – Lexster Jan 22 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.