Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option -i for ips to display interface(s) #1004

Closed
wants to merge 1 commit into from

Conversation

YumingMa
Copy link
Contributor

@YumingMa YumingMa commented Jul 14, 2017

I have made a lot of test with the files in the attachment. Please review the code.

Edit (nwinkler): Removed zip attachment

Copy link
Member

@nwinkler nwinkler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the review comments.

For the tests: Call me paranoid, but I'm not going to download a random zip file that someone I don't know has added to a ticket 😄

Please take a look at the test/plugins/base.plugin.bats file - there is an existing test for the ips function. Please add additional tests that cover the new functionality there, so they are run on every commit (using Travis CI).

@@ -4,13 +4,24 @@ about-plugin 'miscellaneous tools'
function ips ()
{
about 'display all ip addresses for this host'
param 'optional -i'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide an explanation for what this parameter does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I write "param 'optional -i , display the interfaces and the corresponding ip addresses ' "? or other way to do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write something like param '-i: (optional) display the interfaces and the corresponding ip addresses

group 'base'
if command -v ifconfig &>/dev/null
then
ifconfig | awk '/inet /{ gsub(/addr:/, ""); print $2 }'
if [ -n "$1" -a "$1" = "-i" ];then
ifconfig | grep -B 1 -E "inet "| sed 's/addr://g' | sed 's/: / /g' | awk '{if (NR%3==1) print $1 ":"; else if (NR%3==2) print $2}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really have to run grep, sed (twice) and awk all in the same line? I'm sure there's got to be a way that does not use three different tools to accomplish this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beacause running the ifconfig command get different results on different OS.
on Centos:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5302709 errors:0 dropped:0 overruns:0 frame:0
TX packets:5302709 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:253434162 (241.6 MiB) TX bytes:253434162 (241.6 MiB)
On Ubuntu:
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 390 bytes 29016 (29.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 390 bytes 29016 (29.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand that, but ideally you don't need to run three different tools to parse the output. What you're doing with grep/sed/awk can probably be done with just awk alone.

elif command -v ip &>/dev/null
then
ip addr | grep -oP 'inet \K[\d.]+'
if [ -n "$1" -a "$1" = "-i" ];then
ip addr | grep -E '^\s*[1-9]| (25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{1,2})\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{1,2})\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{1,2})' | awk '{print $2}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, but I don't even want to try to understand what this line does 😄

Can you please provide some explanation what this does? Ideally in a code comment...

Anyway, this looks far too complex.

Copy link
Contributor Author

@YumingMa YumingMa Jul 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will added a code comment.
ip addr --- running ip addr to display all interfaces and ip addresses on the device.
the result:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 3.3.3.29/24 brd 3.3.3.255 scope global lo:1
inet 3.3.3.30/24 brd 3.3.3.255 scope global secondary lo:2
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
21: gre0: mtu 1476 qdisc noop state DOWN
link/gre 2.2.2.2 brd 2.2.2.1

grep command --- filter line 1 and 3,4,5 in lo device list. in gre0 device list, filter line 1 and 2.
awk command --- print lo: or ip addresses. print gre0: or ip addresses

the result of the ips -i:
lo:
127.0.0.1/8
3.3.3.29/24
3.3.3.30/24
gre0:
2.2.2.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but there has got to be a simpler way for doing that...

@YumingMa
Copy link
Contributor Author

YumingMa commented Jul 18, 2017

I don't want to mege this PR. beacause these two commands are so dependent on the OS. The code is difficult to cover all cases. the below comments are my cases.

@YumingMa
Copy link
Contributor Author

the result of running "ifconfig" on CentOS:
eth0 Link encap:Ethernet HWaddr 00:0C:29:8B:5F:2F
inet addr:10.10.0.54 Bcast:10.10.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8b:5f2f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21332922 errors:0 dropped:0 overruns:0 frame:0
TX packets:4651204 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2490502766 (2.3 GiB) TX bytes:1458751886 (1.3 GiB)

eth1 Link encap:Ethernet HWaddr 00:0C:29:8B:5F:39
inet addr:12.12.12.1 Bcast:12.12.12.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8b:5f39/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10924782 errors:0 dropped:0 overruns:0 frame:0
TX packets:9648544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2826373567 (2.6 GiB) TX bytes:3626977407 (3.3 GiB)

eth2 Link encap:Ethernet HWaddr 00:0C:29:8B:5F:4D
inet addr:192.172.15.1 Bcast:192.172.15.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8b:5f4d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2508744 errors:0 dropped:0 overruns:0 frame:0
TX packets:3140759 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1954971282 (1.8 GiB) TX bytes:1513674181 (1.4 GiB)

eth3 Link encap:Ethernet HWaddr 00:0C:29:8B:5F:43
inet addr:13.13.13.1 Bcast:13.13.13.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8b:5f43/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8801251 errors:0 dropped:0 overruns:0 frame:0
TX packets:8974794 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2533772506 (2.3 GiB) TX bytes:2796157298 (2.6 GiB)

gre2 Link encap:UNSPEC HWaddr 02-02-02-02-FF-FF-E0-E6-00-00-00-00-00-00-00-00
inet addr:100.10.10.2 P-t-P:100.10.10.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MTU:1476 Metric:1
RX packets:4045 errors:0 dropped:0 overruns:0 frame:0
TX packets:4045 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:339780 (331.8 KiB) TX bytes:355960 (347.6 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:5302709 errors:0 dropped:0 overruns:0 frame:0
TX packets:5302709 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:253434162 (241.6 MiB) TX bytes:253434162 (241.6 MiB)

lo:1 Link encap:Local Loopback
inet addr:3.3.3.29 Mask:255.255.255.0
UP LOOPBACK RUNNING MTU:16436 Metric:1

lo:2 Link encap:Local Loopback
inet addr:3.3.3.30 Mask:255.255.255.0
UP LOOPBACK RUNNING MTU:16436 Metric:1

tun_10_45 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:1.1.1.1 P-t-P:1.1.1.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:4412849 errors:0 dropped:0 overruns:0 frame:0
TX packets:3104433 errors:0 dropped:1864 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:429789680 (409.8 MiB) TX bytes:1431382085 (1.3 GiB)

tun_10_47 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:2.2.2.1 P-t-P:2.2.2.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:2034172 errors:0 dropped:0 overruns:0 frame:0
TX packets:3548421 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:167736430 (159.9 MiB) TX bytes:266262913 (253.9 MiB)

@YumingMa
Copy link
Contributor Author

the result of running "ifconfig" on Ubuntu:
enp8s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.1.4.126 netmask 255.255.255.0 broadcast 10.1.4.255
inet6 fe80::845f:b85f:5b3a:152b prefixlen 64 scopeid 0x20
ether 08:9e:01:9d:06:20 txqueuelen 1000 (Ethernet)
RX packets 6716151 bytes 8750880968 (8.7 GB)
RX errors 192 dropped 0 overruns 192 frame 0
TX packets 1354429 bytes 1090914404 (1.0 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 17

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 119607 bytes 1643578613 (1.6 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 119607 bytes 1643578613 (1.6 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vmnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
inet6 fe80::250:56ff:fec0:1 prefixlen 64 scopeid 0x20
ether 00:50:56:c0:00:01 txqueuelen 1000 (Ethernet)
RX packets 370 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1388 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vmnet8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.36.1 netmask 255.255.255.0 broadcast 172.16.36.255
inet6 fe80::250:56ff:fec0:8 prefixlen 64 scopeid 0x20
ether 00:50:56:c0:00:08 txqueuelen 1000 (Ethernet)
RX packets 3469 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1110 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlp7s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether f6:4e:c4:29:04:c4 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

@YumingMa
Copy link
Contributor Author

the result of running "ip addr" on CentOS:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 3.3.3.29/24 brd 3.3.3.255 scope global lo:1
inet 3.3.3.30/24 brd 3.3.3.255 scope global secondary lo:2
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:0c:29:8b:5f:39 brd ff:ff:ff:ff:ff:ff
inet 12.12.12.1/24 brd 12.12.12.255 scope global eth1
inet6 fe80::20c:29ff:fe8b:5f39/64 scope link
valid_lft forever preferred_lft forever
3: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:0c:29:8b:5f:43 brd ff:ff:ff:ff:ff:ff
inet 13.13.13.1/24 brd 13.13.13.255 scope global eth3
inet6 fe80::20c:29ff:fe8b:5f43/64 scope link
valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:0c:29:8b:5f:4d brd ff:ff:ff:ff:ff:ff
inet 192.172.15.1/24 brd 192.172.15.255 scope global eth2
inet6 fe80::20c:29ff:fe8b:5f4d/64 scope link
valid_lft forever preferred_lft forever
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:8b:5f:2f brd ff:ff:ff:ff:ff:ff
inet 10.10.0.54/24 brd 10.10.0.255 scope global eth0
inet6 fe80::20c:29ff:fe8b:5f2f/64 scope link
valid_lft forever preferred_lft forever
21: gre0: mtu 1476 qdisc noop state DOWN
link/gre 2.2.2.2 brd 2.2.2.1
22: gretap0: <BROADCAST,MULTICAST> mtu 1476 qdisc noop state DOWN qlen 1000
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
23: gre2@NONE: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1476 qdisc noqueue state UNKNOWN
link/gre 2.2.2.2 peer 2.2.2.1
inet 100.10.10.2 peer 100.10.10.1/32 scope global gre2
24: tun_10_45: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
link/[65534]
inet 1.1.1.1 peer 1.1.1.2/32 scope global tun_10_45
26: tun_10_47: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
link/[65534]
inet 2.2.2.1 peer 2.2.2.2/32 scope global tun_10_47

@YumingMa
Copy link
Contributor Author

the result of running "ip addr" on Ubuntu:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp8s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 08:9e:01:9d:06:20 brd ff:ff:ff:ff:ff:ff
inet 10.1.4.126/24 brd 10.1.4.255 scope global dynamic enp8s0
valid_lft 35682sec preferred_lft 35682sec
inet6 fe80::845f:b85f:5b3a:152b/64 scope link
valid_lft forever preferred_lft forever
3: wlp7s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
link/ether f6:4e:c4:29:04:c4 brd ff:ff:ff:ff:ff:ff
4: vmnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:50:56:c0:00:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global vmnet1
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fec0:1/64 scope link
valid_lft forever preferred_lft forever
5: vmnet8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:50:56:c0:00:08 brd ff:ff:ff:ff:ff:ff
inet 172.16.36.1/24 brd 172.16.36.255 scope global vmnet8
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fec0:8/64 scope link
valid_lft forever preferred_lft forever

@nwinkler
Copy link
Member

Sorry, not sure what to do with these comments. Before merging this, we need to have some assurance that the changes work reasonably well on the major operating systems.

Please let me know how you want to proceed.

@YumingMa
Copy link
Contributor Author

I will try to modify as your comments to make sure this command run well on the major OS.

@NoahGorny
Copy link
Member

@YumingMa want to take another stab at this?

@cornfeedhobo
Copy link
Member

cornfeedhobo commented Dec 12, 2020

If this isn't picked back up, I vote this function be removed or moved to a plugin called "misc", or something (open to suggestions. maybe "networking"?). Relates to #1640

My argument is that this function is too opinionated to be worth keeping.

@nwinkler @NoahGorny @davidpfarrell

Edit: as for the code, if people really feel compelled to keep this function around, I recommend simplifying this:

if _command_exists ip ; then

	ip address | awk '
/^[0-9]+:/ { sub(/:/,"",$2) ; iface=$2 } 
/^[[:space:]]*inet / { 
  split($2, a, "/")
  print iface" : "a[1] 
}'

elif _command_exists ifconfig ; then

	ifconfig | awk '
/^[a-z]/ { iface=$1 }
/inet addr:/ {
  sub(/inet addr:/, "")
  print iface" : "$1
}'

else
	>&2 echo 'iproute and ifconfig not found! unable to obtain ips!"
	return 1
fi

Note: this would require some changes to support ipv6 too, but it's pretty easy.

@davidpfarrell
Copy link
Contributor

If this isn't picked back up, I vote this function be removed

I also think that this should be removed from the base plugins.

I could see it being its own stand-alone plugin.

I don't know how popular it is, but there's only one use within bash-it itself (not counting the base plugin test). We'd probably want to update that theme to include a command_exists check.

Side note: Additionally, the phoenix alias set creates an ips alias (not related to IP address)

As for the code: It does seem like it could be simplified - I'd probably consider that a post-move action, separated from the act of moving this into its own plugin.

@nwinkler
Copy link
Member

I'm fine with moving it to a separate plugin. I'm not a huge fan of a misc plugin, since it will simply become another collection of odds and ends without any real purpose. Maybe a network plugin?

The usage in the theme is a bit problematic anyway, the theme relies on the function to be available. At the moment, that's OK, since most people have the base plugin enabled by default. Moving the function will require another plugin to be enabled...

@NoahGorny
Copy link
Member

closing this as this is stale

@NoahGorny NoahGorny closed this Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants