LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   running script on a remote host (http://www.linuxquestions.org/questions/programming-9/running-script-on-a-remote-host-488531/)

p_test 10-01-2006 03:34 PM

running script on a remote host
 
Hi

i want to run a script that will
1. login to a remote host
2. Run some command on the remote host
3. logout of the remote host.

i tried with this format...
*******************************
#!/bin/bash
rsh remotehost

......
commands
......

exit
*******************************

But when I run this script the specified commands run on my localhost & not the remote host.
Can any one please let me know how to make the commands( normal unix commnads mount & ls) on the remotehost ..... & also the exit command did not log me out of the remote host.

Please provide me with a solution

Thanks & Regards

Mara 10-01-2006 04:39 PM

The trick is that rsh ends after your log out. It means you need to pass your commands to rsh, not keep as separate ones.

If you have a longer list of commands to do at the remote site, create a script with them. Then, change your main script in such a way that it first uploads the script with remote side commands (using scp, for instance) and then runs it using rsh - just add script name at the end of the rsh/ssh command. Finally you can remove the script you have run, also using rsh/ssh, this time with rm command.

Markie1006 10-01-2006 05:28 PM

You can do this using a 'here-document' - http://www.faqs.org/docs/abs/HTML/here-docs.html

Like this;-

Code:

$ rsh mbp <<EOF
> cat /etc/motd
> hostname
> uname -a
> EOF
Welcome to Darwin!
mbp.local
Darwin mbp.local 8.7.1 Darwin Kernel Version 8.7.1: Wed Jun  7 16:19:56 PDT 2006; root:xnu-792.9.72.obj~2/RELEASE_I386 i386 i386


p_test 10-02-2006 01:34 AM

Hi Markie1006

Your suggestion worked for me.. the following script( scr ) worked for me.

******************************************
rsh remotehost001 <<EOF
mount
....
commands
.....
EOF
******************************************

But ..I m doing this to test about 100 machines( remotehost001 to remotehost100 ) around me. So I want to redirect this output to a particular file ( say reportfile ) on my localhost . But unable to do so.

When i run this script ( #./scr > reportfile ) the script logs onto other machine and closes too but I don get any result in the reportfile( this ile is on my localhost).
Please help me once again...

Thanks & Regards

Mara 10-02-2006 03:15 PM

Where does the log go to when you run the script? To a file or to standard output? If to standard output, redirection should work. But if it goes to file, you need to copy it from the remote location (or open it remotely, from the server, on your machine).

Markie1006 10-02-2006 03:40 PM

It would probably be easier to put it into a script, in which case it should just be as simple as...

Code:

#!/bin/sh
LOGFILE=rsh.log

cat /dev/null > $LOGFILE

for HOST in host1 host2 host3 host4; do
  rsh $HOST <<EOF >> $LOGFILE 2>&1
    cat /etc/motd
    hostname
    uname -a
EOF

done

It should be perfectly fine to indent the rsh commands to make the script more readable, but you MUST place the ending EOF (or whatever tag you decide to use) at the BEGINNING of the line. Otherwise it won't get recognised and end the 'input'


All times are GMT -5. The time now is 10:56 AM.