Script Shell Extracting hostname


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Shell Extracting hostname
# 1  
Old 07-22-2015
Script Shell Extracting hostname

Hi all,
Please i have to get hostname of an ip address (172.16.68.4). so i have used the command 'host'

Code:
host $ipaddr  >> iptohostname.txt

Here is my file: iptohostname.txt
Code:
4.68.16.172.in-addr.arpa domain name pointer g-4.xxx.yyyy.zz.
3.68.16.172.in-addr.arpa domain name pointer g-3.xxx.yyyy.zz.

Please, how can i extract the hostname ?
i try with this script
Code:
    hostname=$(grep -e $ipaddr "iptohostname.txt" | awk '{ print $1; }')
    echo "here"
    echo $hostname

but, it display nothing.

Thanks a lot for help.
Best Regards.
# 2  
Old 07-22-2015
If you have ipaddr as 4.68.16.172 and trying to fetch as below:-
Code:
4.68.16.172.in-addr.arpa

Then use:-
Code:
cat iptohostname.txt | grep "$ipaddr" | awk '{ print $1}'

Thanks

Last edited by rbatte1; 07-24-2015 at 06:12 AM.. Reason: Added CODE tags
# 3  
Old 07-22-2015
The IP address is reversed in the file !
i try to fetch "172.16.68.4"
# 4  
Old 07-22-2015
How about
Code:
host 72.21.206.6 | awk '{print $NF}'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-22-2015
Thank you so much for help.
Code:
host 72.21.206.6 | awk '{print $NF}'
206-6.amazon.com.

Please, how can i delete the last point ? i'd like only the hostname.

Thank you so much for help.
Kind regards.
# 6  
Old 07-22-2015
Code:
host 72.21.206.6 | awk '{print substr($NF,1,length($NF)-1)}'

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 07-23-2015
Thank you so much for help.
Kind Regards.

---------- Post updated at 05:55 AM ---------- Previous update was at 04:50 AM ----------

Hi,

Please, i have another question in the same context: how can i get the hostname of the machine i work on ?

Here is my script that i try to complete:
Code:
    hostname=$(host $ipaddr | awk '{print substr($NF,1,length($NF)-1)}')
    echo $hostname
    $h= get the current hostname
    if [ $hostname -ne $h ]
    then
    //instructions
    fi

Thank you so much.
Best regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

2. Shell Programming and Scripting

Ping the hostname of many servers using a script

Hi We have some 300 servers in the Data center and some of them are running with AIX and some of them are running with Solaris. I need a script which can be run in one of the server and that script should ping the hostname of all the 300 servers. Also the script should notify if any server is... (9 Replies)
Discussion started by: newtoaixos
9 Replies

3. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

4. Shell Programming and Scripting

Extracting data from https server with the help of unix shell script

There is a folder which can be accessed through URL by giving a particular Username and Password.Inside the folder there are few excel sheets.The excel sheets/folder need to be imported from there to unix box with the help of unix shell script. Can anyone help me?Does anyone have code for it?... (2 Replies)
Discussion started by: vanur
2 Replies

5. Shell Programming and Scripting

extracting function headers in a c/c++ file using shell script

Hi, Is there any way to extract function headers from c and c++ files using a shell script? I tried to do it by reading the C/C++ file line by line and if a line matches a particular pattern (pattern of function header) i extracted it otherwise moved to next line. The problem here is, some... (3 Replies)
Discussion started by: priyadarshini
3 Replies

6. Shell Programming and Scripting

fetch hostname and instance name using shell script

Hi All, Requirement is to fetch hostname and instance name using shell script from all configuration files on a server R12 on IBM AIX... could anyone please share such an experience encountered before.Is there such a script available in this forum or any other site.. Thanks for your time!... (0 Replies)
Discussion started by: a1_win
0 Replies

7. Shell Programming and Scripting

shell script for extracting out the shortest substring from the given starting and en

hi all, i need an urgent help for writing a shell script which will extract out and print a substring which is the shortest substring from the given string where first and last character of that substring will be given by the user. for e.g. if str="abcdpqracdpqaserd" now if the user gives 'a'... (18 Replies)
Discussion started by: pankajd
18 Replies

8. Shell Programming and Scripting

urgent-extracting block data from flat file using shell script

Hi, I want to extract block of data from flat file. the data will be like this start of log One two three end of log i want all data between start of log to end of log i.e One two three to be copied to another file. This particular block may appear multiple times in same file. I... (4 Replies)
Discussion started by: shirish_cd
4 Replies

9. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies
Login or Register to Ask a Question