1832282 Members
1852 Online
110041 Solutions
New Discussion

Re: remote run program

 
Son dam bi
Advisor

remote run program

I have the below script, it is ok to run on local server

#cat /etc/passwd | awk -F: '{ print $1}'

root
bin
daemon
adm
lp
sync
shutdown
halt


But if I try to run it at remote server , i tried ssh root@remoteserver "cat /etc/passwd | awk -F: '{ print $1}'" , then the output is as below , the output format is different , can advise what is wrong , how can i use the existing script to run on remote server ? I have many program also want to run at remote server , can advise how can I make it work ? thx a lot




root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
7 REPLIES 7
Roopesh Francis_1
Trusted Contributor

Re: remote run program

cat /etc/passwd|awk -F : '{print $1}' should work however you may try
cat /etc/passwd|cut -f 1

cut -f
where list is a list of fields assumed to be separated in the file by a delimiter character (see -d); for example, -f 1,7 copies the first and seventh field only. Lines with no field delimiters will be passed through intact (useful for table subheadings), unless -s is specified
saravanan08
Valued Contributor

Re: remote run program

Hi

This will work

ssh root@remoteserver cat /etc/passwd | cut -f 1 -d":"

it will do the same thing as your script

thanks

Saravanan
saravanan08
Valued Contributor

Re: remote run program

hi

remove the double quote it will work

Example:
ssh root@remoteserver cat /etc/passwd | awk -F: '{ print $1}'

it will work
Son dam bi
Advisor

Re: remote run program

thx replies ,

it works , but may I ask what is the reason of it ? as I have other program has the same problem , I would like to fix by myself . thx
Laurent Menase
Honored Contributor

Re: remote run program

ssh ... "cat /etc/passwd | awk -F: '{ print \$1}'"


because else $1 is interpreted because you are between double quotes.
so without \ it is the same as
cat /etc/passwd | awk -F: '{ print }'

so \$1 is necessary.


Dennis Handly
Acclaimed Contributor

Re: remote run program

>Laurent: ssh ... "cat /etc/passwd | awk -F: '{ print \$1}'"

That's the solution I found too.
I first tried to put the command in '' but the shell doesn't seem to want to nest '', even with \'.

>because else $1 is interpreted because you are between double quotes.

Yes, it is that simple.
You can also use fire to fight fire:
XX='$1'
ssh host -n "awk -F: '{ print $XX }' /etc/passwd"
Laurent Menase
Honored Contributor

Re: remote run program

on my system it works.

depending of the shell and unix used by your local and remote user
you may need to have 3 \ in place of 1
like my old aunt said, when it doesn't work with 1 add 2 others.
but in my case 1 \ is enough.
ssh root@myremote " awk -F: '{print \$1 }'root
daemon
bin
sys
adm
uucp
lp
nuucp
hpdb
nobody
www
laurent