LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rsh command issue (http://www.linuxquestions.org/questions/linux-newbie-8/rsh-command-issue-4175452224/)

vicky007aggrwal 03-01-2013 05:54 AM

rsh command issue
 
Hi Guys,
iam not able to run below rsh command having the for loop having back tips in it
please suggest i wasted whole one day to figure it out
but still no solution.
please assits what is the issue
+++++++++++++++++++++++++++++++
rsh -l <user name> <machine name> "for i in `cat ~/file`
do
echo $i
done"
+++++++++++++++++++++++++++++++
i am using redhat linux 5

fortran 03-01-2013 06:25 AM

What do you want to do using this script.
I think this for loop will show every word before space as a separate line.
Have you run this for loop on your system first? Is it giving proper results?
If you want to run on other machine, you can use ssh too.

I can give you example to run the script using ssh.
You have used double quote that can be the problem.
An example to run the script containing loop
Code:

#!/bin/bash
ssh 192.165.xx.xx 'for i in `cat /path/of /the/file` ; do echo $i; done'

My public key is saved as 'authorized_keys' in .ssh of server that's it does not ask me password while running the script.

Where are you getting the problem?
Is it just finished showing command prompt of remote machine and not executing the rest of the script?
Paste your error, if there is any.

shivaa 03-01-2013 07:14 AM

Perhaps you should get any error or message while running it? Can you make a try on:
Code:

~$ rsh -l <user name> <machine name> "for i in `cat ~/file`; do echo $i; done"
Also make a try with what @pavi_kanetkar has said above.

vicky007aggrwal 03-01-2013 09:56 AM

we're using rsh in our project & the actual issue is we're not able to use back tick in a rsh command..

any thoughts how we can use back ticks in rsh commands

shivaa 03-01-2013 10:18 AM

@vicky007aggrwal:
In that case, if login shell for user username on machinename is bash, then you can:
Code:

~$ rsh -l <username> <machinename> "for i in $(cat ~/file); do echo $i; done"
But how can you say that rsh won't accept backticks? Also, as asked above, did you get any error or message(s)?

vicky007aggrwal 03-01-2013 01:39 PM

Thanks shiva & how to use pwd command in rsh as below also
Not working. Please suggest

rsh -l <user name> <machine name> "cd /home/hgs/her/server;ps -ef | grep 'pwd' `

In this also pwd command is showing current directory of my base/source
Machine not destination machine.

Kindly help , m bit naive in these

shivaa 03-01-2013 01:49 PM

Give a try to:
Code:

~$ rsh -l <user name> <machine name> "cd /home/hgs/her/server; pwd"
Do not use ps -ef | grep 'pwd', it shouldn't give you appropriate results.

vicky007aggrwal 03-01-2013 01:52 PM

Thanks shivaa for so prompt reply,but my objective is
To grep the process running from the respective directories
Do you hve any better solution

Can u help me in understand why ps -ef|'pwd' is not apt one

shivaa 03-01-2013 02:00 PM

Sorry, I thought that you just need to check working directory.

You can try:
Code:

~$ rsh -l <username> <machinename> "cd /home/hgs/her/server; ps -aef | grep $(echo $PWD)"
Code:

~$ ps -ef  |grep 'pwd'
It will return all process with name having 'pwd' inside it, which don't make much sense. Instead you can:

Code:

~$ ps -aef | grep $(echo $PWD)
But let know know why you want to get a process containg PWD word or working directory?

jpollard 03-01-2013 02:01 PM

Do the pwd without the ps... the string you get back will be something like '/home/username'.

The ps -ef command will give you strings like "gdm 6765 1 0 Feb27 ? 00:00:00 /usr/libexec/at-spi-bus-launcher" or "username 30558 1 0 08:59 ? 00:00:02 nautilus -n"

Command executed do not necessarily have "/home/username" format... usually they are relative paths, not full paths (as shown in the second line - the command is nautilus... but unless the user types in the full path /usr/bin/nautilus to start the program, the "/usr/bin" will not be present, and neither will the "/home/username/xxx" show for the "xxx" program - only "xxx" will be there.

BTW, the reason your `pwd` only executes locally because your string is being interpreted locally before being passed to the remote system. If you want to pass the `pwd` to the remote system, enclose the command string with single quotes (apostrophe) instead of double quotes.

schneidz 03-01-2013 03:23 PM

i think we volleyed back and forth enough here. can the op explain why the antiquated and insecure program rsh is preferable to ssh ?

also how are you sure that the backticks is what is messing up your script.

you will probably get better results if you paste a sample of the inputs and what you want the output to look like.

vicky007aggrwal 03-01-2013 11:44 PM

thanks everyone for your help, jpollard your suggestion worked when i used the single quotes with back ticks in my rsh command

i have one last issue now, let me explain my scenario to clear out the confusion , i am invoking below command

+++++++++++++++++++++++++++++++++++++
DIR_LOCATION=$1
rsh -l <username> <machinename> 'cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'
+++++++++++++++++++++++++++++++++++++
now what is happening is that $DIR_LOCATION variable is not getting replaced with its value as i am using single quotes but the irony the back tick only works in single
quotes. I tried using doube quotes in place of single but then the command in back tick is not getting executed.

we have one legacy application which is been configured with rsh oonly not ssh,thats why i have to go with that only

Please please share your thoughts & help me in resolving this issue
thanks again for your time.

shivaa 03-01-2013 11:57 PM

This is because you've declared this variable in on your local machine, but trying to use it on remote machine. DIR_LOCATION is not any shell or environmental variable, so your system cannot take it's value until it's declared in the remote system.

Just do like:
Code:

~$ rsh -l <username> <machinename> 'DIR_LOCATION=$1; cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'

vicky007aggrwal 03-02-2013 12:09 AM

Thanks SIva, there is bit more to my use case,actually i canont use $1 directly as i have series of other variables invloved in this process.

As mentioned below APP_STRING & DIR_LOCATION are GLOBAL variables, i cannot afford to write the same variable assignments again in rsh

is there any other approach that i can use, sorry for not narrating the complete scenario earlier but now its complete.

$APP_STRING=`cat configfile | grep -i $1`
$DIR_LOCATION=`echo $APP_STRING |awk -F'|' '{ print $2 }'
rsh -l <username> <machinename> 'cd $DIR_LOCATION; ps -aef | grep `pwd` | grep -v grep| grep -i catalina;echo $?'

shivaa 03-02-2013 01:05 AM

Well, in that case, better to create a script on remote machine, add all stuff in that script and invoke that script from your local machine using rsh.

On remote machine:
Code:

~$ vi /home/username/myscript.sh
#!/bin/bash
APP_STRING=`cat configfile | grep -i $1`
DIR_LOCATION=`echo $APP_STRING |awk -F'|' '{ print $2 }'`
cd $DIR_LOCATION
ps -aef | grep `pwd` | grep -v grep| grep -i catalina
echo $?

Then on local machine:
Code:

~$ rsh -l <username> <machinename> "./myscript.sh"
OR
~$ rsh -l <username> <machinename> "bash myscript.sh"
OR
~$ rsh -l <username> <machinename> "./myscript.sh <something>"

Note: <something> will be used as $1 i.e. argument passed to the script.

Also take a note, do not use $ symbol before variable name while declaring it. But use when invoking it.


All times are GMT -5. The time now is 02:53 PM.