This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
80 lines (73 loc) · 2.09 KB
/
common.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
#!/usr/bin/env bash
git_dir=$basedir/config/backup/
# константы для цветов
NO_COLOR="\e[39m"
GRAY="\e[90m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
# преобразование сокращенного ip адреса в полный
function full_ip {
local ip=$1
local rx='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
local prefix='(/[0-9]{1,2})?'
local result
if [[ $ip =~ ^$rx\.$rx$prefix$ ]]; then
result="192.168.$ip"
elif [[ $ip =~ ^$rx\.$rx\.$rx\.$rx$prefix$ ]]; then
result="$ip"
else
result=""
fi
echo "$result"
}
# проверка, что значение содержится в списке
function in_list {
local value=$1; shift; local list=$@
[[ $list =~ (^|[[:space:]])$value($|[[:space:]]) ]] && return 0 || return 1
}
# функция для подтверждения каких либо действий
function agree(){
local agree
echo -e "Press ${YELLOW}[y]${NO_COLOR} to continue, any key to exit"
read -s -n 1 agree
if [[ $agree == "y" ]]; then
echo -e "${GREEN}Confirmed${NO_COLOR}"
return 0
else
echo -e "${RED}Interrupted${NO_COLOR}"
exit 0
fi
}
# функция для перечисления списков портов с сокращениями, например 1-2,5,7-10
function iterate {
local result=""
IFS=','
for l in $1; do
# return default IFS value
IFS=$' \t\n'
local start=$(cut -d '-' -s -f 1 <<< $l)
local end=$(cut -d '-' -s -f 2 <<< $l)
if [[ $start ]]; then
for i in $(seq $start $end); do
result+="$i "
done
else
result+="$l "
fi
done
IFS=$' \t\n'
echo $result
}
# функция для отправки сообщений в лог операторов через клиент на python
function send_log(){
cur_dir=$(pwd)
cd ~/op_client
source .venv/bin/activate
python client.py add -s "$1" "$2"
deactivate
cd $cur_dir
}