Hi all,
Please, why "echo" instruction:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")" echo $iphost
display:
g-3.xx.yyy.zz has address 172.16.65.35
instead of:
172.16.65.35
?
Perharps, i have to extract 172.16.65.35 from the variable iphost ?
Thank you so much for help.
Best Regards.
Hello chercheur111,
Could you please try to escape character $
as follows and let me know if this helps.
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print \$4 }'")" echo $iphost
Thanks,
R. Singh
1 Like
Thank you much for help.
Yes, it works fine with:
\$4
Aia
July 23, 2015, 2:44pm
4
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")"
It does not make much sense to connect to $machine
via ssh to query its IP since in order to connect, ssh needs to resolve that IP already.
A side note:
If you have the command dig
, returning the ip address alone would be
dig $machine +short
or if you want the reverse lookup
dig -x 172.16.65.35 +short
chercheur111:
Hi all,
Please, why "echo" instruction:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")" echo $iphost
display:
g-3.xx.yyy.zz has address 172.16.65.35
instead of:
172.16.65.35
?
Perharps, i have to extract 172.16.65.35 from the variable iphost ?
Thank you so much for help.
Best Regards.
This cannot possibly work without a semicolon before the echo statement. To illustrate, replace let's replace the whole command substitution
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")"
with a simple assignment
iphost=foo
$ iphost=foo echo $iphost
$
What is going on here? The assignment to iphost is done local to the echo command. But that is never used by echo, since it does not use this variable. What remains is that it gets passed the result of the variable expansion of $iphost
by the shell, which is the empty string. So in fact an empty line is printed.
So the only way this would produce any output would be if the variable happens to contain a value from a previous assignment, the fields resulting to the variable expansion are then passed to the echo command which then prints it..
So in order to make any of the is work you need to use a semicolon or a newline to separate commands:
iphost=... ; echo "$iphost"
It is best to put double quotes around $iphost
With such a long line i would go for a new line
iphost=...
echo "$iphost"
A good example for the local assignment is
PATH=/bin:/usr/bin basename $0
In fact an environment is set for the command; basename
is searched in /bin and /usr/bin