shell 实现跳板机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]$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 [email protected] ;; 2) ssh [email protected] ;; 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 实现跳板机的主要内容,如果未能解决你的问题,请参考以下文章