Help with ifconfig shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with ifconfig shell script
# 1  
Old 03-15-2017
Ubuntu Help with ifconfig shell script

I am new to shell scripting and trying to create a shell script which will extract and print only ip address from ifconfig command and not all the information. I have two issues here -
1. To know active interface like eth0, wl0, etc.
- I guess `route` command will give me interface name in last column.
So I wrote below code -

Code:
info=`route`
echo $info

route command gives output as -
Code:
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    600    0        0 wlp3s0
link-local      *               255.255.0.0     U     1000   0        0 wlp3s0
192.168.0.0     *               255.255.255.0   U     600    0        0 wlp3s0

Now I am not sure how to extract wlp3s0 from this output.
2. when I am able to get this wlp3s0 from above output, I will run -
ifconfig wlp3s0

which will give me below output -
Code:
wlp3s0    Link encap:Ethernet  HWaddr 50:b7:c3:d9:b9:df  
          inet addr:192.168.0.9  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a5d8:9c7b:c23f:116/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:58167 errors:0 dropped:0 overruns:0 frame:0
          TX packets:44103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:42208037 (42.2 MB)  TX bytes:8352741 (8.3 MB)

Again I would want to extract 192.168.0.9 from this output and print it.

Any help would be greatly appreciated.


Moderator's Comments:
Mod Comment Please use CODE - not QUOTE - tags as required by forum rules!

Last edited by RudiC; 03-15-2017 at 03:50 PM.. Reason: Changed QUOTE to CODE tags.
# 2  
Old 03-15-2017
How about
Code:
ifconfig | awk '$1 != "lo" {print $7}' RS=
addr:10.1.1.1

or
Code:
ifconfig | awk '$1 != "lo" {print $31}' RS= FS="[ :]"
10.1.1.1

Do you have the ip command?
Code:
ip -o -4 addr
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: eth0    inet 10.1.1.1/24 brd 10.1.1.255 scope global eth0\       valid_lft forever preferred_lft forever

# 3  
Old 03-17-2017
very close

Thank you very much for the precise code. The best I could make using your code is as below -

ip -o -4 addr | awk '{print $2 " " $4}'

it gives output as -

HTML Code:
lo 127.0.0.1/8
enp2s0 172.29.100.54/24
virbr0 192.168.122.1/24
Do you know how can I remove /24 from the output so that it would look like below -
HTML Code:
lo 127.0.0.1/8
enp2s0 172.29.100.54
virbr0 192.168.122.1
Thanks,
Amar
# 4  
Old 03-17-2017
Try
Code:
ip -o -4 addr | awk '{sub (/\/.*$/, _, $4); print $2, $4}'
lo 127.0.0.1
eth0 10.1.1.1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

not able to run ifconfig

Unable to run the ifconfig in my home dir.. I did add adding /sbin and /usr/sbin in th PATH But still unable to get that .. Am using red hat linux cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.3 (Tikanga) (3 Replies)
Discussion started by: mail2sant
3 Replies

2. Linux

ifconfig - information

Hi All, I need some info on ifconfig. ifconfig is available in Linux and Solaris. However the options available in Solaris is not there in Linux. 1. ifconfig -a == output is different in both OS. I am somewhat able to compare the output. But one thing I am not getting is after the flags... (2 Replies)
Discussion started by: ahamed101
2 Replies

3. Shell Programming and Scripting

How do I set up a shell script using ifconfig?

I am running on AIX 5.3. I have a remote AIX server running on a generator. Many times the generator goes out and I only have a window of 15 mins with the network up and 30 mins the server is powered. I need help creating a script using ifconfig, where it goes out and checks the network every 5... (2 Replies)
Discussion started by: AIX25
2 Replies

4. Shell Programming and Scripting

script to get IP address from ifconfig

I'm a newbie to linux, so pleases forgive me! I would like to find a script that I could use to extract a dynamic ip from the ppp0 and set it as a variable to be used in IPTables basically, if ppp0 ip address is 10.10.10.20, I would like a script that would set a var to 10.10.10.0. of... (9 Replies)
Discussion started by: speedy3k
9 Replies

5. IP Networking

ifconfig modification

Hi all,,i have problem with my homework form my lecture.. He is give me a homework to create: ifconfig eth0 0.255.255.255 1.1.168.192 my lecture says, i can making the shell scripting to finish my homework.. i have question, can setting ifconfig like in the upper? (1 Reply)
Discussion started by: demhyt
1 Replies

6. Solaris

ifconfig -a not working

Hi All, I logged into a sun microsystems server box, using both a vnc session and a ssh session. I am able to use "ifconfig -a" in a ssh session, but the same on a vnc session says that its a invalid command. how is this possible, when i am logging into the same box through two different... (4 Replies)
Discussion started by: pardhuhere
4 Replies

7. UNIX for Dummies Questions & Answers

ifconfig and ipconfig

hey, I know that ifconfig on linux systems is identical to win ipconfig. I wanted to know if there is a linux distribution (new or old) that uses the ipconfig command. thanks alot :) (2 Replies)
Discussion started by: tsmotix
2 Replies

8. Shell Programming and Scripting

ifconfig!!!!

why the ifconfig command is not working in my machine? it says "-bash: ifconfig: command not found" why its says that? actually i m looking for "how can I know the Network Interface Card physical address?" Requesting u all for help. thanks (3 Replies)
Discussion started by: moco
3 Replies

9. OS X (Apple)

ifconfig

Hi, I type (as root) "ifconfig en1 ether aa:aa: " but it doesnt change the mac of my airport. Can anyone help me? Thx. (0 Replies)
Discussion started by: Jariya
0 Replies

10. UNIX for Dummies Questions & Answers

ifconfig

I am trying to change an IP address on a machine running HPUX10 After I change it I can ping it from the outside but it completely locks the console. After a reboot it returns back to its previous IP. Any ideas?? Thanks Brian (7 Replies)
Discussion started by: breigner
7 Replies
Login or Register to Ask a Question