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'
Related
I want to display all network interface for a script, including vip.
~]$ /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:90:88:37
inet addr:192.168.1.15 Bcast:192.168.1.31 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9786341 errors:0 dropped:152 overruns:0 frame:0
TX packets:10162602 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6846599961 (6529.4 Mb) TX bytes:2951709145 (2814.9 Mb)
eth1 Link encap:Ethernet HWaddr 00:50:56:90:E1:2E
inet addr:25.10.100.15 Bcast:25.10.100.31 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:42436 errors:0 dropped:150 overruns:0 frame:0
TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5484037 (5.2 Mb) TX bytes:2446 (2.3 Kb)
Like this
eth0 00:50:56:90:88:37 192.168.1.15 MTU:1500
eth0 00:50:56:90:E1:2E 25.10.100.15 MTU:1500
Please help to get this
Thanks in advance
You can use awk and set the record separator to an empty line, for example this
/sbin/ifconfig | awk -v RS='' '{print $14}'
will print
MTU:1500
MTU:1500
so just pick the fields you want.
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)
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
eth0 Link encap:Ethernet HWaddr 06:20:08:46:CD:C7
inet addr:172.26.26.60 Bcast:172.26.26.63 Mask:255.255.255.192
inet6 addr: fe80::420:8ff:fe46:cdc7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1
RX packets:1577 errors:0 dropped:0 overruns:0 frame:0
TX packets:1340 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:141738 (138.4 KiB) TX bytes:215770 (210.7 KiB)
Interrupt:165
eth1 Link encap:Ethernet HWaddr 06:81:8C:00:F4:F2
inet addr:172.26.26.37 Bcast:172.26.26.63 Mask:255.255.255.192
inet6 addr: fe80::481:8cff:fe00:f4f2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:9001 Metric:1
RX packets:269 errors:0 dropped:0 overruns:0 frame:0
TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:16216 (15.8 KiB) TX bytes:8921 (8.7 KiB)
Interrupt:164
the server is a LAMP environment. I use DHCP mode to add a eth1, ifconfig shows as the above. and I can't connect to the eth0 with ip 172.26.26.60 I can only connect to the eth1 with 172.26.26.37 with putty. is set but not working.
I want to know why I can't connect to the 172.26.26.60?
edit:
route -n
it outputs like below:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.26.26.0 0.0.0.0 255.255.255.192 U 0 0 0 eth0
172.26.26.0 0.0.0.0 255.255.255.192 U 0 0 0 eth1
0.0.0.0 172.26.26.1 0.0.0.0 UG 0 0 0 eth1
Check your Ssh server config. It might only be listening on one address. netstat -an | grep 22 | grep LISTEN should show you which address you're listening on.
Step 0 - ping, check cables, etc.
Step 1 - check if sshd is really listening on 0.0.0.0. Otherwise it's only available on the corresponding interface.
Step 2 - check iptables -nvL
Step 3 - check route -n
I need a linux script to get the number of RX packets form an interface when call ifconfig.
EXAMPLE:
this is ifconfig :
eth0 Link encap:Ethernet HWaddr 08:00:27:cc:94:d5
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fecc:94d5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:71460 errors:0 dropped:0 overruns:0 frame:0
TX packets:46999 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:67766944 (67.7 MB) TX bytes:5110629 (5.1 MB)
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:4587 errors:0 dropped:0 overruns:0 frame:0
TX packets:4587 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:471078 (471.0 KB) TX bytes:471078 (471.0 KB)
when calling the script :
./getRXpackets eth0
the output will be : 71460
the same for the other interface
./getRXpackets lo
the output will be : 4587
\K discards the previously characters from printing at the final. \K keeps the text matched so far out of the overall regex match.
Code:
#!/bin/bash
var=$(ifconfig $1 | grep -oP 'RX packets:\K\d+')
echo $var;
Run the above script by,
bash ./script eth0