问题
Part 1) I try to execute an echo hostname
on my remote host (using ssh).
just the hostname
command 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