1
votes

Im currently using the line of script below to ssh from my local machine to a server (lets call it ip-address1) then from that machine i want to ssh to another machine (lets call this machine ip-address2). The script i use is as follows:

sshpass -p mypassword ssh -tt user@ip-address1 ssh -tt -i /root/.ssh/vm_private_key user@ip-address2 "pwd; ls;"

The problem is only the first command (pwd) executes on ip-address2 then it closes and the ls command executes on ip-address1 before it then closes. I want both commands to execute on ip-address2. The output in my terminal is something like the following:

/home/user (pwd command executing here)
Connection to ip-address2 closed.
//files then get outputted here (ls command executes after ip-address2 has 
closed)
Connection to ip-address1 closed.

I think there may be something wrong with my quotation but i cant figure out what. Please help.

Thanks.

1

1 Answers

1
votes

I don't have any way to test this, but try the following:

sshpass -p mypassword ssh -tt user@ip-address1 \
    "ssh -tt -i /root/.ssh/vm_private_key user@ip-address2 'pwd; ls;'"

You definitely need to quote the entire command you want to run on the ip_address1, including the command you'll pass to ip_address2.

Edit

I'm in an environment where I have multiple machines to test; the following command works for me:

ssh snewell@<host sanitized> \
    "ssh <host2 sanitized> 'hostname; ls -a <path sanitized>;'"

hostname definitely displays the result of the final server (host2), and ls is listing a directory that the first host doesn't have.