Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I want to isolate the IP of a certain linux interface using a single command over ifconfig to use it as a parameter in a command.
The following command is working:
ifconfig | grep -A1 docker | grep inet | cut -d ':' -f2 | cut -d ' ' -f1
but I wonder if there is a shorter and better way to do it.
Important: It must work with any european language configuration. For instance, the Spanish output for ifconfig looks like:
docker0 Link encap:Ethernet direcciónHW 02:42:b2:ff:14:5b
Direc. inet:172.17.0.1 Difus.:0.0.0.0 Másc:255.255.0.0
Dirección inet6: fe80::42:b2ff:feff:145b/64 Alcance:Enlace
ACTIVO DIFUSIÓN MULTICAST MTU:1500 Métrica:1
Paquetes RX:12569 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:32629 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:0
Bytes RX:698734 (698.7 KB) TX bytes:46670727 (46.6 MB)
enp4s0 Link encap:Ethernet direcciónHW f0:bf:97:57:17:17
ACTIVO DIFUSIÓN MULTICAST MTU:1500 Métrica:1
Paquetes RX:0 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:0 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:1000
Bytes RX:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupción:18
lo Link encap:Bucle local
Direc. inet:127.0.0.1 Másc:255.0.0.0
Dirección inet6: ::1/128 Alcance:Anfitrión
ACTIVO BUCLE FUNCIONANDO MTU:65536 Métrica:1
Paquetes RX:3681 errores:0 perdidos:0 overruns:0 frame:0
Paquetes TX:3681 errores:0 perdidos:0 overruns:0 carrier:0
colisiones:0 long.colaTX:1000
Bytes RX:343046 (343.0 KB) TX bytes:343046 (343.0 KB)
ifconfig docker |grep -oP 'inet:\K[^ ]+'
172.17.0.1
Using -P flag of grep to enable perl regex and look around.
Using a regex:
ifconfig docker | awk '/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/{print $2}'
If the machine is linux you can use
hostname -i
With single awk process:
ifconfig docker0 | awk '/inet:/{ print substr($2,6); exit }'
substr($2,6) - extracting IP address from the 2nd field starting from the 6th char (i.e. from inet:172.17.0.1 --> 172.17.0.1)
The output will be (in your case):
172.17.0.1
We can use the ip command, whose output is intended to be machine-readable:
ip -o -br -4 addr show dev docker0
We'll want to extract from the third set of spaces until the /24:
ip_addr=$(ip -o -br -4 addr show dev docker0 | tr -s ' /' '\t\t' | cut -f3)
Related
I was trying to get a substring in a bash script however the way I did is not a good solution. I'm parsing the response of "ifconfig" command and trying to get the first network interface name.
result of ifconfig:
eth0 Link encap:Ethernet HWaddr b8:27:eb:6d:a1:92
UP BROADCAST 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:73 errors:0 dropped:0 overruns:0 frame:0
TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7099 (6.9 KiB) TX bytes:7099 (6.9 KiB)
wlan0 Link encap:UNSPEC HWaddr ****
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:792698 errors:0 dropped:792552 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:219274179 (209.1 MiB) TX bytes:0 (0.0 B)
wlan5 Link encap:Ethernet HWaddr ****
inet addr:**** Bcast:**** Mask:****
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:48934 errors:0 dropped:3422 overruns:0 frame:0
TX packets:21217 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14458518 (13.7 MiB) TX bytes:2692948 (2.5 MiB)
getting first interface name which is wlan0
conf=`ifconfig`
net=${conf:670:6}
I didn't understand but position sometimes changes this is why i can't use index 670. Wlan0 can be wlan1,wlan2 and so on... I can't specifically search for wlan0. Any suggestions?
GNU awk
ifconfig | awk -vRS= '!/^(eth0|lo)/{print $1;exit}'
Skips etho and lo blocks and prints the first field of the next, then quits.
At first I thought the question was to get all the wlans and answered this:
$ ifconfig | egrep -A6 '^wlan[0-9]:'
but then it was pointed out that this was GNU, not BSD so I should have said
$ ifconfig | egrep -A6 '^wlan[0-9]'
(no colons). Then it was clarified that only the first one was needed, so perhaps
$ ifconfig | head -n 6
is a better answer?
If there are not exactly 6 lines in any description, then this isn't a very good answer.
Another approach is this:
$ ifconfig eth0 || ifconfig lo0 || ifconfig wlan0 || ifconfig wlan1
and so on. The || means if the first part fails, try the second. You'll get error messages until you hit one that works.
Now here is something better!
$ ifconfig | head -n 1
will give you the first one. Take that line, cut out the first thing on the line, then pass that to ifconfig. This should work on Linux:
$ ifconfig | head -n 1 | awk '{print $1}' | xargs ifconfig
What about something like?
ifconfig | grep -v "^ " "eth0" "lo" | head -1 | cut -c1-6
Have not a Linux pc on which to test it, though.
Basically, i just extract all the lines which do have the name of a net interface, removing all the eth0 and lo stuff, then I get the first one and get only the chars I need
I want to install a webserver on my zolertia z1 sensor. I followed step here : http://wismote.org/doku.php?id=development:sample_code
When i run tunslip program like this :
"sudo ./tunslip -B 115200 -s /dev/ttyUSB0 192.168.1.1 255.255.255.0 "
Results are :
slip started on ``/dev/ttyUSB0''
opened device ``/dev/tun0''
ifconfig tun0 inet `hostname` up
route add -net 192.168.1.0 netmask 255.255.255.0 dev tun0
ifconfig tun0
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:127.0.1.1 P-t-P:127.0.1.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP 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:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
The route on tun0 is opened but he doesn't detect my sensor connected with serial line. There no "route add -net 192.168.1.2 netmask 255.255.255.255 dev tun0" at end and i don't know why. I don't know if i must change the flag for a TAP or TUN device ?!
if i try to login on my sensor with "make login". it works fine. the program is
correctly installed on this.
I tried this on a virtual image with contiki and on Ubuntu 12.04.4 LTS x86_64. I have the same result on both OS.
Maybe you've to change your BaudRate which it's used to be arround 38.400 bauds on motes z1 Zolertia.
sudo ./tunslip -B 38400 -s /dev/ttyUSB0 192.168.1.1 255.255.255.0
i'm trying to get some specific lines of this command:
ifconfig
here is the output of the command:
eth0 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BC
inet addr:10.71.1.30 Bcast:10.71.1.255 Mask:255.255.255.0
inet6 addr: fe80::210:e0ff:fe3f:6fbc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3059275068 errors:0 dropped:1378 overruns:0 frame:0
TX packets:2094779962 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6566542892239 (5.9 TiB) TX bytes:202791652910 (188.8 GiB)
eth1 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BD
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
RX packets:1417584931 errors:0 dropped:32908 overruns:0 frame:0
TX packets:1284691038 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2256566826674 (2.0 TiB) TX bytes:182643225952 (170.0 GiB)
I just want lines that contains "Link" and "bytes" words, for example:
eth0 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BC
RX bytes:6566542892239 (5.9 TiB) TX bytes:202791652910 (188.8 GiB)
eth1 Link encap:Ethernet HWaddr 00:10:E0:3F:6F:BD
RX bytes:2256566826674 (2.0 TiB) TX bytes:182643225952 (170.0 GiB)
Te best approach must be to use grep to filter the lines. For example, use:
ifconfig | egrep " Link|bytes"
Note I added a space before Link to avoid matching the line ending with Scope:Link.
You can also use:
ifconfig | awk '/ Link/ || /bytes/'
or
ifconfig | grep " Link\|bytes"
Of course grep is the best tool for this. But, there are some other ways are available to do the same. That is,
ifconfig | sed -n '/Link \|bytes/p'
and
ifconfig | awk '/Link |bytes/'
ifcofig|perl -lne 'print if(/Link/||/bytes/)'
Here's a couple of useful patterns that most people I've met don't seem to know about. The first is particularly relevant to your problem.
grep -e is your best new-found friend
ifconfig | grep -e Link -e bytes
You can do a similar thing with sed, which has an additional bonus of also printing out the first line (which might contain a heading). In this example, I'm printing out the heading line and any lines contain LISTEN or 'bar' (The second expression is just there to reinforce the fact that you're not limited to one.) Here, the 1p is addressing the first line, (origin-1), with the operation to 'p'rint
lsof -Pni | sed -n -e 1p -e '/LISTEN/p' -e '/bar/p'
I use the following command to disable the eth0 interface's multicast mode , but i not works :
sudo ifconfig eth0 -multicast
when do this, the eth0's configure is so:
ifconfig -v eth0
eth0 Link encap:Ethernet HWaddr 00:16:3E:E8:43:01
inet addr:10.232.67.1 Bcast:10.232.67.255 Mask:255.255.255.0
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:46728751 errors:0 dropped:0 overruns:0 frame:0
TX packets:15981372 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8005709841 (7.4 GiB) TX bytes:3372203819 (3.1 GiB)
then, i do icmp echo_request in host 10.232.67.2:
ping 224.0.0.1
and tcpdump the package on host 10.232.67.1:
tcpdump -i eth0 host 224.0.0.1 -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
21:11:03.182813 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 324, length 64
21:11:04.184667 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 325, length 64
21:11:05.186781 IP 10.232.67.2 > 224.0.0.1: ICMP echo request, id 3639, seq 326, length 64
so, how to disable the multicast mode ?
by the way , when i disable the broadcast:
sudo ifconfig eth0 -broadcast
error message is :
Warning: Interface eth0 still in BROADCAST mode.
so, why can't stop broad cast mode ?
To disable multicast on an interface (you had it right):
ifconfig eth0 -multicast
tcpdump without the -p flag puts the interface in PROMISCUOUS mode, so it captures any traffic on the wire. If you're on a hub, you can see traffic sent to/from everyone else. When an interface isn't in promiscuous mode, it ignores all traffic not sent to it. If you try the tcpdump and ping without promiscuous mode, and multicast disabled, you shouldn't see the traffic:
tcpdump -p -i eth0 host 224.0.0.1 -n
You don't want to disable BROADCAST mode. It just designates the broadcast address for your subnet.
I try to setup a netcat server/client with UDP and IPv6 on the same pc.
Here are my interfaces on my pc:
[root#rh55hp360g7ss7 trunk_dir]# ifconfig
eth0 Link encap:Ethernet HWaddr xxx
inet addr:192.168.255.166 Bcast:192.168.255.255 Mask:255.255.255.0
inet6 addr: fe80::1ec1:deff:fef3:4870/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21948499 errors:0 dropped:0 overruns:0 frame:0
TX packets:24300265 errors:0 dropped:0 overruns:0 carrier:0
collisions:360733 txqueuelen:1000
RX bytes:3645218404 (3.3 GiB) TX bytes:1672728274 (1.5 GiB)
Interrupt:162 Memory:f4000000-f4012800
Then I start the netcat server:
nc -6ul fe80::1ec1:deff:fef3:4870%eth0 5678
And the netcat client (still on the same pc)
nc -6u fe80::1ec1:deff:fef3:4870%eth0 5678
But then, when I type something into the NetCat Client, nothing is transfered to the server.
The same example is working if
I start the netcat client on another pc
I'm using TCP instead of UDP (i.e. when I remove the -u option)
When I'm using IPv4 instead of IPv6 (i.e. when I remove the -6 option and if I take the IPv4 address).
Any Ideas?
TSohr.
Here is the routing table, in case it might help:
[root#rh55hp360g7ss7 trunk_dir]# route -A inet6
Kernel IPv6 routing table
Destination Next Hop Flags Metric Ref Use Iface
fe80::/64 * U 256 0 0 eth0
::1/128 * U 0 265 5 lo
fe80::1ec1:deff:fef3:4870/128 * U 0 10551 1 lo
ff00::/8 * U 256 0 0 eth0
[root#rh55hp360g7ss7 trunk_dir]#
## Added 2012-03-13
With ::1, it is working.
I have the same problem when trying to run a SIP stack on the pc.
It's only a problem with Red Hat and with link-local scope. When using an address with global scope, its working.
I tried out with Ubuntu 10.4, here it is working also with link-local adddresses.
This is my Red Hat Distribution:
[root#BETESIP02 sipp]# uname -a
Linux BETESIP02 2.6.18-194.el5PAE #1 SMP Tue Mar 16 22:00:21 EDT 2010 i686 i686 i386 GNU/Linux