awk syntax error with ssh

I am attempting to ssh to a server and grab the line from /etc/group if it contains a specific GID.

Example line from /etc/group:

mysql:!:64600:mysql

Run locally on <host>:

# awk -F: '$3 == 64600 {print $1}' /etc/group
mysql

Attempt to ssh to <host> and execute:

# ssh <host> awk -F: '$3 == 64600 {print $1}' /etc/group
Syntax Error The source line is 1.
The error context is
>>> == <<<
awk: 0602-500 Quitting The source line is 1.

Enclosing the command in single or double quotes gives the same error:

# ssh <host> "awk -F: '$3 == 64600 {print $1}' /etc/group"
Syntax Error The source line is 1.
The error context is
>>> == <<<
awk: 0602-500 Quitting The source line is 1.

I figure it has to have something to do with the expansion of $3 before the command is passed to <host> but I can't figure out how to protect it.

Help? Please?

 ssh somenode "awk -F':' '\$1==\"jmcnama\" {print $1}' /etc/group"

works fine for me. SHELL==/bin/bash

A clumsy method is to replace the ' by '\'' and put the remote command in ' ' .

ssh <host> 'awk -F: '\''$3 == 64600 {print $1}'\'' /etc/group'

The better method:
put the normal command in a script cmd.sh

#!/bin/sh
awk -F: '$3 == 64600 {print $1}' /etc/group

and execute it remotely

ssh <host> /bin/sh  < cmd.sh
1 Like

Thanks Jim, it works for executing the command but I'm using ksh and as soon as I try and put it in a variable it errors:

# AUDITGROUP=`ssh ${SRV} "awk -F':' '\$3==\"64600\" {print $1}' /etc/group"`
Syntax Error The source line is 1.
The error context is
>>> == <<<
awk: 0602-500 Quitting The source line is 1.

---------- Post updated at 12:09 PM ---------- Previous update was at 12:05 PM ----------

Thanks MiG. It works and although it might appear "clumsy" I prefer that so I don't have to remember to include the cmd.sh file if I copy the base script to another server.