Command host with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command host with awk
# 1  
Old 07-23-2015
Command host with awk

Hi all,


Please, why "echo" instruction:
Code:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")" echo $iphost

display:
Code:
  g-3.xx.yyy.zz has address 172.16.65.35

instead of:
Code:
  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.
# 2  
Old 07-23-2015
Hello chercheur111,

Could you please try to escape character $ as follows and let me know if this helps.
Code:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print \$4 }'")" echo $iphost

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 07-23-2015
Thank you much for help.
Yes, it works fine with:
Code:
\$4

# 4  
Old 07-23-2015
Code:
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
Code:
dig $machine +short

or if you want the reverse lookup
Code:
dig -x 172.16.65.35 +short

# 5  
Old 07-23-2015
Quote:
Originally Posted by chercheur111
Hi all,


Please, why "echo" instruction:
Code:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")" echo $iphost

display:
Code:
  g-3.xx.yyy.zz has address 172.16.65.35

instead of:
Code:
  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
Code:
iphost="$(ssh root@$machine -x "host $machine | awk '/has address/ { print $4 }'")"

with a simple assignment
Code:
iphost=foo

Code:
$ 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:
Code:
iphost=...  ; echo "$iphost"

It is best to put double quotes around $iphost

Last edited by Scrutinizer; 07-23-2015 at 07:11 PM..
# 6  
Old 07-24-2015
With such a long line i would go for a new line
Code:
iphost=... 
echo "$iphost"

A good example for the local assignment is
Code:
PATH=/bin:/usr/bin basename $0

In fact an environment is set for the command; basename is searched in /bin and /usr/bin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run awk command on remote host

I have below command to check for error logs from last 24 hours from the file : /var/log/messages/ The command is working fine on the local host. sudo awk -F - -vDT="$(date --date="24 hours ago" "+%b %_d %H:%M:%S")" ' DT < $1' /var/log/messages | egrep -i "error|fail" I want to run the... (8 Replies)
Discussion started by: rahul2662
8 Replies

2. UNIX for Dummies Questions & Answers

What is the command used to list all connections on a host?

give the syntax (3 Replies)
Discussion started by: sonu pandey
3 Replies

3. UNIX for Dummies Questions & Answers

Command for Host Name to IP Address or Vice Versa

Hi, In unix or linux is there any command exist to identify Host Name to IP Address or Vice Versa? Thanks in advance (6 Replies)
Discussion started by: nag_sathi
6 Replies

4. IP Networking

ping can not recognize host but host command can

Hi, I have a weird problem. when ever I do ping command like for example ping unix.comI get the following message: # ping unix.com ping: unknown host unix.com but when I use host the computer is able to know the host. # host unix.com unix.com has address 81.17.242.186 unix.com mail is... (2 Replies)
Discussion started by: programAngel
2 Replies

5. UNIX for Dummies Questions & Answers

Host command

hi guys I have standalone Suse 10 SP3 that when I run # hostname webserver but when I run host command # host webserver ;; connection timed out; no servers could be reached I would like to get the IP when I issue host webserver x.x.x.x and the name when host x.x.x.x... (1 Reply)
Discussion started by: karlochacon
1 Replies

6. Shell Programming and Scripting

command to check whether the remote host is up or not

Hi, My script needs to check whether the remote host is up or not. If it is up i need to start few servers in that host or else just a notification should be sent that the remote host is down? Could someone help me out in this? Regards Arun (4 Replies)
Discussion started by: arunkumarmc
4 Replies

7. Shell Programming and Scripting

Get IP section from host command

How do i get only the IP part from a host call. For example in my script i put #!/bin/sh - # # @(#)daily.local Tim Golding # # Back up server_ip=`host roadhaulage.com` echo $server_ip This echos the following roadhaulage.com has address 86.54.98.194 roadhaulage.com... (5 Replies)
Discussion started by: timgolding
5 Replies

8. Shell Programming and Scripting

ssh to remote host and execute command

Hi, could anyone please tell me how to ssh to remote host foo and execute command on it and print the result on local host? Thanks, Paresh (1 Reply)
Discussion started by: masaniparesh
1 Replies

9. UNIX for Dummies Questions & Answers

Extract time and host IP from 'who' command?

I have been slowly working towards getting a shell window in Cygwin (and/or Gnome and/or KDE) to start up and display the last time it was opened (or sourced) similarly to how the OS X Terminal does so. As close as I've been able to get so far is the 'who' command, but I can't seem to puzzle out... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

10. UNIX for Advanced & Expert Users

using: ssh host command

I will remote a connection to a Unix server and launch a command on this server. This commnand requires me to set a view in Clearcase to see the vob element. How do I set a view an run a command with ssh with the syntax: ssh host command? (3 Replies)
Discussion started by: majo
3 Replies
Login or Register to Ask a Question