shell: remote echo hostname and write it locally

≯℡__Kan透↙ 提交于 2020-01-17 15:08:36

问题


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)


回答1:


This should work:

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



回答2:


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!)



来源:https://stackoverflow.com/questions/35085580/shell-remote-echo-hostname-and-write-it-locally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!