zbuz添加用户,并批量分发ssh公钥脚本:
#!/bin/bash . /etc/init.d/functions function add_user(){ jumper="$1" useradd ${jumper} echo 123456 | passwd --stdin ${jumper} >/dev/null 2>&1 if [ `grep -o $jumper /etc/passwd | wc -l` -gt 1 ] then action "add user ${jumper} success" /bin/true else action "add user ${jumper} success failure" /bin/false exit 0 fi su ${jumper} -c "ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1" if [ $? -eq 0 ] then action "Create ssh pub key success" /bin/true else action "Create ssh pub key failure" /bin/false fi } add_user $* for n in 7 8 do /usr/bin/expect /server/scripts/fenfa_key.exp /home/${jumper}/.ssh/id_dsa.pub ${jumper} 11.0.0.${n} >/dev/null 2>&1 if [ $? -eq 0 ] then action "fenfa ssh pub key to 11.0.0.${n} success" /bin/true else action "fenfa ssh pub key to 11.0.0.${n} failure" /bin/false fi done
expect实现无密码验证
#!/usr/bin/expect if { $argc != 3 } { puts "usage:expect $argv0 sshkey user host" exit } #define var set sshkey [lindex $argv 0] set user [lindex $argv 1] set host [lindex $argv 2] set password "123456" spawn ssh-copy-id -i $sshkey $user@$host expect { "yes/no" {send "yes\r";exp_continue} "*password" {send "$password\r"} } expect eof
简单shell脚本实现跳板机
#!/bin/bash function traper(){ trap '' INT QUIT TSTP TERM HUP } function menu(){ cat <<-EOF ================Host List============== 1)11.0.0.7 2)10.0.0.8 3)exit ================Host End=============== EOF } function host(){ USER=test09 case "$1" in 1) ssh $USER@11.0.0.7 ;; 2) ssh $USER@11.0.0.8 ;; 3) exit esac } function main(){ while true do traper clear menu read -p 'Pls input your choice:' num host $num done } main
开机脚本调用跳板机脚本
cat /etc/profile.d/jump.sh #!/bin/bash [ $UID -ne 0 ] && [ $USER != "zihang" ] &&\ /bin/bash /server/scripts/tiaoban.sh
========================不足之处,请多指教=========================