linux 批量创建远程用户

2015-09-07

#add_remote_user_bat.sh
#linux批量创建远程用户
#!/bin/bash ### AUTHOR: Joe ### DATE: 2015/07/12 ### REV: 1.0 if [ ! -n "$1" ];then echo -e "Input the username you want grant login privilege to:c" read username pub_key=$(cat /data0/backup/authorized_keys_bak/$username'.pub') echo -e "Input the ip address $username want to login:c" read ipadd echo -e "Whether need root permissions? [yes|no]:c" read sudo ssh -n $ipadd '(chattr -i /etc/passwd /etc/group /etc/shadow /etc/sudoers)' FIND_USER=`ssh -n $ipadd "grep -c $username /etc/passwd"` if [ $FIND_USER -gt 0 ];then ssh -n $ipadd "userdel $username" fi #ssh -n $ipadd '(sed -i "/HOME/s/HOME=/home/HOME=/data/home/g" /etc/default/useradd)' #ssh -n $ipadd '(mkdir -p /data/home)' ssh -n $ipadd '(useradd '$username')' ssh -n $ipadd '(mkdir /home/'$username'/.ssh)' ssh -n $ipadd '(echo '$pub_key' > /home/'$username'/.ssh/authorized_keys)' ssh -n $ipadd '(chown -R '$username' /home/'$username'/.ssh)' ssh -n $ipadd '(chmod 700 /home/'$username'/.ssh;chmod 600 /home/'$username'/.ssh/authorized_keys)' if [ $sudo == yes ];then FIND_SUDO=`ssh -n $ipadd "grep -c $username /etc/sudoers"` if [ $FIND_SUDO -le 0 ];then ssh -n $ipadd "echo -e '$username ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers" echo "$ipadd sudo privilege has been granted to $username!" else echo "$username's sudo privilege already exist on $ipadd!" fi fi ssh -n $ipadd '(chattr +i /etc/passwd /etc/group /etc/shadow /etc/sudoers)' echo "$ipadd login privilege has been granted to $username!" else echo -e "Input the username you want grant login privilege to:c" read username pub_key=$(cat /data0/backup/authorized_keys_bak/$username'.pub') echo -e "Whether need root permissions? [yes|no]:c" read sudo for ipadd in `cat $1` do ssh -n $ipadd '(chattr -i /etc/passwd /etc/group /etc/shadow /etc/sudoers)' FIND_USER=`ssh -n $ipadd "grep -c $username /etc/passwd"` if [ $FIND_USER -gt 0 ];then ssh -n $ipadd "userdel $username" fi ssh -n $ipadd '(useradd '$username')' ssh -n $ipadd '(mkdir /home/'$username'/.ssh)' ssh -n $ipadd '(echo '$pub_key' > /home/'$username'/.ssh/authorized_keys)' ssh -n $ipadd '(chown -R '$username' /home/'$username'/.ssh)' ssh -n $ipadd '(chmod 700 /home/'$username'/.ssh;chmod 600 /home/'$username'/.ssh/authorized_keys)' if [ $sudo == yes ];then FIND_SUDO=`ssh -n $ipadd "grep -c $username /etc/sudoers"` if [ $FIND_SUDO -le 0 ];then ssh -n $ipadd "echo -e '$username ALL=(ALL) NOPASSWD: ALL' >>/etc/sudoers" echo "$ipadd sudo privilege has been granted to $username!" else echo "$username's sudo privilege already exist on $ipadd!" fi fi ssh -n $ipadd '(chattr +i /etc/passwd /etc/group /etc/shadow /etc/sudoers)' echo "$ipadd login privilege has been granted to $username!" done fi
原文地址:https://www.cnblogs.com/cenliang/p/4788764.html