Skip to content

Commit b808194

Browse files
authored
Create ipGet2.sh
Short bash script to get all IPs and interface names
1 parent 2eaa136 commit b808194

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

ipGet2.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Filename: ipGet2.sh
2+
# OS's Supported - Most flavors of RedHat/Ubuntu as well verified on Darwin/OSX
3+
# Outcome: uses a combo of piping thru awk and grep to identify
4+
# a) All interface names
5+
# b) IP address of each
6+
7+
#!/bin/bash
8+
OS=$(uname -s)
9+
10+
# For macOS/Darwin
11+
if [ "$OS" = "Darwin" ]; then
12+
13+
interfaces=($(ifconfig | grep '^[a-z]' | awk '{print $1}' | tr -d ':'))
14+
15+
for intf in "${interfaces[@]}"; do
16+
ip_and_mask=$(ifconfig $intf | grep 'inet ' | awk '{print $2, $4}')
17+
echo "$intf: $ip_and_mask"
18+
19+
done
20+
elif [ "$OS" = "Linux" ]; then
21+
# Linux commands
22+
interfaces=($(ip -o link show | awk -F': ' '{print $2}' | tr -d '@'))
23+
for intf in "${interfaces[@]}"; do
24+
ip_and_mask=$(ip -o -f inet addr show $intf | awk '{print $4}')
25+
echo "$intf: $ip_and_mask"
26+
done
27+
else
28+
echo "Unsupported operating system."
29+
fi
30+
---
31+
Ubuntu Example Output:
32+
33+
bash$ ./ipGet2.sh
34+
lo: 127.0.0.1/8
35+
eth0: 104.x.x.135/20
36+
eth1: 10.108.0.2/20
37+
docker0: 172.17.0.1/16
38+
tun0: 10.8.4.14/24
39+
40+
41+
Darwin Example Output:
42+
bash$ ./ipGet2.sh
43+
lo0: 127.0.0.1 0xff000000
44+
gif0:
45+
stf0:
46+
en6:
47+
ap1:
48+
en0: 192.168.1.103 0xffffff00
49+
awdl0:
50+
llw0:
51+
en3:
52+
en2:
53+
en4:
54+
en1:
55+
bridge0:
56+
utun0:
57+
utun1:
58+
utun2:
59+
utun3:
60+
utun4: 10.8.4.2 10.8.4.1
61+
62+
- As you can see making this work in Mac was a PIA but it works ;)
63+
64+
65+
66+
67+
68+
Verfied on Darwin (GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin23))
69+
Verified on Ubuntu 22 (GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu))
70+

0 commit comments

Comments
 (0)