LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2016, 01:32 AM   #1
shams
Member
 
Registered: Jan 2004
Posts: 535

Rep: Reputation: 30
how to update /etc/hosts ip in dynamic ip?


Hi,
I have 3G wrieless router whith every new connection the ubuntu 16.10 getting a new ip, i also have local web and mail servers, i need to update the ip of /etc/hosts in every new connection, this script was donig work for me in ubuntu 16.04 but it is no more working in ubuntu 16.10:
Code:
#!/bin/bash
# Get the wlp2s0 ip address and updates the /etc/hosts ip in 3ed line.
ip="$(ifconfig | grep -A 1 'wlp2s0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1
echo $ip | sed -i '3s/.*/'$ip' me.example.com' /etc/hosts
The problem is this code didn't get the ip no more in ubuntu 16.10:
Code:
ifconfig | grep -A 1 'wlp2s0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1
This is ifconfig reslut:
Code:
wlp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::1cdd:502e:9475:5480  prefixlen 64  scopeid 0x20<link>
        ether 00:1e:64:f6:65:4e  txqueuelen 1000  (Ethernet)
        RX packets 53946  bytes 71202824 (71.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 45498  bytes 4468984 (4.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
Old 10-12-2016, 02:35 AM   #2
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
ifconfig is deprecated now. Use 'ip' instead.
Code:
ip=$(ip addr ls wlp2s0 | grep '\<inet\>' | cut -d' ' -f6 | cut -d'/' -f1)
sed -i 's/^.\+\(\s\+me.example.com\)/'$ip'\1/g' /etc/hosts

Last edited by c0wb0y; 10-12-2016 at 03:23 AM. Reason: removed space
 
1 members found this post helpful.
Old 10-12-2016, 09:55 AM   #3
shams
Member
 
Registered: Jan 2004
Posts: 535

Original Poster
Rep: Reputation: 30
Thanks for the reply, the first of script working fine, can get the ip address, but the second part:
Code:
sed -i 's/^.\+\(\s\+me.example.com\)/'$ip'\1/g' /etc/hosts
doesn't work.

Last edited by shams; 10-12-2016 at 09:57 AM.
 
Old 10-12-2016, 10:26 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,460

Rep: Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827Reputation: 5827
Your router's DHCP server might have address reservation. If so it will assign the same address based upon your computers MAC.
 
Old 10-12-2016, 11:39 AM   #5
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
Any reason not to use static configuration?
 
Old 10-12-2016, 03:58 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
Quote:
Originally Posted by Emerson View Post
Any reason not to use static configuration?
3G mobile broadband, i presume.
changes IPs all the time.
 
Old 10-12-2016, 10:31 PM   #7
shams
Member
 
Registered: Jan 2004
Posts: 535

Original Poster
Rep: Reputation: 30
The 3G wireless router didn't have any option for static ip.
 
Old 10-12-2016, 10:38 PM   #8
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
You can still assign static IP addresses to your computers.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic ip address of the machine in /etc/hosts RottenMutt Linux - Networking 3 11-24-2006 10:35 AM
Dynamic internet sharing among several hosts fillgood Linux - Networking 5 07-04-2006 06:16 AM
Possible to have a dynamic host entry in hosts file? bbarron Linux - Networking 3 06-28-2006 08:01 PM
Dynamic IP, /etc/hosts and DNS naimslim89 Linux - Networking 3 05-23-2006 04:22 AM
what should be in the /etc/hosts file if I have dynamic ip? Niceman2005 Linux - Networking 2 03-24-2006 09:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration