通过 ssh 远程连接使用 awk 时需要建议来分隔波浪号

通过 ssh 远程连接使用 awk 时需要建议来分隔波浪号

再会。

我试图在通过 ssh 远程连接使用 awk 时分隔波浪号。当我在本地主机上运行该命令时,该命令按预期工作。当我尝试在远程服务器上使用相同的命令时,它返回语法错误。

-bash-4.2$ /opt/SMAW/bin/RtpDumpLog -s 2111250000 | awk '$4 ~ /major/ { print }'
05-12-2021 02:11:20  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[32721]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:11:21  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[32724]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:11:22  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[32726]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:11:25  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[911]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:11:27  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[914]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:12:10  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[1081]: fatal: Write failed: Connection reset by peer [preauth]"
05-12-2021 02:12:11  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[1083]: fatal: Read from socket failed: Connection reset by peer [preauth]"
05-12-2021 02:15:24  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[2332]: fatal: Write failed: Connection reset by peer [preauth]"
06-12-2021 19:18:14  141-9994  major        "OEM:cscf0002vm003oam001:sshd: sshd[30695]: fatal: Write failed: Broken pipe [preauth]"

到远程服务器:

-bash-4.2$ ssh -q 192.168.210.142 "/opt/SMAW/bin/RtpDumpLog -s 2111250000 | awk '$4 ~ /major/ { print }'"
awk: cmd. line:1:  ~ /major/ { print }
awk: cmd. line:1:  ^ syntax error

我尝试使用反斜杠来分隔波浪号:

-bash-4.2$ ssh -q 192.168.210.142 "/opt/SMAW/bin/RtpDumpLog -s 2111250000 | awk '$4 \~ /major/ { print }'"
awk: cmd. line:1:  \~ /major/ { print }
awk: cmd. line:1:  ^ backslash not last character on line

请指教。

问候, 比约恩

答案1

不要awk在远程主机上运行:

ssh -q 192.168.210.142 /opt/SMAW/bin/RtpDumpLog -s 2111250000 |
awk '$4 == "major"'

问题不在于波形符,而是您$4在双引号字符串中使用的事实。您会注意到 已$4被删除,因为它的值在调用 shell 中为空。

如果您需要在远程主机上运行完整的管道,我建议使用此处引用的文档中给出的脚本来执行此操作:

ssh -q 192.168.210.142 sh -s <<'SCRIPT_END'
/opt/SMAW/bin/RtpDumpLog -s 2111250000 |
awk '$4 == "major"'
SCRIPT_END

使用 时-ssh将从其标准输入流读取命令,该输入流连接到ssh,从此处文档获取命令。我选择sh在此处显式调用,因为我不知道您的登录 shell 是什么。如果它是一个标准sh的 shell,那么你可能会放弃sh -s.

相关内容