LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2006, 03:34 PM   #1
p_test
Member
 
Registered: Aug 2004
Location: Jaipur(Rajasthan), INDIA
Posts: 35

Rep: Reputation: 15
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
 
Old 10-01-2006, 04:39 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 10-01-2006, 05:28 PM   #3
Markie1006
LQ Newbie
 
Registered: Oct 2006
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
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

Last edited by Markie1006; 10-01-2006 at 10:22 PM.
 
Old 10-02-2006, 01:34 AM   #4
p_test
Member
 
Registered: Aug 2004
Location: Jaipur(Rajasthan), INDIA
Posts: 35

Original Poster
Rep: Reputation: 15
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
 
Old 10-02-2006, 03:15 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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).
 
Old 10-02-2006, 03:40 PM   #6
Markie1006
LQ Newbie
 
Registered: Oct 2006
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
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'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't SSH to remote machine: Connection closed by remote host Avatar Linux - Networking 35 10-23-2017 12:21 AM
Logging into a remote host automatically through shell script patil_reddy Red Hat 1 06-13-2006 06:23 AM
Resolving <www.some remote host>.... failed: Host not found. koodoo Linux - Newbie 2 06-27-2005 08:48 AM
Running a script in background on a remote machine TheCoffeeMug Linux - Networking 3 11-06-2003 10:08 AM
remote host connection script question??? gopi_20us Programming 4 03-01-2002 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration