This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webinterface-persist-ip
executable file
·132 lines (110 loc) · 2.67 KB
/
webinterface-persist-ip
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
#!/usr/bin/env bash
# Copyright (c) 2023 rM-self-serve
# SPDX-License-Identifier: MIT
webinterface_persist_ip_version='v2.0'
ip_cmd="ip addr change 10.11.99.1/32 dev usb0"
main() {
case "$@" in
'-h' | '--help' | '')
cli_info
;;
'apply-prstip')
apply_prstip
;;
'revert-prstip')
revert_prstip
;;
*)
echo 'input not recognized'
cli_info
;;
esac
true
}
cli_info() {
echo -e "${GREEN}webinterface-persist-ip ${webinterface_persist_ip_version}${NC}"
echo ''
echo 'This simple program will ensure the web-interface is internally'
echo "accessible at 10.11.99.1:80 after disconnecting the usb cord."
echo 'Source+Docs: https://github.com/rM-self-serve/webinterface-persist-ip'
echo ''
echo -e "${CYAN}USAGE:${NC}"
echo ' webinterface-persist-ip [OPTIONS]'
echo ''
echo -e "${CYAN}COMMANDS:${NC}"
echo ' apply-prstip Apply persist ip modification'
echo ' revert-prstip Revert persist ip modification'
echo ''
echo -e "${CYAN}OPTIONS:${NC}"
echo ' -h, --help Show help'
}
vars_prstip() {
target_file='/etc/ifplugd/ifplugd.action'
target_content=$(cat $target_file)
stop_line=$(echo "$target_content" | grep -n 'systemctl stop' | cut -d : -f 1)
total_line=$(echo "$target_content" | wc -l)
tmp_file="/home/root/.webint-persist-ip.ifplugd.action.tmp"
}
is_action_moded() {
echo "$1" | grep -qhE "$ip_cmd"
}
apply_prstip() {
vars_prstip
echo "Applying webint-upldbtn will edit:"
echo "/etc/ifplugd/ifplugd.action"
echo ""
read -r -p "Would you like to continue? [y/N] " response
case "$response" in
[yY][eE][sS] | [yY])
apply_prstip_act
;;
*)
echo "Cancel"
;;
esac
}
apply_prstip_act() {
if is_action_moded "$target_content"; then
echo "Persist ip already applied"
exit
fi
root_line="$((stop_line + 1))"
[[ -f $tmp_file ]] && rm "$tmp_file"
{
echo "$target_content" | sed -n 1,"$stop_line"p
echo " $ip_cmd"
echo "$target_content" | sed -n "$root_line","$total_line"p
} >"$tmp_file"
chmod +x "$tmp_file" && cp "$tmp_file" "$target_file"
echo "Persist ip applied"
}
revert_prstip() {
vars_prstip
read -r -p "Would you like to revert persist ip? [y/N] " response
case "$response" in
[yY][eE][sS] | [yY])
revert_prstip_act
;;
*)
echo "Cancel"
;;
esac
}
revert_prstip_act() {
if ! is_action_moded "$target_content"; then
echo "Persist ip has not been applied"
exit
fi
root_line="$((stop_line + 2))"
[[ -f "$tmp_file" ]] && rm "$tmp_file"
{
sed -n 1,"$stop_line"p "$target_file"
sed -n "$root_line","$total_line"p "$target_file"
} >"$tmp_file"
chmod +x "$tmp_file" && mv "$tmp_file" "$target_file"
echo "Persist ip reverted"
}
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
main "$@"