LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk on remote server (https://www.linuxquestions.org/questions/linux-newbie-8/awk-on-remote-server-4175538471/)

linux_project 04-01-2015 04:22 PM

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.

joe_2000 04-01-2015 04:41 PM

Your remote command has different quoting. You cannot have single quotes wrapped with single quotes...

joe_2000 04-01-2015 04:58 PM

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 }'"

linux_project 04-01-2015 05:41 PM

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)

Keith Hedger 04-01-2015 06:16 PM

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.

linux_project 04-01-2015 06:52 PM

I have to run the same command on 100 of machines.

dijetlo 04-01-2015 07:14 PM

Quote:

I have to run the same command on 100 of machines.
So if you want to automate it, you'll need to use pexpect or a similar workaround (unless you want to hammer in your login/password hundreds of times). It's probably going to be easier to have the master script create a directory in temp, scp the individual scripts onto the target server, execute them and scp the output to a single collection point ( before deleting the working directory, disco, disco and on to the next). It's slower on a per server basis but if you break up the servers you have to do into smaller groups, you can run several instances of the same master script in parallel (all the 'work' is being done remotely so it's not a significant load for the master server). It also has the advantage of being modular, with significant parts of the code base being re-usable.
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:

I have to run the same command on 100 of machines.
So if you want to automate it, you'll need to use pexpect or a similar workaround (unless you want to hammer in your login/password hundreds of times). It's probably going to be easier to have the master script create a directory in temp, scp the individual scripts onto the target server, execute them and scp the output to a single collection point ( before deleting the working directory, disco, disco and on to the next). It's slower on a per server basis but if you break up the servers you have to do into smaller groups, you can run several instances of the same master script in parallel (all the 'work' is being done remotely so it's not a significant load for the master server). It also has the advantage of being modular, with significant parts of the code base being re-usable.
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.

linux_project 04-01-2015 07:23 PM

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 .

dijetlo 04-02-2015 02:22 AM

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.

pan64 04-02-2015 03:26 AM

actually you can do the following too:
Code:

ssh <remote command> | (local) grep | (local) awk
#and also
#that grep and awk can be combined into one single script, so:
ssh host 'free -g' | awk '/Mem/ { print $2 }'
# should do the job


allend 04-02-2015 08:23 AM

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>
Note this passes all the output from 'free' and 'cat /etc/os-release' commands run on the remote host to be processed using 'awk' on the local host.
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.