Declare and grep a variable via ssh/remote/loop

If I am running a bash command, and some awk getting the ethernet adapter on the local machine. It works fine. But if I will run it from the remote, it is EMPTY on echo and throwing error in grep.
Thank you

This work perfectly fine

$ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip a| grep $f

6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    inet 127.127.127.127/32 brd 127.127.127.127 scope host br0

$ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip a| grep $f|grep scope
    inet 127.127.127.127/32 brd 127.127.127.127 scope host br0

$ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip a| grep $f|grep scope
    inet 127.127.127.127/32 brd 127.127.127.127 scope host br0
$

Now from my remote machine:


$ for i in REMOTEserver; do  ssh $i " f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; echo $f "  ; done


$ for i in REMOTEserver; do  ssh $i " f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; echo \$f "  ; done

$ for i in REMOTEserver; do  ssh $i " f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; echo $f "  ; done


$ for i in REMOTEserver; do  ssh $i " f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip a|grep  $f "  ; done
bash: ip: command not found
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

$


------ Post updated at 02:23 PM ------

Please close this, i figured it out.
Thank you

It is nice to hear that you have solved your problem.

Please show us what you did so we can all learn from your research!

for i in `my remote servers|sort`; do echo -n $i "";ssh $i " f=\`/sbin/ip a|egrep '127.127|128.128' |head -1| awk '{print \$NF }' \` ; echo \$f    "  ; done

------ Post updated at 02:29 PM ------

I added \ on "`" , without it, I can't declare a variable to a remote server.
Thank you

1 Like