Is it possible to run one part of the ksh/sh script on one server then ssh to another server and continue with the rest of the script? Or is there a work-around?

I have a user with which I don't have to authenticate each time I access a server, so I can hop from one server to another without any keyboard interaction.

I've already tried this, trying to separate some logic into another script then :

MYSCRIPT="nohup ksh myscript.ksh > mylog.log &"
ssh servername ${MYSCRIPT}

I tried this but it doesn't seem to execute this script on another server.

The whole code above is part of a for loop; I execute the same script for a list of servers.

For testing purposes, the list has two servers; for some weird and twisted reason, only the last (or second) server script works as I expect.

All servers can see myscript.ksh -- it is visible and executable to all of them.

  • 2
    Perhaps post a bit more code. My test example which looks just like your code seems to work fine. – Steven D Feb 12 '11 at 3:58
  • Where is myscript.ksh? Unless it's in your home directory, you should be running myscript.ksh, not ksh myscript.ksh. – Mikel Feb 12 '11 at 20:47
  • If your routing/fw allows it, I'd rather suggest that you coordinate your remote server access from one central host without hopping around (at least, that's how I fetch my backups). – wag Feb 12 '11 at 21:13

It sounds like you want to run a sequence of commands on another server without having to log in multiple times. To accomplish this, you could do:

local_command sequence
ssh remote-server << End-Of-File
        remote_command 1
        remote_command 2
        remote_command 3

This would run remote_command 1, and when it finishes, run remote_command 2, then remote_command 3 all on the remote server

  • I like this approach, can you please give an example, or a man page, I think this is what I want.. I want to execute entire script many lines on multiple servers – c0mrade Feb 12 '11 at 21:49
  • Gotcha – c0mrade Feb 13 '11 at 2:41

To be honest your wording is a bit vague but let's trace possible causes:

What you described should work just fine, but see ssh-copy-id source to look for discrepancies with your own script (it is a shell script bundled with OpenSSH).

You mentioned that the command does not work on one of your machines, maybe the server has a different setup preventing the script to start in the first place.

Also, you mentioned authentication, so I suppose you're using passwordless public key authentication, and you might want to restrict the commands authorized with the associated key: see man ssh, section AUTHORIZED_KEYS FILE FORMAT.

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.