LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2015, 05:22 PM   #1
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 8

Rep: Reputation: Disabled
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.
 
Old 04-01-2015, 05:41 PM   #2
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Your remote command has different quoting. You cannot have single quotes wrapped with single quotes...
 
Old 04-01-2015, 05:58 PM   #3
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
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 }'"
 
Old 04-01-2015, 06:41 PM   #4
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
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)
 
Old 04-01-2015, 07:16 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,252

Rep: Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559
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.
 
Old 04-01-2015, 07:52 PM   #6
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
I have to run the same command on 100 of machines.
 
Old 04-01-2015, 08:14 PM   #7
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,307
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 04-01-2015, 08:23 PM   #8
linux_project
LQ Newbie
 
Registered: Apr 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
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 .

Last edited by linux_project; 04-01-2015 at 08:24 PM.
 
Old 04-02-2015, 03:22 AM   #9
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,307
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 04-02-2015, 04:26 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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
 
1 members found this post helpful.
Old 04-02-2015, 09:23 AM   #11
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Problem printing column on remote host via script using awk keif Programming 2 10-25-2013 11:00 AM
Installing a linux-distro remote desktop on an already remote debian server... errr? Sabinou Linux - Newbie 2 04-26-2012 06:29 PM
What can do scheduled remote back-ups to a remote ftp server? eindoofus Linux - Software 1 10-07-2011 01:45 PM
How to call Shell Script on a remote server with remote servers env variables need Linux - Server 1 10-14-2009 09:37 PM
SSH - Remote aplications in remote server Urien Linux - Newbie 11 04-04-2009 05:02 AM


All times are GMT -5. The time now is 03:03 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration