LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-05-2021, 02:48 PM   #1
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Rep: Reputation: Disabled
I get syntax error from awk command when querying a remote server


Good day.

This query on a local host works just fine:

Code:
-bash-4.2$ df -P | awk '0+$5 >=40 {print}'
/dev/mapper/vg00-root              8125880 4679656   3010412      61% /
/dev/vda1                           245671  132995     95473      59% /boot
I get a syntax error from awk from remote server with this query:

Code:
-bash-4.2$ ssh -q 192.168.210.20 "df -P | awk '0+$5 >=40 {print}'"
awk: cmd. line:1: 0+ >=40 {print}
awk: cmd. line:1:    ^ syntax error
I've tried using backslashes, single and double quotes, but can't find the right answer.

Please advise.

Bjoern
 
Old 08-05-2021, 03:09 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,691

Rep: Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716
Quote:
Originally Posted by brh1 View Post
Good day.

This query on a local host works just fine:

Code:
-bash-4.2$ df -P | awk '0+$5 >=40 {print}'
/dev/mapper/vg00-root              8125880 4679656   3010412      61% /
/dev/vda1                           245671  132995     95473      59% /boot
I get a syntax error from awk from remote server with this query:

Code:
-bash-4.2$ ssh -q 192.168.210.20 "df -P | awk '0+$5 >=40 {print}'"
awk: cmd. line:1: 0+ >=40 {print}
awk: cmd. line:1:    ^ syntax error
I've tried using backslashes, single and double quotes, but can't find the right answer.

Please advise.

Bjoern
Have you tried
Code:
ssh -q 192.168.210.20 "df -P" | awk '0+$5 >=40 {print}'
?????
 
Old 08-05-2021, 03:54 PM   #3
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
Have you tried
Code:
ssh -q 192.168.210.20 "df -P" | awk '0+$5 >=40 {print}'
?????
Thanks for you reply.

Your command works, but please allow me to expand on my issue. When I add a command to identify the remote host (uname -n), it prints the remote host name but then performs the 'df -P' on the local host. Note the hostnames are correct, but the numbers are all identical:

Code:
-bash-4.2$ for scf in {20..73} ; do ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf uname -n;df -P | awk '0+$5 >= 40 {print}';done
Remote-VM201scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM202scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM203scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM204scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM205scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM206scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM207scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
Remote-VM208scf001
/dev/mapper/vg00-root              8125880 4682900   3007168      61% /
/dev/vda1                           245671  132996     95472      59% /boot
If you were to put the uname and df -h in quotes it would work if it weren't for the greater-than symbol.


Bjoern
 
Old 08-05-2021, 04:21 PM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,149

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Code:
for scf in {20..73} ; do 
  ssh -q -o "StrictHostKeyChecking no" 192.168.210."$scf" "uname -n"
  ssh -q -o "StrictHostKeyChecking no" 192.168.210."$scf" "df -P" | awk '0+$5 >= 40 {print}';done
done
or inline true false

Code:
"uname -n && df -P" | awk '0+$5 >= 40 {print}'
The first will work for sure. I'm out of town right now so I don't have a remote to test the second one against.


The problem with formatting like this is harder to find problems imo.

Code:
for scf in {20..73} ; do ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf uname -n;df -P | awk '0+$5 >= 40 {print}';done
The reason your df isn't working is this is what you are passing with the above command. In this case you are running the df on the local machine. Hence your results.

Code:
for scf in {20..73} ; do
  ssh -q -o "StrictHostKeyChecking no" 192.168.210."$scf" uname -n
  df -P | awk '0+$5 >= 40 {print}'
done
Formatting like this is generally easier to read, again imo even if it's more lines. It also makes possible to keep in the 80 character line length which i understand is just good practices in a script.

Last edited by jmgibson1981; 08-05-2021 at 04:34 PM.
 
Old 08-05-2021, 04:37 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930Reputation: 5930
To elaborate on the others post
Code:
do ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "uname -n;df -P" | awk '0+$5 >= 40 {print}';done
"uname -n;df -P" would run on the remote host but the results would be piped to awk on the localhost.

Code:
do ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "uname -n;df -P | awk '\$5>=40 {print}'";done
One way run awk on the remote host would be to use an escape character i.e backslash to avoid variable expansion.

Last edited by michaelk; 08-05-2021 at 04:44 PM.
 
1 members found this post helpful.
Old 08-06-2021, 02:28 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
May I humbly suggest df --output=pcent instead of df -P?
Code:
ssh ... 'uname -n;df --output=pcent'|awk 'NR==1||0+$0>=40'
 
Old 08-06-2021, 11:32 AM   #7
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
May I humbly suggest df --output=pcent instead of df -P?
Code:
ssh ... 'uname -n;df --output=pcent'|awk 'NR==1||0+$0>=40'
Thanks shruggy.

Neat solution, but it only displays column 5 when I need the whole line to identify the partion:
Code:
ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf 'uname -n;df --output=pcent'|awk 'NR==1||0+$0>=40';done;echo;fi

Remote-VM201scf001
 70%
 59%

Remote-VM202scf001
 70%
 59%

Remote-VM203scf001
 70%
 59%

Remote-VM204scf001
 70%
 59%

Remote-VM205scf001
 70%
 59%

Remote-VM206scf001
 70%
 59%

Remote-VM207scf001
 70%
 59%

Remote-VM208scf001
 70%
 59%
Bjoern
 
Old 08-06-2021, 11:46 AM   #8
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by
[code
do ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "uname -n;df -P | awk '\$5>=40 {print}'";done[/code]
One way run awk on the remote host would be to use an escape character i.e backslash to avoid variable expansion.
Thank you Michael

Your solution works, however, it also caught the 9% of the /var mount:

Code:
-bash-4.2$  for scf in {20..73} ; do echo;ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "uname -n;df -P | awk '\$5>=40 {print}'";done
Remote-VM201scf001
Filesystem                1024-blocks    Used Available Capacity Mounted on
/dev/mapper/vg00-root         8125880 5311460   2378608      70% /
tmpfs                         8133628  508544   7625084       7% /run
/dev/vda1                      245671  133086     95382      59% /boot
/dev/mapper/vg00-var          3030800  241028   2616104       9% /var

Remote-VM202scf001
Filesystem                1024-blocks    Used Available Capacity Mounted on
/dev/mapper/vg00-root         8125880 5306844   2383224      70% /
/dev/vda1                      245671  133107     95361      59% /boot
/dev/mapper/vg00-var          3030800  236148   2620984       9% /var

Remote-VM203scf001
Filesystem                1024-blocks    Used Available Capacity Mounted on
/dev/mapper/vg00-root         8125880 5311452   2378616      70% /
/dev/vda1                      245671  133085     95383      59% /boot
/dev/mapper/vg00-var          3030800  235152   2621980       9% /var

Remote-VM204scf001
Filesystem                1024-blocks    Used Available Capacity Mounted on
/dev/mapper/vg00-root         8125880 5311468   2378600      70% /
/dev/vda1                      245671  133086     95382      59% /boot
/dev/mapper/vg00-var          3030800  235052   2622080       9% /var
When I added the '0+' it worked perfectly:
Code:
-bash-4.2$  for scf in {20..73} ; do echo;ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "uname -n;df -P | awk '0+\$5>=40 {print}'";done

Remote-VM201scf001
/dev/mapper/vg00-root         8125880 5311460   2378608      70% /
/dev/vda1                      245671  133086     95382      59% /boot

Remote-VM202scf001
/dev/mapper/vg00-root         8125880 5306844   2383224      70% /
/dev/vda1                      245671  133107     95361      59% /boot

Remote-VM203scf001
/dev/mapper/vg00-root         8125880 5311452   2378616      70% /
/dev/vda1                      245671  133085     95383      59% /boot

Remote-VM204scf001
/dev/mapper/vg00-root         8125880 5311468   2378600      70% /
/dev/vda1                      245671  133086     95382      59% /boot
Thank you,
Bjoern
 
Old 08-06-2021, 12:46 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
An alternative:
Code:
for scf in {20..73}
do
  echo
  ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "/bin/bash -s" <<"_RSCRIPT_"
uname -n
df -P | awk '0+$5>=40 {print}'
_RSCRIPT_
done

Last edited by MadeInGermany; 08-06-2021 at 12:47 PM.
 
1 members found this post helpful.
Old 08-06-2021, 12:53 PM   #10
brh1
Member
 
Registered: Sep 2012
Location: Texas
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
An alternative:
Code:
for scf in {20..73}
do
  echo
  ssh -q -o "StrictHostKeyChecking no" 192.168.210.$scf "/bin/bash -s" <<"_RSCRIPT_"
uname -n
df -P | awk '0+$5>=40 {print}'
_RSCRIPT_
done
Thank you, this one works as well.

Bjoern
 
Old 08-06-2021, 01:47 PM   #11
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by brh1 View Post
Neat solution, but it only displays column 5 when I need the whole line to identify the partition
Ah sorry,I somehow managed to completely misunderstand your intention. When I think of it now, of course displaying just the percentage makes little sense.

Last edited by shruggy; 08-06-2021 at 02:06 PM.
 
  


Reply

Tags
df, ssh



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] VIM syntax highlighting: Use of AWK syntax inside shell scripts kaiserkarl13 Programming 1 03-03-2021 09:59 AM
[SOLVED] I am trying to remote execute a python command vis ssh. I am getting a syntax error from the remote server. buckb Linux - Networking 7 12-29-2015 07:52 AM
[SOLVED] "Error: syntax before '@' token and Error: syntax at 'OTHER' token" bullrider Programming 2 07-27-2009 08:00 AM
Error running blackbox: BScreen::BScreen: an error occured while querying the X se Niceman2005 Linux - Software 4 03-09-2005 09:14 PM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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
Open Source Consulting | Domain Registration