-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_hosts_file_server.sh
More file actions
175 lines (146 loc) · 4.31 KB
/
edit_hosts_file_server.sh
File metadata and controls
175 lines (146 loc) · 4.31 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
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
<<"COM"
Descripiton: This script will edit the generated host file from ACAS in CSV format. To only contain servers.
Date: 10/29/2024
Author: iStackz
COM
# display message
echo -e "The number of parameters given is $#\n";
# parameter check
if [[ $# < 1 ]]
then
# display message
echo -e "\nError! No host file supplied";
echo -e "\nUsage: edit_hostfile_server_only.sh <filename>";
# error exit
exit 1;
fi
# check if supplied file exists
if [[ -e $1 ]]
then
# display message
echo -e "\nYou have provided $1 as a parameter";
fi
# create a variable
declare ANSWER;
# while loop to prompt user for input
while [[ $ANSWER != 'yes' ]]
do
# promp user for input
read -p "Do you want to edit the host file to contain servers only? (yes/no): " ANSWER;
# if statment to check if $ANSWER == 'no'
if [[ $ANSWER == 'no' ]]
then
# display message
echo -e "\nYou've answered 'no'. Will now exit the script";
# normal exit
exit 0;
fi
done
# create a temporary file
TEMPFILE=$(mktemp);
## TESTS ##
# add lines to file
#echo $(grep -o "\[all\:vars\]" $1) >> $TEMPFILE;
#echo -e "$(grep -i 'ansible' $1)\n" >> $TEMPFILE;
#echo -e "$(grep -i '^gather' $1)" >> $TEMPFILE;
#sed -i '/^$/d' $TEMPFILE;
#echo -e "\n$(grep -o '\[today\]')" >> $TEMPFILE
#echo -e "$(grep -E '.+srv[0-9]{2}' $1)" >> $TEMPFILE;
#echo -e "$(grep -E '.+srv[0-9]{4}' $1)" >> $TEMPFILE;
#echo -e "$(grep -E '.*ns[0-9](\d{2}|\d{4})' $1)" >> $TEMPFILE;
# working regex
#echo -e "$(grep -E '.*srv[0-9].*' $1)" >> $TEMPFILE;
#echo -e "$(grep -E '.*db[0-9].*' $1)" >> $TEMPFILE;
#echo -e "$(grep -E '.+fs[0-9]' $1)" >> $TEMPFILE;
# while loop to iterate through host file (recommended by ChatGPT because for loop would cut output)
while IFS= read -r LINE
do
# if statement to check host file for regex
if [[ $LINE =~ \[all\:vars\] ]]
then
# add line to tempfile
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ ^ansible.*$ ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ ^vars.*$ ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ ^gather.*$ ]]
then
echo -e "$LINE\n" >> $TEMPFILE;
elif [[ $LINE =~ \n ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ \[today\] ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .*srv[0-9].* ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .*db[0-9].* ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .+fs[0-9] ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .+ns[0-9]{2}.+ ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .*srv[0-9]{4}.* ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .*db[0-9]{4}.* ]]
then
echo $LINE >> $TEMPFILE;
elif [[ $LINE =~ .*ns[0-9]{4}.* ]]
then
echo $LINE >> $TEMPFILE;
fi
done < $1; # passes the hostsfile into the while loop
# rename the tempfile to the original hostfile
if [[ -s $TEMPFILE ]]
then
# rename file
mv $TEMPFILE $1;
# display message
echo -e "\nFile filtered successfully. Only matching hostnames have been kept";
else
# display message
echo -e "\nNo matching hostsnames found. Original file is unchanged";
# remove the tempfile
rm $TEMPFILE;
fi
#################################################################################
<<"COM"
alternate version concise
COM
# Array of regular expressions
REGEX=(\[all\:vars\] ^ansible.*$ ^vars.*$ ^gather.*$ \[today\] .*srv[0-9].*s .*db[0-9].*s .+fs[0-9] .+ns[0-9]{2}.+ .*srv[0-9]{4}.* .*db[0-9]{4}.* .*ns[0-9]{4}.*);
# while loop to iterate through hostfile
while IFS=read -r LINE
do
# for loop to iterate through REGEX ARRAY
for EXPRESSION in "${#REGEX[@]}"
do
# compare LINE from hostfile to EXPRESSION from REGEX array
if [[ $LINE =~ $EXPRESSION ]]
then
# add line to file (if matched)
echo $LINE >> $TEMPFILE;
fi
done
done
# check if tempfile is empty or not
if [[ -s $TEMPFILE ]]
then
# display message
echo -e "\nFile filtered successfully. Only matching hostnames have been kept";
# rename file
mv $TEMPFILE $1;
else
# display message
echo -e "\nNo matching hostnames found. Original file remains unchanged.";
# remove tempfile
rm -f $TEMPFILE;
fi