秘钥认证用户自动控制 发表于 2015-03-18 | 分类于 Shell相关 | | 阅读次数 | 字数统计 1,808 | 阅读时长 9 先上图 再上代码 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138#!/bin/bash #author: QingFeng#qq: 530035210#blog: http://my.oschina.net/pwd/blog #自动添加秘钥认证用户#缺省的配置如下logdir=/data/log/shell #日志路径log=$logdir/shell.log #日志文件 is_font=1 #终端是否打印日志: 1打印 0不打印 is_log=0 #是否记录日志: 1记录 0不记录random_time=$(date +%Y%m%d_%H%M%S)datef(){date "+%Y-%m-%d %H:%M:%S"}print_log(){if [[ $is_log -eq 1 ]];then[[ -d $logdir ]] || mkdir -p $logdirecho "[ $(datef) ] $1" >> $logfiif [[ $is_font -eq 1 ]];thenecho -e "[ $(datef) ] $1"fi}#自动生成keyaddautoKey(){if [[ ! -f /usr/bin/expect ]];thenprint_log "$FUNCNAME():不存在expect函数:开始安装."yum install tcl-devel tcl expect -y -qprint_log "$FUNCNAME():expect函数:安装完成."fimkdir -p /tmp/ssh_$random_timecd /tmp/ssh_$random_timeexpect -c " spawn /usr/bin/ssh-keygen -t rsa set timeout -1 expect \"\*id_rsa)\*:\" send \"$1\r\" expect \"\*no passphrase)\*:\" send \"$1\r\" expect \"\*again\*:\" send \"$1\r\" expect eof " > /dev/nullnum=$(ls /tmp/ssh_$random_time/$1* -l |wc -l)if [[ $num -eq 2 ]];thenprint_log "$FUNCNAME():该用户$1秘钥自动生成完成,路径: /tmp/ssh_$random_time"elseprint_log "$FUNCNAME():\033[31m该用户$1秘钥自动生成失败,退出\033[0m"exitfi}#添加用户addUser(){if [[ $1 == "" ]];thenprint_log "$FUNCNAME():\033[31m用户名不能为空\033[0m"exitfistrlength=$(expr length $1)if [[ $strlength -lt 5 ]];thenprint_log "$FUNCNAME():\033[31m用户名的长度最少大于4,退出\033[0m"exitfiUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }' |grep "$1")if [[ -z $User ]];thenprint_log "$FUNCNAME():不存在非系统用户:$1,开始添加用户操作."adduser $1 -g 10[[ -d /home/$1/.ssh ]] || mkdir /home/$1/.ssh addautoKey $1cp /tmp/ssh_$random_time/$1.pub /home/$1/.ssh/authorized_keys chmod 600 /home/$1/.ssh/authorized_keyschown $1:wheel /home/$1/ -Rcp /etc/ssh/sshd_config /etc/ssh/sshd_config_$(date +%Y%m%d_%H%M%S)sshdUser=$(cat /etc/ssh/sshd_config |grep "$1")if [[ -z $sshdUser ]];thensed -i "s/AllowUsers/AllowUsers $1/" /etc/ssh/sshd_config/etc/init.d/sshd restartprint_log "$FUNCNAME():更新sshd_config文件并重启sshd完成."elseprint_log "$FUNCNAME():sshd_config文件中已经存在$1."fielse print_log "$FUNCNAME():已经存在非系统用户:$1,请确认后在添加."fi }#查找用户lookUp(){loginUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }')print_log "$FUNCNAME():如下用户拥有登陆系统权限:\n\033[32m$loginUser\033[0m"}#删除用户deleteUser(){if [[ $1 == "" ]];thenprint_log "$FUNCNAME():\033[31m用户名不能为空\033[0m"exitfiUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }' |grep "$1")if [[ ! -z $User ]];thenprint_log "$FUNCNAME():存在非系统用户:$1"elseprint_log "$FUNCNAME():\033[31m不存在非系统用户:$1,退出\033[0m"exitfi userdel -rf $1if [[ $? -eq 0 ]];thenprint_log "$FUNCNAME():删除非系统用户:$1成功."elseprint_log "$FUNCNAME():\033[31m删除非系统用户:$1失败.\033[0m"ficp /etc/ssh/sshd_config /etc/ssh/sshd_config_$random_timesed -i "s/$1//g" /etc/ssh/sshd_config /etc/init.d/sshd restartprint_log "$FUNCNAME():更新sshd_config文件并重启sshd完成."}case $1 in add) addUser $2;; look) lookUp;; delete) deleteUser $2;; *) echo -e "秘钥认证用户自动控制\n用法示例: \n1.添加/删除秘钥认证用户: ./account.class.sh add/delete 用户名 \n2.查找可以登陆的用户 ./account.class.sh look";; esac 改进版,检测系统是否添加key认证,无则自动添加,批量添加key认证用户 上图:\ 批量添加用户图: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223#!/bin/bash #author: QingFeng#qq: 530035210#blog: http://my.oschina.net/pwd/blog #自动添加秘钥认证用户#缺省的配置如下logdir=/data/log/shell #日志路径log=$logdir/shell.log #日志文件 is_font=1 #终端是否打印日志: 1打印 0不打印 is_log=0 #是否记录日志: 1记录 0不记录random_time=$(date +%Y%m%d_%H%M%S)#定义默认公钥default_publicKey="ssh-dss CCCCB3NzaC1kc3MAAACBAJRKD+AyqOD2gtLSAPpbbMEy/VrMW8Z8fof1nyKA9OppiUWUWdVL9iJGDdBzvVB3hb9KZYX1bsns77KrD/VB+8jsCe/62rrsUmxJoUwvWyF2B+cvboxwe5cdXyTawt1bAMHNq8jiWrgSDaR7bplFXD3I6lwYk89I+ofxafXxmZE7AAAAFQCay8NRvgNMxkbExxhMLeRZBK2xpwAAAIEAhUpYCf0STqTUcTSTabQDmfizywG7+ZFSvppJCMrWdobG/+rZ61tN2xGWK4zRP13NJOVcIDaXsQwhhuZbGD8d1tEwGqldBAlTsouJWGiWPMJPhUfjKEFTIHn8ug2zDP/vE7yNgiuMalhn+Fglt+AMG78tiOCn1P7kYVjPeGklr8AAAACAWm3qmqYOiTIMtShfmcIJc06XOPPOjxXzwntN+c8rmy+gZbI6wx4vRwYbldaduMtPn7Q29BqJfcCAy/P7ymVyrX/a2ksWehddk7HdyUHnq9WAX1+MxE4BZq7nV4MHD3Fyn8bay+D76BgQdZjTta3dNXwbA5WdmJnZi68Bk5ZXcjM="#定义whell组可以无密码登陆default_Wheel="%wheel ALL=(ALL) NOPASSWD: ALL"#定义sshd配置文件default_user="zhangsan"default_sshdConfig="Protocol 2SyslogFacility AUTHPRIVStrictModes noRSAAuthentication yesPubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keysPermitEmptyPasswords noPasswordAuthentication noChallengeResponseAuthentication noGSSAPIAuthentication yesGSSAPICleanupCredentials yesUsePAM yesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALLX11Forwarding yesUseDNS noSubsystem sftp /usr/libexec/openssh/sftp-serverAllowUsers $default_user"datef(){date "+%Y-%m-%d %H:%M:%S"}print_log(){if [[ $is_log -eq 1 ]];then[[ -d $logdir ]] || mkdir -p $logdirecho "[ $(datef) ] $1" >> $logfiif [[ $is_font -eq 1 ]];thenecho -e "[ $(datef) ] $1"fi}#自动生成keyaddautoKey(){if [[ ! -f /usr/bin/expect ]];thenprint_log "$FUNCNAME():不存在expect函数:开始安装."yum install tcl-devel tcl expect -y -qprint_log "$FUNCNAME():expect函数:安装完成."fimkdir -p /tmp/ssh_$random_timecd /tmp/ssh_$random_timeexpect -c " spawn /usr/bin/ssh-keygen -t rsa set timeout -1 expect \"\*id_rsa)\*:\" send \"$1\r\" expect \"\*no passphrase)\*:\" send \"$1\r\" expect \"\*again\*:\" send \"$1\r\" expect eof " > /dev/nullnum=$(ls /tmp/ssh_$random_time/$1* -l |wc -l)if [[ $num -eq 2 ]];thenprint_log "$FUNCNAME():该用户$1秘钥自动生成完成,路径: /tmp/ssh_$random_time"elseprint_log "$FUNCNAME():\033[31m该用户$1秘钥自动生成失败,退出\033[0m"exitfi}#添加用户addUser(){if [[ $1 == "" ]];thenprint_log "$FUNCNAME():\033[31m用户名不能为空\033[0m"exitfistrlength=$(expr length $1)if [[ $strlength -lt 5 ]];thenprint_log "$FUNCNAME():\033[31m用户名的长度最少大于4,退出\033[0m"exitfiUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }' |grep "$1")if [[ -z $User ]];thenprint_log "$FUNCNAME():不存在非系统用户:$1,开始添加用户操作."adduser $1 -g 10addautoKey $1[[ -d /home/$1/.ssh ]] || mkdir -p /home/$1/.ssh cp /tmp/ssh_$random_time/$1.pub /home/$1/.ssh/authorized_keys chmod 600 /home/$1/.ssh/authorized_keyschown $1:wheel /home/$1/ -Rcp /etc/ssh/sshd_config /etc/ssh/sshd_config_$(date +%Y%m%d_%H%M%S)sshdUser=$(cat /etc/ssh/sshd_config |egrep -v "^$|^#" |grep "$1")if [[ -z $sshdUser ]];thensed -i "s/AllowUsers/AllowUsers $1/" /etc/ssh/sshd_config/etc/init.d/sshd restartprint_log "$FUNCNAME():更新sshd_config文件并重启sshd完成."elseprint_log "$FUNCNAME():sshd_config文件中已经存在$1."fielse print_log "$FUNCNAME():已经存在非系统用户:$1,请确认后在添加."fi }#查找用户lookUp(){loginUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }')print_log "$FUNCNAME():如下用户拥有登陆系统权限:\n\033[32m$loginUser\033[0m"}#删除用户deleteUser(){if [[ $1 == "" ]];thenprint_log "$FUNCNAME():\033[31m用户名不能为空\033[0m"exitfiUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }' |grep "$1")if [[ ! -z $User ]];thenprint_log "$FUNCNAME():存在非系统用户:$1"elseprint_log "$FUNCNAME():\033[31m不存在非系统用户:$1,退出\033[0m"exitfi userdel -rf $1if [[ $? -eq 0 ]];thenprint_log "$FUNCNAME():删除非系统用户:$1成功."elseprint_log "$FUNCNAME():\033[31m删除非系统用户:$1失败.\033[0m"ficp /etc/ssh/sshd_config /etc/ssh/sshd_config_$random_timesed -i "s/$1//g" /etc/ssh/sshd_config /etc/init.d/sshd restartprint_log "$FUNCNAME():更新sshd_config文件并重启sshd完成."}#检查key认证checkUser(){if [[ $default_user == "" ]];thenprint_log "$FUNCNAME():\033[31m用户名不能为空\033[0m"exitfistrlength=$(expr length $default_user)if [[ $strlength -lt 5 ]];thenprint_log "$FUNCNAME():\033[31m用户名的长度最少大于4,退出\033[0m"exitfiUser=$(cat /etc/passwd |grep -v "nologin" |awk -F':' '{if ($3> 500) print $1 }' |grep "$default_user")if [[ -z $User ]];thenprint_log "$FUNCNAME():不存在非系统用户:$default_user,开始添加用户操作."adduser $default_user -g 10[[ -d /home/$default_user/.ssh ]] || mkdir /home/$default_user/.ssh echo $default_publicKey > /home/$default_user/.ssh/authorized_keys chmod 600 /home/$default_user/.ssh/authorized_keyschown $default_user:wheel /home/$default_user/ -Rcp /etc/ssh/sshd_config /etc/ssh/sshd_config_$(date +%Y%m%d_%H%M%S)sshdUser=$(cat /etc/ssh/sshd_config |egrep -v "^$|^#"|grep "$default_user")if [[ -z $sshdUser ]];thenecho -e "$default_sshdConfig" > /etc/ssh/sshd_configsed -i "s/^$//g" /etc/ssh/sshd_config check_suders=$(cat /etc/sudoers |egrep -v "^#|^$"|grep "%wheel")if [[ -z $check_suders ]];thenecho -e "$default_Wheel" >> /etc/sudoersfi /etc/init.d/sshd restartprint_log "$FUNCNAME():添加key认证,更新sshd_config文件并重启sshd完成."elseprint_log "$FUNCNAME():sshd_config文件中已经存在$default_user."fielse check_sshdnum=1check_sudersnum=1check_sshd=$(cat /etc/ssh/sshd_config |egrep -v "^#|^$" |grep "$default_user")if [[ -z $check_sshd ]];thencheck_sshdnum=0print_log "已经添加$default_user用户,但是没有配置sshd_config"ficheck_suders=$(cat /etc/sudoers |egrep -v "^#|^$" |grep "wheel" |grep "NOPASSWD")if [[ -z $check_suders ]];thencheck_sudersnum=0print_log "已经添加$default_user用户,但是没有配置sudoers"fi if [[ $check_sshdnum -ne 0 && $check_sudersnum -ne 0 ]];thenprint_log "该服务器已经配置秘钥认证."fifi}case $1 in add) addUser $2;; look) lookUp;; delete) deleteUser $2;; check) checkUser ;; *) echo -e "秘钥认证用户自动控制\n用法示例: \n1.添加/删除秘钥认证用户: ./account.class.sh add/delete 用户名 \n2.查找可以登陆的用户 ./account.class.sh look\n3.检测系统是否是key认证,不是则添加key认证 ./account.class.sh check\n(默认增加一个[$default_user]的认证用户)";; esac 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏