Reputation: 169
I have a following bash script:
#!/bin/bash
public_ip=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
#echo "$public_ip"
declare -a FILELIST
for existing_user in $(ls /home/); do
#echo "$existing_user"
FILELIST[${#FILELIST[@]}+1]=$(echo "$existing_user")
done
declare -a FILELIST_WITH_VALID_USERS
echo "Start execution of user-check.sh"
while read -r line; do
# Split the line into a name and a list of variables
IFS=',' read -r server_ip package_type users <<< "$line"
echo "$server_ip $package_type $users"
if [[ $public_ip == $server_ip ]]; then
IFS=',' read -r -a users_array <<< "$users"
for user in "${users_array[@]}"; do
[[ ${FILELIST[*]} =~ "$user" ]] && echo "$user is valid and should be present in server $server_ip" || echo "$user should be deleted and is not supposed to be present in server $server_ip"
done
fi
done < server.info
It is main goal to get existing users into array from /home and to get users from file server.info and then to notify if user is present in /home but not is present in server.info
Server.info looks like this(format: server_ip, package_type, array of users):
104.248.245.89,rpm,auditor,igor.test,ivan.test,michael.test,vova.test,testbot
When I run it here is output:
Start execution of user-check.sh
104.248.245.89 rpm auditor,igor.test,ivan.test,michael.test,vova.test,testbot
auditor is valid and should be present in server 104.248.245.89
igor.test is valid and should be present in server 104.248.245.89
ivan.test is valid and should be present in server 104.248.245.89
michael.test is valid and should be present in server 104.248.245.89
vova.test is valid and should be present in server 104.248.245.89
testbot is valid and should be present in server 104.248.245.89
However in my ls /home I also have user ruslan.test and bash script should output that this user is invalid as it is not present in /home
Upvotes: 0
Views: 53
Reputation: 1707
The problem with the logic of your script is that you are using the list containing ONLY known VALID users (server.info) as the list for the loop performing the check.
You should be using the list of candidates that need to be evaluated (home_users.list) for that! I made that change for the first testing loop.
I also modified the logic and report of the original loop to report which "authorized" users were not yet created on the remote host.
A test version of your script showing this is as follows. I have taken the liberty to offer some suggestions on formatting the output, to make things stand out during visual scan and easier to parse for a subsequent "cleanup batch".
#!/bin/bash
redON="\e[91;1m"
redOFF="\e[0m"
orangeON="\e[33;1m"
orangeOFF="\e[0m"
cat >server.info <<EnDoFiNpUt
104.248.245.89,rpm,auditor,igor.test,ivan.test,michael.test,vova.test,newbot,testbot
EnDoFiNpUt
cat >home_users.list <<EnDoFiNpUt
auditor
igor.test
ivan.test
michael.test
ruslan.test
vova.test
testbot
EnDoFiNpUt
declare -a FILELIST
i=0
#for existing_user in $(ls /home/)
for existing_user in $(cat home_users.list)
do #echo "$existing_user"
FILELIST[${i}]=$(echo "$existing_user")
i=$((i+=1))
#echo "${i} => ${existing_user}"
done
public_ip=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
#echo "$public_ip"
public_ip=104.248.245.89
#declare -a FILELIST # FILELIST_WITH_VALID_USERS
echo "Running 'user-check.sh' ..."
while read -r line
do
# Split the line into a name and a list of variables
IFS=',' read -r server_ip package_type users <<< "${line}"
printf "[${server_ip}] ${package_type} ${users}\n"
if [[ ${public_ip} == ${server_ip} ]]
then
IFS=',' read -r -a users_array <<< "${users}"
for user in "${FILELIST[@]}"
do
if [[ ${users_array[*]} =~ "${user}" ]]
then
#echo "${user} is valid and should be present in server ${server_ip}"
printf "[${server_ip}] Valid and confirmed - ${user}\n"
else
#echo "${user} should be deleted and is not supposed to be present in server ${server_ip}"
printf "[${redON}PURGE${redOFF}] ${user} @ ${server_ip} for ${package_type}\n"
fi
done
for user in "${users_array[@]}"
do
if [[ ! ${FILELIST[*]} =~ "${user}" ]]
then
printf "[${orangeON}MISSING${orangeOFF}] ${user} @ ${server_ip} for ${package_type}\n"
fi
done
fi
done < server.info
The session output is as follows:
ericthered:/0__WORK$ ./test_95.sh
Running 'user-check.sh' ...
[104.248.245.89] rpm auditor,igor.test,ivan.test,michael.test,vova.test,newbot,testbot
[104.248.245.89] Valid and confirmed - auditor
[104.248.245.89] Valid and confirmed - igor.test
[104.248.245.89] Valid and confirmed - ivan.test
[104.248.245.89] Valid and confirmed - michael.test
[PURGE] ruslan.test @ 104.248.245.89 for rpm
[104.248.245.89] Valid and confirmed - vova.test
[104.248.245.89] Valid and confirmed - testbot
[MISSING] newbot @ 104.248.245.89 for rpm
ericthered:/0__WORK$
or graphically,
Upvotes: 1