- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Populate /etc/hosts from nslookup of hostname
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
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
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- 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-21-2011 01:15 PM
тАО05-21-2011 01:15 PM
Populate /etc/hosts from nslookup of hostname
I am thinking that I have seen a script on this forum which populates /etc/hosts from nslookup output. I am unable to find it.
Thanks,
Allan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2011 02:22 PM
тАО05-21-2011 02:22 PM
Re: Populate /etc/hosts from nslookup of hostname
but here's a start with no sanity checking:
#
###
### IP to Name ###
###
IP=$1
if [ "$IP" = "" ] ; then
echo "usage: $0
exit 1
fi
NAME=$(nslookup $IP | grep Name: | awk '{print $2}')
if [ "$NAME" = "" ] ; then
echo IP $IP not found >&2
exit 2
else
echo "$IP $NAME"
fi
You could this in a number of ways.
## ip_to_name 172.16.0.12
172.16.0.12 manny.fepu.com
## ip_to_name 172.16.0.12 >> /tmp/new_hosts_list
Say /tmp/iplist has list of ip addresses to convert
## ( > /tmp/new_hosts_list ; > /tmp/error
cat /tmp/iplist \
|while read IP
do
ip_to_name $IP >> /tmp/new_hosts_list 2>>/tmp/error
done
)
bv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-21-2011 02:31 PM
тАО05-21-2011 02:31 PM
Re: Populate /etc/hosts from nslookup of hostname
This would be simple enough:
# nslookup www.firefox.com|awk '{if (/^Name/) {N=$2;getline;A=$2;print A,N}}' >> /etc/hosts
This will handle multiple addresses, too, as for 'www.google.com'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2011 07:21 AM
тАО05-22-2011 07:21 AM
Re: Populate /etc/hosts from nslookup of hostname
I don't know why I assumed that the input was an IP address !?!?
Brain fart, I guess.
Better would be:
#
H=$1
if [ "$H" = "" ] ; then
echo "usage: $0 {host_name | IP_address}"
exit 1
fi
NAME=$(nslookup $H | grep Name: | awk '{print $2}')
IP=$(nslookup $H | grep Address: | awk '{print $2}')
if [ "$NAME" = "" -o "$IP" = "" ] ; then
echo Host/IP $H not found >&2
exit 2
else
echo "$IP $NAME"
fi
bv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2011 08:06 AM
тАО05-22-2011 08:06 AM
Re: Populate /etc/hosts from nslookup of hostname
Another thing would be what if the host is already there with the same IP address so I want to avoid populating the hosts file then.
and also delete the same hosts if the IP address changes for it, how can I tackle that so as to avoid having two or more entries for the same host even though the IPs are different.
Thanks,
Allan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2011 08:18 AM
тАО05-22-2011 08:18 AM
Re: Populate /etc/hosts from nslookup of hostname
> Another thing would be what if the host is already there with the same IP address so I want to avoid populating the hosts file then.
> and also delete the same hosts if the IP address changes for it, how can I tackle that so as to avoid having two or more entries for the same host even though the IPs are different.
Why do you want to maintain a local '/etc/hosts' file in the first place when DNS exists?
Further, if you would run 'nslookup' with a target like 'www.google.com' you would be rewarded with multiple addresses. What would you do in that case, since they are all valid and will be returned in a rotating order for each query.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2011 06:47 PM
тАО05-22-2011 06:47 PM
Re: Populate /etc/hosts from nslookup of hostname
Thanks,
Allan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-23-2011 05:01 AM
тАО05-23-2011 05:01 AM
Re: Populate /etc/hosts from nslookup of hostname
> Hi JRF, one of the programs times out on dns resolution, so I want to use /etc/hosts file.
I would suggest you find the reason for that program's behavior rather than kludge up a local view of what rightly belongs in DNS. I guarantee that once you rely on a local hosts file, one day the IP address of a host will change and someone (you?) will wonder why the program can't reach its target.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 05:22 AM
тАО06-04-2011 05:22 AM
Re: Populate /etc/hosts from nslookup of hostname
Were you satisfied with the answers your received?
...JRF...