Fetch ipaddress and hostname from host file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fetch ipaddress and hostname from host file.
# 1  
Old 01-30-2014
Fetch ipaddress and hostname from host file.

hello guys,

I have a query ,I am looking for a unix command using awk and grep that help me fetching a particular ip address and hostname from the host file.........??????
# 2  
Old 01-30-2014
Hello,

Could you please provide us the input and expected output please for same.


Thanks,
R. Singh
# 3  
Old 01-30-2014
You can do it directly with
grep -i <ip address> /etc/hosts

Not sure why you need to use awk. Please provide more details if you have any specific requirement.
# 4  
Old 01-30-2014
Not sure why you would need -i with an IP address, but you would need it for an alphabetic host name search.

Do you want to get them into variables?

If you are searching for a name, you will need to use the -E flag and give a variety of strings to match, such as:-
  • First on line (IP address); followed by a space
  • First on line (IP address); followed by a tab
  • Preceded by space; last on line
  • Preceded by tab; last on line
  • Preceded by space; followed by a space
  • Preceded by space; followed by a tab
  • Preceded by tab; followed by a space
  • Preceded by tab; followed by a tab
You also need to trim off any comments that might be picked up by mistake too. Consider searching for prod this /etc/hosts file:-
Code:
100.100.100.1        host1      prod          # This is real production
200.200.200.2        host2      pre-prod      # This is not live yet.
300.300.300.3        host3      ex-prd        # This was prod for a bit

Remember that one IP address can have multiple host names associated with it in /etc/hosts. Try:-
Code:
cut -f 1 -d "#" /etc/hosts |  grep -Ei \
"^search-string |^search-string<tab>| search-string$|<tab>search-string$| search-string | search-string<tab>|<tab>search-string |<tab>search-string<tab>" | \
  read my_ip my_host1 my_host2 my_host2 my_host3 my_other_hosts

use the tab character where it shows <tab> rather than use that as a literal string.

The ^ indicates start of line.
The $ indicates end of line.
The | is a logical or between each of the possible searches.


Even then, I might not have considered everything, but if you are looking for information on other servers, it's best to use a name and let the server figure it out through DNS in any case. If you need to talk to a particular application, get a CNAME set up in DNS and refer to that, so that if you move the application, you redirect the CNAME entry and your code need toy change.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting an ipaddress and using it to send files but error at the end of file

i want to extract ip address from a file and using that ip address copy file to systems. set fid set content close $fid ## Split into records on newlines set records send "records splited\n" send $records\n set timeout 600 set a "test\n" send $a foreach rec $records { ##... (1 Reply)
Discussion started by: amature_rach
1 Replies

2. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

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

3. Shell Programming and Scripting

Get the ipaddress and align based on the input file

Hi All, I have a file contains below contents, "interfacename/subnet: public (or) interfacename/subnet:cluster_interconnect" "en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public... (6 Replies)
Discussion started by: kamauv234
6 Replies

4. Shell Programming and Scripting

make the name of file and fetch few things from log file

Hello All, I am working on a script where I need to fetch the value from a log file and log file creates with different name but few thing are common DEV_INFOMGT161_MULTI_PTC_BLD01.Stage_All_to_stp2perf1.042312114644.log STP_12_02_01_00_RC01.Stage_stp-domain_to_stp2perf2.042312041739.log ... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Solaris

Classic: sendmail[pid]: My unqualified host name (hostname) unknown

Hi all, I'd like to disable sendmail permanently on my "new" 220R with freshly installed SunOS 5.10 because it prints annoying messages to the system console. Also I'm interested in how to fix the actual problem with unqualified host name. My unqualified host name (hostname) unknown; sleeping... (2 Replies)
Discussion started by: pseudocoder
2 Replies

6. UNIX for Dummies Questions & Answers

grep specific ipaddress from a file

All, Iam new to unix and i have 1 requirement, can anyone help me please I have provided the file below, i will be having similar files in 100+clients systems. i want to check the the ip address "192.168.208.40" and if it is present then i should get a mail alert, if the ip address is not... (1 Reply)
Discussion started by: tbd
1 Replies

7. 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

8. Shell Programming and Scripting

Fetch lines from a file matching column2 of another file

Hi guys, Please help me out in this problem. I have two files FILE1 abc-23 : 4529675 cde-42 : 9824532 dge-91 : 1245367 gre-45 : 9824532 fgr-76 : 4529675 FILE2 4529675 : Gal Glu house-2-be 9824532 : cat mouse 1245367 : sirf surf-2-beta where FILE2 is a static file with fixed... (5 Replies)
Discussion started by: smriti_shridhar
5 Replies

9. 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

10. 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