Quote:
Originally Posted by genderbender
ssh root@ipaddress "for i in `cat /etc/hosts | awk '{print $2}'`; do ping $i; done;"
|
Often you want single quotes around the whole thing, so that there is no parameter expansion before it gets passed to the remote machine by
ssh, otherwise you would need to escape the
$ characters.
Code:
ssh root@ipaddress 'for i in $(cat /etc/hosts | awk '\''{print $2}'\''); do ping $i; done'
When you need a single quote inside the statement, you have to exit the quotes, escape the single quote, and then reenter the quotes (hence the
'\'' sequences). The
$(...) is an alternative to the backquotes which can sometimes be more readable.
Incidentally, you will also want to limit the number of pings or it will never finish, and sometimes the
/etc/hosts file has lines that might confuse this script.