I need to Obtain my remote Linux Server eth1 IP address using bash script through the ssh and i have following command: /sbin/ifconfig eth1 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}'

but when i run :

spawn ssh -t $user@$remote_host "/sbin/ifconfig eth1 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $2}' ; echo $ip " it says can't read "1": no such variable while executing "spawn ssh -t $user@$remote_host "ip=/sbin/ifconfig eth1 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}' ; echo $ip "" (file "./ssh2.sh" line 11)

So could please help me with this issue. Thanks

  • can you create a script in the remote server?
    – Brigo
    Apr 15, 2013 at 18:36
  • You have at least one typo in there, as one of your "$1"s has changed to a "$2". Probably that's all that changed, but it would be best to double- (and then triple-)check to make sure there aren't any other typos.. Apr 15, 2013 at 18:39
  • Yes i can create. Apr 15, 2013 at 18:40
  • if you do: ssh $user@$remote_host /sbin/ifconfig eth1 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}' you get a good reply.
    – Brigo
    Apr 15, 2013 at 18:40
  • 2
    If you dont want promp for password use key authentication.
    – Brigo
    Apr 15, 2013 at 19:41

1 Answer 1


Why not use ip instead of the deprecated ifconfig, and skip all the extra greps and cuts since awk can do that.

So a command like ip addr show dev eth0 | awk '/^ +inet / {split($2,a,"/"); print a[1]}'

In any case, most of your problem in your example commands have to do with the type of quotes you choose, and how bash evaluates them.

If you do something like cfrancy@ws:~$ ssh root@srv01 echo "Hello $USER" then the output you will see is Hello cfrancy, and not Hello root like you might expect. Because you have used double quotes " around your command for the remote system, all the variables are evaluated before the command is sent. The command cfrancy@ws:~$ ssh root@srv01 echo 'Hello $USER' will return Hello root since the $USER is evaluated on the remote site. Of course you also need to switch around or escape your other quotes inside your compound command so things work out properly.

Anyway, keeping the quoting behavior in mind the command would probably do what you want. though I think my solution that uses only ip, and awk is a better choice since I am only useing 2 programs instead of 4.

ssh -t $user@$remote_host '/sbin/ifconfig eth0 | grep "inet addr:"| grep -v "" | cut -d: -f2 | awk "{ print $1}"'
  • I'm holding out for them to figure out ip was a more annoyance than help and revert.
    – Scott Pack
    Apr 16, 2013 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .