0

This is the standard Linux ifconfig command

user@linux:~$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:00:00:00:00:10  
          inet addr:192.168.1.1  Bcast:192.168.56.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:112 errors:0 dropped:0 overruns:0 frame:0
          TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:14616 (14.2 KiB)  TX bytes:17776 (17.3 KiB)

eth1      Link encap:Ethernet  HWaddr 00:00:00:00:00:11
          inet addr:10.0.1.1  Bcast:10.0.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

user@linux:~$

And this is to print only IP Address from ifconfig

user@linux:~$ cat script.sh
ifconfig | grep ad.*Bc | cut -d: -f2 | awk '{ print $1}'
user@linux:~$ 

The output looks like this

user@linux:~$ ./script.sh
192.168.1.1
10.0.1.1
user@linux:~$

What I'm trying to accomplish is to print the output of the IP Address in 1 line ... something like this.

user@linux:~$ ./script2.sh
192.168.1.1 10.0.1.1
user@linux:~$

Is it possible with ifconfig? If yes, I would appreciate if you could share the trick. Thanks

Charlotte Russell
  • 1,355
  • 1
  • 13
  • 16
  • See this https://stackoverflow.com/questions/23934425/parse-ifconfig-to-get-only-my-ip-address-using-bash?rq=1 – Manish R Jun 14 '17 at 13:47
  • @manishr, that was different. That was to print the output of the IP Address only (default is to print each IP Address in new line, not in 1 line). Btw, I've already provided my answer on that link too. You might want to check that link again. Thanks What I'm trying to accomplish here is to print the output of the IP Address in 1 line and zhenguoli has already provided the accurate answer – Charlotte Russell Jun 14 '17 at 14:39

3 Answers3

1

If you have hostname,

hostname -I

Test:

$ hostname -I
192.168.2.253 192.168.0.179
sat
  • 14,589
  • 7
  • 46
  • 65
  • -I (capital) is not supported in this box user@BusyBox:~$ hostname -I hostname: invalid option -- 'I' BusyBox v1.24.2 (2016-05-16 13:46:43 UTC) multi-call binary. Usage: hostname [OPTIONS] [HOSTNAME | -F FILE] Get or set hostname or DNS domain name -s Short -i Addresses for the hostname -d DNS domain name -f Fully qualified domain name -F FILE Use FILE's content as hostname user@BusyBox:~$ – Charlotte Russell Jun 14 '17 at 13:49
0
ifconfig | grep ad.*Bc | cut -d: -f2 | awk '{ print $1}' | { tr '\n' ' '; echo; }

Your code is almost functional. Just convert the newline to space and add a more newline.

zhenguoli
  • 2,268
  • 1
  • 14
  • 31
0
ifconfig | awk '{match($0,/inet addr:([^ ]+)/,a);x=x FS a[1]}END {print x} '
  142.133.152.191         127.0.0.1

Or using grep:

ifconfig | grep -oP 'inet addr:\K[^ ]+' | xargs
142.133.152.191 127.0.0.1
P....
  • 17,421
  • 2
  • 32
  • 52