I'm not sure I have the capability to test this right now, but was wondering if someone might advise if it is possible to do something like this:

Run a shell script
    Shell Script SSH's into a machine (Connection requires SSH key only)
        Stuff is run on that machine
        Script run on that machine SSH's into another machine (Connection requires SSH key and password)
            Stuff is run in that machine
            Exit from that machine
        Stuff is run in that machine
        Exit from that machine
    More stuff happens for a few hours

Presumably it's just a case of assembling the required level of script and letting it run? To steal an example from another SoF question:

    ls -la
        ls -la
    ssh -t ${UserName}@server "${sshScript2}"
    ls -la
ssh -t ${UserName}@server "${sshScript}"

Where in the case of sudo stuff I just run sudo -s -u user bash -c script or whatever?

I'm just unsure if this will result in an endless loop, and as the machines I'm connecting to are in a production environment, I can't afford to risk breaking them for the purpose of testing.


Yes. It is possible. But if your script would be longer, I recommend to do this in more commands using ControlMaster, as Ansible does it, otherwise you will get lost in all the quotes, backslashes and stuff you can see in your post.

scp script1.sh remote:/tmp/
ssh remote "bash /tmp/script.sh"

and your script.sh can contain one more similar batch from remote machine to another. Also the the script can contain rm /tmp/script.sh to clean up itself.

  • This is the method I use, though because script.sh has been made for this purpose, I usually add the rm /tmp/script.sh to the bottom of itself. – forquare Aug 26 '15 at 15:14
  • 1
    Yes. It is maybe more elegant solution. I will update the answer. – Jakuje Aug 26 '15 at 15:30
  • Sorry, how (in simple terms) does this go about passing the script to each machine? Is it copied over at SSH connection? or in realtime, also, is the script still blocking, and does it mean I need to plant files or remove them manually (comments suggest I have to rm /tmp/script for each file I use on each target machine so I'm skeptical of this. – XtrmJosh Aug 26 '15 at 17:48
  • First of all you prepare your script and by running the scp you transfer it to remote server and by ssh you will execute. This can dive in recursively. If you leave the rm as last command in your script, you don't need to care for cleanup. – Jakuje Aug 26 '15 at 18:21
  • Oh god, I didn't read your original post properly. I see now, and as much as I hate the idea of copying files around in this way it does seem like the best solution. Time to give it a whirl. Thanks. – XtrmJosh Aug 27 '15 at 7:26

In such case I usually use heredoc to feed a script to remote shell. Not usable when script requires user input from stdin, though.

ssh ${UserName}@server <<EOF1
ls -la
ssh ${UserName}@server <<EOF2
ls -la
ls -la
  • This is elegant solution, but I see potential problem if there is some weird shell that doesn't support newlines in the command like this, ex. tcsh – Jakuje Aug 26 '15 at 18:24
  • Sadly, the script will require user input from stdin, but I appreciate this is a good solution. – XtrmJosh Aug 27 '15 at 7:26

I would suggest to use "-q -o "BatchMode=yes"" option as well as using public key authentication.

Also think about single-quoting the command you'd like to run on the remote server to avoid any problem regarding a potential local interpretation of the given command.

E.g : ssh -q -o "BatchMode=yes" user@server 'ls -al'

This is a fairly common technique (nested ssh). No problem implementing this.

  • Public key authentication already in use, sadly security policies enforce the requirements here, and it must be password + pubkey, which mades life awfully miserable. Looks like roughly what I'll be going for though, depending on the results of passing sh files around... – XtrmJosh Aug 26 '15 at 17:47

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.