Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 way around? Anyone had experience with this ?

here are some details:

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, wanted to seperate 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

and one more thing the whole code above is part of the for loop and I basically execute same script for list of servers.

For testing purposes, the list has two servers, for some weird and twisted reason only the last/seconds server script works as I expect.

another detail:

all server can see myscript.ksh it is visible and executable to all of them

share|improve this question
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
add comment

2 Answers 2

up vote 3 down vote accepted

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
End-Of-File

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

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.