-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils-network.sh
More file actions
executable file
·50 lines (41 loc) · 2.45 KB
/
utils-network.sh
File metadata and controls
executable file
·50 lines (41 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/utils-network.sh
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/utils-network.sh
## https://davoudarsalani.ir
## also have a look at --,--> nmcli -c no --terse --fields DEVICE,CONNECTION,STATE dev status
## '--> nmcli -c no -t device
nmcli_output="$(nmcli --colors no device)"
## ethernet
read -a eth_info < <(printf '%s\n' "$nmcli_output" | \grep 'ethernet') ## enp4s0f1 ethernet connected CAB
[ "$eth_info" ] && {
eth_devc="${eth_info[0]}" ## enp4s0f1
eth_state="${eth_info[2]}" ## connected
## ^^ OR: "$(ip -o link show | \grep "$eth_devc" | awk '{print $9}')" ## UP
eth_conn="${eth_info[-1]}" ## CAB
eth_ip="$(ip addr show "$eth_devc" | \grep 'inet ' | awk '{print $2}')"
eth_mac="$(ip addr show "$eth_devc" | \grep 'link/ether' | awk '{print $2}')" ## OR cat /sys/class/net/$eth_devc/address
}
## wifi
read -a wf_info < <(printf '%s\n' "$nmcli_output" | \grep 'wifi[^-]') ## wlp3s0 wifi connected LTE
[ "$wf_info" ] && {
wf_devc="${wf_info[0]}" ## wlp3s0
wf_state="${wf_info[2]}" ## connected
## ^^ OR: "$(ip -o link show | \grep "$wf_devc" | awk '{print $9}')" ## UP
wf_conn="${wf_info[-1]}" ## LTE
wf_ip="$(ip addr show "$wf_devc" | \grep 'inet ' | awk '{print $2}')"
wf_mac="$(ip addr show "$wf_devc" | \grep 'link/ether' | awk '{print $2}')" ## OR cat /sys/class/net/$wf_devc/address
}
## vpn
read -a vpn_info < <(printf '%s\n' "$nmcli_output" | \grep 'tun') ## tun0 tun connected RRR
[ "$vpn_info" ] && {
vpn_devc="${vpn_info[0]}" ## tun0
vpn_state="${vpn_info[2]}" ## connected
## ^^ OR: "$(ip -o link show | \grep "$vpn_devc" | awk '{print $9}')" ## UP
vpn_conn="${vpn_info[-1]}" ## RRR
vpn_ip="$(ip addr show "$vpn_devc" | \grep 'inet ' | awk '{print $2}')"
vpn_mac="$(ip addr show "$vpn_devc" | \grep 'link/ether' | awk '{print $2}')"
}
desired_eth_ip='192.168.200.1/24'
unset nmcli_output