LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2014, 11:05 AM   #1
jjscott
Member
 
Registered: Dec 2012
Posts: 33

Rep: Reputation: Disabled
Shell scripting question


I need to append a line at the bottom of /etc/hosts file to include the current ip address for my pc.

I found the following command that obtains the ip address, but it echo's it to the display and I need to use it as a variable in a shell script in conjunction with the cat command.

Here is the command that echo's the ip address to the display: ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

I am using CentOS 6.5

Thanks
 
Old 03-22-2014, 11:40 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Code:
echo "Hello"
Will echo Hello to stdout.
Code:
 echo "Hello" >> file.txt
Will append Hello to file.txt
 
Old 03-22-2014, 11:46 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You might also want to have a bit more of a look at the awk command as on its own it would be able to complete all the steps where you use grep and cut
 
Old 03-22-2014, 05:21 PM   #4
louigi600
Member
 
Registered: Dec 2013
Location: Italy
Distribution: Slackware
Posts: 635
Blog Entries: 20

Rep: Reputation: 81
Code:
ifconfig  |grep "inet " | grep -v "127\.0\.0\.1" |awk -v host=$(hostname) '{printf("%s %s\n",$2,host)}' >> /etc/hosts
this will append in /etc/hosts an entry with ip address and hostname, which is probabbly what you want anyway.
 
Old 03-22-2014, 05:54 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
regards awk I have seen it described as 'glorified grep'

Building on previous example

Code:
awk -v host=$(hostname) '/inet / && !/127\.0\.0\.1/ {printf("%s %s\n",$2,host)}' < <(/sbin/ifconfig) >> /etc/hosts

Makes little difference here, but consider large complex scripts running on a loaded server and limiting the no. of pipes (and sub shells ) can make all the difference



A great guide for scripting

http://mywiki.wooledge.org/BashGuide/

It highlights many bad habits you can pickup from the net..
 
Old 03-22-2014, 06:38 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
It also depends if you have multiple interfaces. Do you want the internet routable IP? And whether you want the public or private IP of your system.

If you want the public IP.

Code:
curl ifconfig.me
If you want the IP of the interface that has a default route set up for internet then you'd want something like the following command.

Code:
ip -o ro get $(ip ro | awk '$1 == "default" { print $3 }') | awk '{print $5}'
I blogged about it here on LQ.

Last edited by sag47; 03-22-2014 at 07:15 PM.
 
Old 03-23-2014, 05:48 AM   #7
jjscott
Member
 
Registered: Dec 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
I tried both the awk & ifconfig examples below and they both give me what I want except the have a leading "addr:" at the beginning of the output line. Is there anyway to clean that up?

awk -v host=$(hostname) '/inet / && !/127\.0\.0\.1/ {printf("%s %s\n",$2,host)}' < <(/sbin/ifconfig) >> /etc/hosts
ifconfig |grep "inet " | grep -v "127\.0\.0\.1" |awk -v host=$(hostname) '{printf("%s %s\n",$2,host)}' >> /etc/hosts
 
Old 03-23-2014, 07:29 AM   #8
louigi600
Member
 
Registered: Dec 2013
Location: Italy
Distribution: Slackware
Posts: 635
Blog Entries: 20

Rep: Reputation: 81
You should not have any leading "addr" ulness that's an IP address:

Code:
root@r2d2:~# ifconfig |grep "inet " | grep -v "127\.0\.0\.1" |awk -v host=$(hostname) '{printf("%s %s\n",$2,host)}' 
192.168.1.4 r2d2
root@r2d2:~#
If you have more then one interface configured (or alias) you get multiple lines with different IP but same hostname ... you just need to redirect output to /etc/hosts instead of having it shown on standard output.
If you are still getting text in front of the IP address I suspect that your distro is writing something like "inet:192.168.1.4"
so you might haveto work a bit hardet to get the result you want.

Why are you doing this ? do you have a machine that is configuring IP via DHCP and you want to add itself to hosts ?
If so you might want to have an IP reserved on DHCP server so that you get the same one each time and you can do without this thing.

Alternatively you can have the post ip up hook scripts fix the address in hosts for you. This depends on the dhcp client you are using. Look at your client's man page for more details on how you might go about it.

Last edited by louigi600; 03-23-2014 at 07:41 AM.
 
Old 03-23-2014, 07:57 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually the addr: string comes from the output of ifconfig. Anyway, an alternative method is the ip command. Here is an example:
Code:
ip -4 -o address show | awk '$2 !~ /^lo$/{sub(/\/.*/,"",$4); "hostname"|getline h; print $4, h}'
 
1 members found this post helpful.
Old 03-23-2014, 08:27 AM   #10
jjscott
Member
 
Registered: Dec 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks for the help Colucix! That worked perfectly.
 
Old 03-23-2014, 10:23 AM   #11
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Again it depends on what you want. If you want the default interface that has internet access you'll want to account for that.

Code:
ip -o ro get $(ip ro | awk '$1 == "default" { print $3 }') | awk '{print $5,"'$HOSTNAME'"}' >> /etc/hosts
That is the exact command I used in my first post but I added the $HOSTNAME env variable and I'm appending it to /etc/hosts. You should look up the man pages and understand the commands that are being handed to you in this thread. Attempting to filter out the loopback (lo) interface is not very good and you should be using a command similar to what I've given you.

Last edited by sag47; 03-23-2014 at 10:25 AM.
 
1 members found this post helpful.
  


Reply



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
Shell Scripting Question Komelore Programming 2 02-28-2007 07:16 PM
shell scripting question JSLayton Linux - General 4 06-01-2006 06:55 AM
a shell-scripting question: mrchaos Slackware 3 09-22-2005 11:00 AM
Shell Scripting Question b_vasu Linux - Newbie 1 11-21-2003 02:10 PM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 03:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:50 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