shell: remote echo hostname and write it locally

2019-09-21 03:50发布

Part 1) I try to execute an echo hostname on my remote host (using ssh). just the hostnamecommand it works. But I have to echo the hostname because I have to copy it.

Part2) Paste the copied hostname (of the host) to a folder which is local. Can someone help me? I tried:

ssh -tt -i key.pem centos@ec2xxx .amazonaws.com sudo sh -c \ "echo `hostname`" >> /myfile

But this is just giving me an empty line. I have to use echo in it (assignement)

标签: shell ssh echo
2条回答
Juvenile、少年°
2楼-- · 2019-09-21 03:56

This should work:

ssh -tt -i key.pem centos@ec2xxx.amazonaws.com 'sudo sh -c "echo `hostname`"' >> /file
查看更多
萌系小妹纸
3楼-- · 2019-09-21 04:13

You're jumping through unnecessary hoops. You can simply run hostname as the remote command, and redirect ssh's stdout:

ssh "$host" hostname >>/file

For example:

$ rm -f /tmp/hn; ssh "$host" hostname >/tmp/hn; wc -c /tmp/hn
6 /tmp/hn

(that's a 5-char hostname in there, redacted!)

查看更多
登录 后发表回答