-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibnet.sh
210 lines (194 loc) · 6.33 KB
/
libnet.sh
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
function add_dhcpd_entry {
local subnet=$1
local netmask=$2
local range_from=$3
local range_to=$4
local dirty
local oldvalue
local oldvalue1
local oldvalue2
install_apt_package augeas-tools augtool
if ! augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" match "/files/etc/dhcp/dhcpd.conf/subnet/network" | grep -q $subnet; then
logheredoc EOT
sudo augtool -L -A --transform 'Dhcpd incl /etc/dhcp/dhcpd.conf' <<EOT >/dev/null
clear '/files/etc/dhcp/dhcpd.conf/subnet[last() + 1]'
set "/files/etc/dhcp/dhcpd.conf/subnet[last()]/network" "$subnet"
set "/files/etc/dhcp/dhcpd.conf/subnet[last()]/netmask" "$netmask"
save
EOT
dirty=1
fi
oldvalue=$(augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/netmask" | sed -En 's/\/.* = \"?([^\"]*)\"?$/\1/p')
if [ "$netmask" != "$oldvalue" ]; then
logexec sudo augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" set "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/netmask" "${netmask}" >/dev/null
dirty=1
fi
oldvalue1=$(augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/range/from" | sed -En 's/\/.* = \"?([^\"]*)\"?$/\1/p')
oldvalue2=$(augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/range/to" | sed -En 's/\/.* = \"?([^\"]*)\"?$/\1/p')
if [ "$range_from" != "$oldvalue1" ] || [ "$range_to" != "$oldvalue2" ]; then
logheredoc EOT
sudo augtool -L -A --transform 'Dhcpd incl /etc/dhcp/dhcpd.conf' <<EOT >/dev/null
set "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/range/from" "${range_from}"
set "/files/etc/dhcp/dhcpd.conf/subnet[network='$subnet']/range/to" "${range_to}"
save
EOT
dirty=1
fi
if [ "$dirty" == "1" ]; then
return 0
fi
return 1
}
function edit_dhcpd {
local key=$1
local value=$2
if [ "${value}" == "<ON>" ]; then
if augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/${key}" | grep -q '(o)'; then
logexec sudo augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" clear "/files/etc/dhcp/dhcpd.conf/option/${key}" >/dev/null
return 0
fi
fi
if [ "${value}" == "<OFF>" ]; then
if augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/${key}" | grep -q '(none)'; then
logexec sudo augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" rm "/files/etc/dhcp/dhcpd.conf/option/${key}" >/dev/null
return 0
fi
fi
oldvalue=$(augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" get "/files/etc/dhcp/dhcpd.conf/option/${key}" | sed -En 's/\/.* = \"?([^\"]*)\"?$/\1/p')
if [ "$value" != "$oldvalue" ]; then
logexec sudo augtool -L -A --transform "Dhcpd incl /etc/dhcp/dhcpd.conf" set "/files/etc/dhcp/dhcpd.conf/option/${key}" "${value}" >/dev/null
return 0
fi
return 1
}
function random_mac {
local prefix=$1
local len=${#prefix}
local infix
local hexchars
local end
if [ $len -lt 3 ]; then
infix=":"
else
infix=${prefix:2:1}
# echo "infix=$infix"
fi
if [[ ${prefix:$((len-1)):1} == ${infix} ]]; then
prefix="${prefix:0:$((len-1))}"
# echo "prefix=$prefix"
fi
let "nwords = (${len}+3-1)/3"
# echo "nwords=$nwords"
let "n = 12-2*nwords"
# echo "n=$n"
hexchars="0123456789ABCDEF"
end=$( for i in $(seq ${n}); do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e "s/\(..\)/${infix}\1/g" )
echo ${prefix}${end}
}
function parse_URI {
local URI="$1"
local pattern='^(([[:alnum:]]+)://)?(([[:alnum:]]+)@)?([^:^@]+)(:([[:digit:]]+))?$'
if [[ "$URI" =~ $pattern ]]; then
proto=${BASH_REMATCH[2]}
user=${BASH_REMATCH[4]}
ip=${BASH_REMATCH[5]}
port=${BASH_REMATCH[7]}
return 0
else
errcho "You must put proper address of the ssh server in the first argument, e.g. [email protected]:2022"
return 1
fi
}
function add_host {
local host=$1
local ip=$2
local fqdn=$3
if grep -q "^[\\s\\d\\.]+$host" /etc/hosts; then
#We need to replace the line rather than append
if [[ "$fqdn" == "" ]]; then
logexec sudo sed -i -e "/^[\\s\\d\\.]+$host/$ip $host/" /etc/hosts
else
logexec sudo sed -i -e "/^[\\s\\d\\.]+$host/$ip $host $fqdn/" /etc/hosts
fi
else
if [[ "$fqdn" == "" ]]; then
$loglog
echo "$ip $host" | sudo tee -a /etc/hosts >/dev/null
else
$loglog
echo "$ip $host $fqdn" | sudo tee -a /etc/hosts >/dev/null
fi
fi
}
function enable_host {
local host=$1
local ip=$2
local HOSTS_LINE="${ip} ${host}"
local pattern=" *#? *${ip} +${host}"
pattern=${pattern//./\\.} #replace . into \.
if grep -E "$pattern" /etc/hosts; then #element is already added
on_pattern=" *# *${ip} +${host}"
if grep -E "$on_pattern" /etc/hosts; then #element is not already turned on
sudo sed -i -r "s/${pattern}/${ip} ${host}/g" /etc/hosts
fi
else
add_host "$1" "$2"
fi
}
function disable_host {
local host=$1
local ip=$2
local HOSTS_LINE="${ip} ${host}"
local pattern=" *#? *${ip} +${host}"
pattern=${pattern//./\\.} #replace . into \.
if ! grep -E "$pattern" /etc/hosts; then #element is already added
add_host "$1" "$2"
fi
on_pattern=" *# *${ip} +${host}"
if ! grep -E "$on_pattern" /etc/hosts; then #element is not already turned on
sudo sed -i -r "s/${pattern}/# ${ip} ${host}/g" /etc/hosts
fi
}
function add_host_ssh_certificate {
local host=$1
local line=$(echo -n | openssl s_client -connect ${host}:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p')
textfile /usr/share/ca-certificates/extra/${host}.crt "${line}" root
already_done1=$?
linetextfile /etc/ca-certificates.conf extra/${host}.crt
already_done2=$?
if [[ "${already_done1}" == "0" || "${already_done2}" == "0" ]]; then
logexec sudo update-ca-certificates
fi
}
function get_iface_ip {
local iface=$1
if ifconfig $local_n2n_iface 2>/dev/null >/dev/null; then
ip addr show $1 | awk '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}'
return 0
else
return 1
fi
}
function get_bridge_ifaces {
brctl show | awk 'NF>1 && NR>1 {print $1}'
}
function is_host_up {
ping -c 1 -w 1 $1 >/dev/null
}
function is_host_tcp_port_up {
local host=$1
local port=$2
nc -zw3 $1 $2
}
function get_local_ip {
local line=$(ip route get 1)
local pattern='^.* src (.*) uid'
if [[ $line =~ $pattern ]]; then
echo "${BASH_REMATCH[1]}"
return
else
errcho "Cannot get local ip from $line"
return -1
fi
}