awk on remote server
Hi,
I am running awk command on remote server it is not giving desired results but if i run the same command on local server. it works as expected. Can anybody help me to resolve this and let me know the logic behind this? Remote server # ssh test 'free -g | grep Mem | awk -F " " '{ print $2 }'' awk: cmd. line:1: { awk: cmd. line:1: ^ unexpected newline or end of string local server # free -g | grep Mem | awk -F " " '{ print $2 }' 31 Thanks in advance. |
Your remote command has different quoting. You cannot have single quotes wrapped with single quotes...
|
Played around with this a bit. Seems that you also have to escape the $ symbol. Try e.g. this
Code:
ssh test "free -g | grep Mem | awk -F \" \" '{ print \$2 }'" |
Thanks Joe for answering. both way worked. Now i forwarded to one step more . I want to concatenate two or more command output to single line. I tried with echo -n and tr "\n" '' in the first command but it didn't work. Actually i have to execute many commands on remote server that i want the commands output like " output1 output2 output3 .. so on "
ssh test "" lsb_release -d | cut -f2 -d ':' | xargs | tr "\n" ' '" ; free -g | grep Mem | awk -F \" \" '{print \$2}' " CentOS release 6.2 (Final) |
Rather than using these long complex and error prone single line commands why not just stuff them into a script on the remote server and then just run the script?
If you keep a local copy of the scriptl you can test to make sure it works, change it as needed and then just upload it to the remote server. |
I have to run the same command on 100 of machines.
|
Quote:
You might try using python for this or at least looking to migrate this kind of work to python as you develop the skill set for it. ---------- Post added 04-01-15 at 08:14 PM ---------- Quote:
You might try using python for this or at least looking to migrate this kind of work to python as you develop the skill set for it. |
Hi Dijetlo,
I am executing the same with script. The server can do ssh password less so this is not pain in my case . No need to reenter username/password again and again. The way you suggested is also looking doable but the way i am looking should be doable too. I am only lacking on some syntax . |
OK, so if you're executing them with a local script, what is the question you're currently asking, how to collect the data?
We'll, it's on your terminal, isn't it? Save the putty session. |
actually you can do the following too:
Code:
ssh <remote command> | (local) grep | (local) awk |
Building on what has gone before, and mindful of the desire to execute many commands on remote server that results in output like " output1 output2 output3 .. so on "
Code:
ssh <user>@<host> 'free;cat /etc/os-release' | awk '/Mem/{x[1]=$2}/PRETTY_NAME/{x[2]=gensub(/^.*=/,"","g")}END{print x[1],x[2]}' >> <ouputfile> I have used the /etc/os-release file as it is more widely accepted than lsb_release. http://0pointer.de/blog/projects/os-release.html |
All times are GMT -5. The time now is 05:27 AM. |