0

As said in the title, I am writing a script that should run on Computer A. Computer A should ssh on Computer B, run Program B, then exit the ssh session (Program B still running on Computer B) and then run Program A on Computer A.

I must use expect and I must use a password for ssh to accomplish this.

Here is my try so far (it is not working):

#!/bin/bash

#checks to see if programA is already running

if pidof -x "programA" >/dev/null; then
    echo "Program A already running"
    exit 1
fi

spawn ssh username@{$1}
/usr/bin/expect "assword:"
send "password"
./programB & # run programB in the background
exit
./programA

Here is the script which is supposed to terminate the programs on both computers:

pkill programA
spawn ssh username@{$1}
/usr/bin/expect "assword:"
send "password"
pkill programB
exit

The script to start the programs doesn't work as expected. It starts both Program A and Program B on Computer A. It was also brought to my attention that I need to detach my ssh session because otherwise when I exit it on Computer B Program B will terminate.

Can someone provide me with the script which does what I want it to do, with explanations where I went wrong?

Thank you in advance.

2

Looks almost OK. Try the following (untested obviously)

#!/bin/bash

#checks to see if programA is already running

if pidof -x "programA" >/dev/null; then
    echo "Program A already running"
    exit 1
fi

expect <<EOF
 spawn ssh username@${1}
 expect "assword:"
 send "password\r"
 expect "$ "
 send "nohup ./programB &\r" # run programB in the background
 exit
EOF
./programA

edit: address comment about ssh username@{$1}

| improve this answer | |
  • I understand that it has been brought over verbatim from the question, but this doesn’t look right username@{$1} — I’d imagine the { } want removing. – bxm Dec 8 '19 at 7:17
  • Could you elaborate on what does EOF stand for in expect and what does \r do on the end of the send? Also, I think I would need to call /usr/bin/expect instead of just expect. – DearJohn Dec 8 '19 at 13:53
  • The \r is telling expect to send the "enter" key. The EOF goes with the <<EOF, everything between them is given to the expect program. I would expect that your PATH variable contains /usr/bin so you should be able to use expect rather than /usr/bin/expect to find the expect program. @bxm, thanks, edited. – icarus Dec 8 '19 at 15:32
  • After launching programb, I'd recommend you expect the prompt then send "exit\r" then expect eof -- that will exit from the ssh session more gracefully. – glenn jackman Dec 10 '19 at 14:48

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.