首页 > 代码库 > shell 实现跳板机
shell 实现跳板机
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
========================不足之处,请多指教=========================
本文出自 “迷失在Linux” 博客,请务必保留此出处http://zihang.blog.51cto.com/158746/1913476
shell 实现跳板机
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。