Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want a script that ssh to remote server and then run a script which is at my local machine and then store the output on my local machine

Expect script for ssh to remote server:

#!/usr/bin/expect -f

set Timeout 2
        set IPaddress
        set Username "username"
        set Password "password"

        spawn ssh $Username@$IPaddress
        expect "ssword:"
        send "$Password\r"

script to be run on remote machine and gather the data:


for serveraddress in `cat list.out`
"seeisso $serveraddress | grep -E -i ' os |proddropdown'"
done >> getos

=== Not sure how to : 1. combine both and get the result. 2 in expect script it does not takes the \r and returns to newline.

share|improve this question

Quick sample

#!/usr/bin/expect -f
set timeout -1
set Username "user"
set password "pass"
set ipaddress "host"
set script "script-name"

spawn $env(SHELL)
send -- "ssh $Username@$ipaddress $script\r"
expect "assword: "
send -- "$password\r"
send -- "exit\r"
expect eof

That will work to execute the script on the remote side. I didn't toy with it too much but when it's done you have to Ctrl+C to exit expect. At this point I'd be setting up ssh keys for a small environment and for a larger use puppet or something to manage it then for even larger go for single sign-on probably kerberos.

share|improve this answer
I have to put script on remote machine? – nis Apr 24 '13 at 16:24
is there a away i can execute the script which is on my local machine ? – nis Apr 24 '13 at 16:25
what are the -- dashes after "send" for? – javadba May 29 '15 at 5:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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