I need a script to add a users workstation ip to /etc/sysconfig/iptables
and then run a service iptables reload
. I need the same type of script for removing this rule from iptables.
I found the code below on a different forum.
It's somewhat what I need to do. It includes add and remove options, but instead of adduser
it needs to reflect the iptable rule such as
iptables -I INPUT 1 -p tcp -s xxx.xxx.xx.xxx --dport 22 -m comment --**comment "testing for user1" -j ACCEPT
.
The ip address itself needs to be passed as a parameter, so I can execute as
./script 155.154.15.00
I am using RHEL version 6.4.
Any feedback is appreciated.
#!/bin/bash
clear
echo "########## MENU ############\n"
options=("add_user" "delete_user" "exit")
select opt in "${options[@]}"
do
case $opt in
"add_user")
clear
while [ 1 ]
do
clear
echo "1. Add user manually"
echo "2. Add user via TXT file"
read -p "Enter your choice" ch
case $ch in
1)
read -p "Enter user name : " useradd
read -p "Enter user password:" passwd
echo -e "Successfully added the user"
;;
2)
if [ $(id -u) -eq 0 ]; then
for row in `more $1`
do
username=${row%:*}
password=${row#*:}
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
done
else
echo "Only root may add a user to the system"
exit 2
fi
esac
done
;;
"delete_user")
read -p "Enter a User name to delete "UNAME
passwd
userdel $UNAME
echo "User $UNAME has been deleted"
;;
"exit")
break
;;
esac
done