• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Running expect script to send commands in SSH

yhelothar

Lifer
I'm looking at this guide here.
http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts/(language)/eng-US
Code:
#!/usr/bin/expect -f
spawn ssh aspen 
expect "password: " 
send "PASSWORD\r" 
expect "$ " 
send "ps -ef |grep apache\r" 
expect "$ " 
send "exit\r"
It works to login to SSH but it doesn't seem to want to do anything after that.

The server that I SSH into has additional messages, would that pose an issue?


*************************************************************************

19940429 All users of this computer system acknowledge that activities on it
may be subject to monitoring; the privacy of activities on this
computer cannot be ensured. All computer account users are required
to read and abide by the ITS Computing and Usage Policies. Please
refer to the web page at:
http://www.usc.edu/its/policies/computing/

*************************************************************************
hpc-login2 is a 64-bit dual CPU with 4 cores each.
*************************************************************************
 
Last edited:
I don't know anything about remote admin, but if you're getting messages that require acknowledgment, you'd have to account for that in your script, no?
 
You should try running your expect script with the -d flag. This will dump out some debugger info so you can see what steps might be failing.
 
What does it do? The last line, send "exit\r", will make it automatically log out after running ps. Does it run show that apache is running? If not, do you know that it really is running?

When you log in manually, does the prompt end in "$ "? if not, that script won't work.

BTW, that link doesn't work for me. I wonder if you meant this one.
 
You should try running your expect script with the -d flag. This will dump out some debugger info so you can see what steps might be failing.
When I put -d in the script, it doesn't seem to show the debugger info. But if I run the command, expect -d, and then enter in each line manually, it'll give me debug info along the way. It looks like it works when I type in each line manually. However, it times out before my send command is successful. I'm actually running a qsub, instead of the apache command. That was just a template I pasted from the site.

The qsub takes about a minute to connect. It seems like the expect script is timing out before it has a chance to connect.

What does it do? The last line, send "exit\r", will make it automatically log out after running ps. Does it run show that apache is running? If not, do you know that it really is running?

When you log in manually, does the prompt end in "$ "? if not, that script won't work.

BTW, that link doesn't work for me. I wonder if you meant this one.

I also omitted the "exit\r" part since I don't want it to exit right away.

But I think it is exiting right away anyways, at least from what I can gather from the debug, it is actually running the qsub.

Thanks for fixing the link.
 
Last edited:
You'd want to use the -d in the expect call itself:

# expect -d scriptname

And it will dump out the debugger info.

With that said, have you tried replacing the qsub with something else?
 
Ah that did the trick for the debug. It looks like it's logging out right after it sends the command. How do I get it to not log out?

Also I tried ls instead of qsub, and it logs out without showing the folder contents.


Here are some of the last lines of the debugger from where things go awry.

sc.edu/its/policies/computing/\r\n\r\n*************************************************************************\r\nhpc-login2 is a 64-bit dual CPU with 4 cores each.\r\n*************************************************************************\r\n\r\n\u001b]0;mikehuan@hpc-login2:~\u0007[mikehuan@hpc-login2 ~]$ "
send: sending "ls\r" to { exp6 }
mike@mike-MS-7514 ~ $
 
Last edited:
I got this working. Basically the script exits out immediately when it's done, so I just needed an extra expect "$ " line to keep it going until the send command is complete.
 
Back
Top