HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Server names and its IPs
Operating System - HP-UX
1830939
Members
2844
Online
110017
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2009 04:03 PM
05-11-2009 04:03 PM
Hello,
I have some server names and there are entries related to it in /etc/hosts file.
The /etc/hosts file contains more IPs also.
I need a shell script which can output server names and
its related IPs from /etc/hosts file.
Can someone help me ?
Thanks,
Shiv
I have some server names and there are entries related to it in /etc/hosts file.
The /etc/hosts file contains more IPs also.
I need a shell script which can output server names and
its related IPs from /etc/hosts file.
Can someone help me ?
Thanks,
Shiv
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2009 04:56 PM
05-11-2009 04:56 PM
Solution
I think what oyu meant was that you have a file with just server names and you want to search through /etc/hosts for a match. Although the simplest is a simple grep, you may have hostnames that are part of other hostnames (or no match at all). And you must skip comments in /etc/hosts since they are ignored by networking software. So here is an example:
#!/usr/bin/sh
# Usage: lookup hostname-file
# Returns IP address for a hostname match
set -u
export PATH=/usr/bin
export MYNAME=${0##*/}
function Usage
{
[ $# -gt 0 ] && print "\n$@"
print "Usage: $MYNAME"
exit 1
}
# Main program
[ $# -ne 1 ] && Usage "Requires one filename"
MYFILE=$1
[ -r $MYFILE ] || Usage "$MYFILE not found"
cat $MYFILE | while read HOST JUNK
do
grep -v "^\#" /etc/hosts | while read IP HOSTNAMES
do
if [ $(print "$HOSTNAMES" | grep -wc $HOST) -gt 0 ]
then
print "$HOST = $IP"
fi
done
done
---------------------------------
You can add some other details like reporting on hostnames not found. This script will report all occurrences of the hostname so if /etc/hosts hasa multiple entries, you'll see those too.
Bill Hassell, sysadmin
#!/usr/bin/sh
# Usage: lookup hostname-file
# Returns IP address for a hostname match
set -u
export PATH=/usr/bin
export MYNAME=${0##*/}
function Usage
{
[ $# -gt 0 ] && print "\n$@"
print "Usage: $MYNAME
exit 1
}
# Main program
[ $# -ne 1 ] && Usage "Requires one filename"
MYFILE=$1
[ -r $MYFILE ] || Usage "$MYFILE not found"
cat $MYFILE | while read HOST JUNK
do
grep -v "^\#" /etc/hosts | while read IP HOSTNAMES
do
if [ $(print "$HOSTNAMES" | grep -wc $HOST) -gt 0 ]
then
print "$HOST = $IP"
fi
done
done
---------------------------------
You can add some other details like reporting on hostnames not found. This script will report all occurrences of the hostname so if /etc/hosts hasa multiple entries, you'll see those too.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2009 10:18 AM
05-12-2009 10:18 AM
Re: Server names and its IPs
Hi Shiv:
As Bill noted, the simplest solution may be to use 'grep's options. Assuming, as he said, that you have a file of server names and you want to match those in '/etc/hosts' you can do:
# grep -f /tmp/myhosts /etc/hosts|grep -v "^#"
...to find tokens in 'myhosts' that exist in '/etc/hosts' and skip any comment lines.
# grep -v -f /tmp/hosts /etc/hosts|grep -v "^#"
...to find tokens in 'myhosts' that do _NOT_ exist in '/etc/hosts'.
Regards!
...JRF...
As Bill noted, the simplest solution may be to use 'grep's options. Assuming, as he said, that you have a file of server names and you want to match those in '/etc/hosts' you can do:
# grep -f /tmp/myhosts /etc/hosts|grep -v "^#"
...to find tokens in 'myhosts' that exist in '/etc/hosts' and skip any comment lines.
# grep -v -f /tmp/hosts /etc/hosts|grep -v "^#"
...to find tokens in 'myhosts' that do _NOT_ exist in '/etc/hosts'.
Regards!
...JRF...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP