将远程服务器的主机名添加到格式化的 ssh 命令响应中
Adding the hostname of a remote server to a formatted ssh command response
如果我 运行 在远程 REHL 服务器上直接(通过 ssh 终端)执行此命令:
w -uh | awk -vhostname="$(hostname)" '{print "On "hostname ,,"is connected with IP: "}
'
我得到这样的输出:
On remote.server1.address.com accountname is connected with IP: 8.8.8.8
但是,如果我 运行 通过脚本中的 SSH 命令远程执行此命令(以检查多个服务器):
ssh account@remote.server.address.com w -uh | awk -vhostname="$(hostname)" '{print "On "hostname ,,"is connected with IP: "}
'
命令的结果给出本地机器主机名而不是远程服务器:
On local.machine.address.com accountname is connected with IP: 8.8.8.8
On local.machine.address.com accountname is connected with IP: 0.0.0.0
On local.machine.address.com accountname is connected with IP:255.255.255.255*
我正在寻找如下结果。
On remote.server1.address.com accountname is connected with IP: 8.8.8.8
On remote.server2.address.com accountname is connected with IP: 0.0.0.0
On remote.server3.address.com accountname is connected with IP:255.255.255.255*
发生这种情况是因为命令 hostname
在您的本地计算机上是 运行。您需要在引号中传递整个命令,例如:
ssh account@remote.server.address.com "w -uh | awk -v hostname=$(hostname) '{print \"On \"hostname ,$1,\"is connected with IP: \"$3}'"
如果我 运行 在远程 REHL 服务器上直接(通过 ssh 终端)执行此命令:
w -uh | awk -vhostname="$(hostname)" '{print "On "hostname ,,"is connected with IP: "}
'
我得到这样的输出:
On remote.server1.address.com accountname is connected with IP: 8.8.8.8
但是,如果我 运行 通过脚本中的 SSH 命令远程执行此命令(以检查多个服务器):
ssh account@remote.server.address.com w -uh | awk -vhostname="$(hostname)" '{print "On "hostname ,,"is connected with IP: "}
'
命令的结果给出本地机器主机名而不是远程服务器:
On local.machine.address.com accountname is connected with IP: 8.8.8.8
On local.machine.address.com accountname is connected with IP: 0.0.0.0
On local.machine.address.com accountname is connected with IP:255.255.255.255*
我正在寻找如下结果。
On remote.server1.address.com accountname is connected with IP: 8.8.8.8
On remote.server2.address.com accountname is connected with IP: 0.0.0.0
On remote.server3.address.com accountname is connected with IP:255.255.255.255*
发生这种情况是因为命令 hostname
在您的本地计算机上是 运行。您需要在引号中传递整个命令,例如:
ssh account@remote.server.address.com "w -uh | awk -v hostname=$(hostname) '{print \"On \"hostname ,$1,\"is connected with IP: \"$3}'"