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 [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


========================不足之處,請多指教=========================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章