LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   I get syntax error from awk command when querying a remote server (https://www.linuxquestions.org/questions/linux-general-1/i-get-syntax-error-from-awk-command-when-querying-a-remote-server-4175698768/)

brh1 08-05-2021 02:48 PM

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

wpeckham 08-05-2021 03:09 PM

Quote:

Originally Posted by brh1 (Post 6272848)
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}'
?????

brh1 08-05-2021 03:54 PM

Quote:

Originally Posted by wpeckham (Post 6272857)
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

jmgibson1981 08-05-2021 04:21 PM

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.

michaelk 08-05-2021 04:37 PM

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.

shruggy 08-06-2021 02:28 AM

May I humbly suggest df --output=pcent instead of df -P?
Code:

ssh ... 'uname -n;df --output=pcent'|awk 'NR==1||0+$0>=40'

brh1 08-06-2021 11:32 AM

Quote:

Originally Posted by shruggy (Post 6272968)
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

brh1 08-06-2021 11:46 AM

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

MadeInGermany 08-06-2021 12:46 PM

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


brh1 08-06-2021 12:53 PM

Quote:

Originally Posted by MadeInGermany (Post 6273114)
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

shruggy 08-06-2021 01:47 PM

Quote:

Originally Posted by brh1 (Post 6273086)
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.


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