用户登录自动调用修改网络信息脚本
当用户user登录是调用/usr/bin/changeip.sh
useradd user
passwd user #修改密码
vim /etc/passwd
user:x:0:0::/home/user:/bin/changeip.sh
接下来你懂的!
#/usr/bin/changeip.sh脚本如下
#!/bin/bash netdevice=eth0 show_ip() { echo -e "\033[42;37m ------------------------Display IP Address------------------------ \033[0m" ifconfig $netdevice | grep "inet" | grep -v "inet6" echo -e "\033[42;37m ------------------------Display Route Information------------------------ \033[0m" route -n | grep $netdevice } change_ip() { echo -e "\033[41;37m ------------------------Welcome to Use Platform------------------------ \033[0m" echo -e "\033[42;37m ------------------------Modify IP Address------------------------ \033[0m" oldip=`ip a |grep -w eth0|sed -n '2p'|awk '{print $2}'|cut -f1 -d '/'`
#防止ctrl+c退出 trap "" SIGINT echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" read -p "Please enter a new IP address:" newip echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" read -p "Please enter a subnet mask:" netmask echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" read -p "Please enter a gateway address:" gateway echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" read -p "Please enter dns1 address:" dns1 echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" read -p "Please enter dns2 address:" dns2 echo -e "\033[41;37m ----------------------------------------------------------------- \033[0m" #修改php配置文件 echo `php /usr/bin/changeip.php ${netdevice} ${newip} ${netmask} ${gateway} ${dns1} ${dns2}` if [ 'false' = `php /usr/bin/changeip.php ${netdevice} ${newip} ${netmask} ${gateway} ${dns1} ${dns2}` ] then echo -e "\033[41;37m Modify network error! \033[0m" continue fi sed -i -e '/IPADDR/d' -e '/NETMASK/d' -e '/GATEWAY/d' -e '/DNS1/d' -e '/DNS2/d' /etc/sysconfig/network-scripts/ifcfg-$netdevice sed -i '$ a\IPADDR='$newip'\n\NETMASK='$netmask'\n\GATEWAY='$gateway'\n\DNS1='$dns1'\n\DNS2='$dns2'' /etc/sysconfig/network-scripts/ifcfg-$ netdevice ifconfig eth0 $newip netmask $netmask route add default gw $gateway } exit_con() { exit } show_menu() {
echo "1) View Network Configure" echo "2) Modify Network Configure" echo "3) Exit Network Configure" read -p "Select: " num case $num in 1) show_ip ;; 2) change_ip ;; 3) exit_con esac } while true; do show_menu done