-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodify_address.sh
executable file
·133 lines (113 loc) · 3.83 KB
/
modify_address.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
#!/usr/bin/env bash
# Copyright (C) 2015-2022 Internet Systems Consortium.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Author: Włodzimierz Wencel
revert_addr() {
IFS='.' read -ra ADDR <<< $1
for i in "${ADDR[@]}"; do
revert=$i"."$revert
done
echo $revert
}
mask2cidr() {
nbits=0
IFS=.
for dec in $1 ; do
case $dec in
255) let nbits+=8;;
254) let nbits+=7;;
252) let nbits+=6;;
248) let nbits+=5;;
240) let nbits+=4;;
224) let nbits+=3;;
192) let nbits+=2;;
128) let nbits+=1;;
0);;
*) echo "Error: new netmask not valid!"; exit 1
esac
done
echo "$nbits"
}
changeaddress() {
read -p "Are you sure? [Yy]" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
cd $1
for file_name in $(find `pwd` -type f -name "*.feature");
do
# make backup if there is none
tmp=$file_name
tmp+="_BACKUP"
cp -n $file_name $tmp
# Changing address
printf "\nChanging file: %s" $file_name
mask=$(mask2cidr $3)
sed -i "s/$4.0\/24/$2.0\/$mask/" $file_name
sed -i "s/$5/$3/" $file_name
default_revert=$(revert_addr $4)
new_revert=$(revert_addr $2)
sed -i "s/$default_revert/$new_revert/" $file_name
sed -i "s/$4/$2/g" $file_name
done
printf "\n"
else
printf "\nAbording..\n"
exit 0
fi
}
restore_files(){
read -p "Are you sure? [Yy]" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
cd $1
for file_name in $(find `pwd` -type f -name "*_BACKUP");
do
printf "\nRestoring file: %s" ${file_name::-7}
mv -f $file_name ${file_name::-7}
done
printf "\n"
else
printf "\nAbording..\n"
exit 0
fi
}
## main
if [ "$1" == --help -o "$1" == -h ]; then
#TODO: write help here
printf "That script will be an interactive configuration of Forge.\nTo change default settings:
./modify_address -d <path_to_test_dir> <new_subnet_addr_3_octets> <new_netmask>
to change addresses that are not default:
./modify_address -s <path_to_test_dir> <new_subnet_addr_3_octets> <new_netmask>\
<old_subnet_addr_3_octets> <old_netmask>\nIf you want to restore tests form backup files:
./modify_address -r <path_to_directory>\n"
exit 0
elif [ "$1" == --default-addr -o "$1" == -d ]; then
default_address="192.168.50"
default_netmask="255.255.255.0"
printf "You are about to change all tests in path: %s" $2
printf "\nYour variables are:\nNew address: %s\nNew netmask: %s\n" $3 $4
changeaddress $2 $3 $4 $default_address $default_netmask
exit 0
elif [ "$1" == --switch-addr -o "$1" == -s ]; then
printf "You are about to change all tests in path: %s" $2
printf "\nYour variables are:\nOld subnet address: %s\nNew subnet address: %s\n" $5 $3
printf "Old netmask: %s\nNew netmask: %s\n" $6 $4
changeaddress $2 $3 $4 $5 $6
exit 0
elif [ "$1" == --switch-str -o "$1" == -st ]; then
printf "You are about to change all FILES in path: %s" $2
printf "\nYour variables are:\nOld subnet address: %s\nNew subnet address: %s\n" $5 $3
printf "\nYou are about to change string: %s\nTo: %s\n" $5 $3
changestr $2 $3
exit 0
elif [ "$1" == --restore-addr -o "$1" == -r ]; then
printf "You are about to restore backuped tests features in directory: %s\nAll previous changes will be lost!\n" $2
restore_files $2
exit 0
else
printf "Choose first argument from: --help -h; --default -d; --switch -s; --restore -r\n"
exit 0
fi