I have two scripts. Both scripts are on the same server (serverA)

The first one (serverA) makes an ssh connection to the other server (serverB), and executes the second script,


ssh $ipaddress var="$var1" "bash -s" < $script2

script2 then executes on serverB.

Is there a way to combine these into one script? So it would start by making the ssh connection, then execute the rest of the script, something like:


#Make an SSH Connection to another server
ssh $ipaddress

#Now Execute the rest of the script
do stuff here . . .
and here . . .
ssh $ipaddress /bin/bash <<END
do stuff here . . .
and here . . .

assumming script2 in local dir

if scp script2 $ipaddress:/tmp
   ssh $ipaddress /tmp/script2 "arg" >& /my/result.$ipaddress
   ssh $ipaddress rm /tmp/script2 

you'll pay an extra transfert, but if script2 is non trivial (e.g. not just a tail or wc ), it might be worth.

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.