--------------------------------------------------> 代码如下<----------------------------------------------------------
1 #!/bin/bash 2 # 3 # 功能:实现基于SSH密钥对通信的主机自动化运维和快速部署 4 # 须知:此脚本目前只实现了,批量SSH无密码登录,更多跨主机自动化执行操作,需要后期根据功能写成函数模块 5 # 联系:QQ-765482322 email:login_532_gajun@sina.com 6 7 # variable define 8 script_path="/etc/keepalived/$(basename $0)" 9 ssh_user=root 10 ssh_passwd="s23gajun" 11 12 # function define 13 check_int(){ 14 local char=$1 15 if [[ $char =~ ^[1-9][0-9]*$ ]];then 16 return 0 17 else 18 return 1 19 fi 20 } 21 22 check_ip(){ 23 local IP=$1 24 valid_check=$(echo "$IP" | egrep "^([0-9][0-9]*.){3}[0-9][0-9]*$" | awk -F. '{if (NF==4&&($1>=1&&$1<=239)&&($2>=0&&$2<=255)&&($3>=0&&$3<=255)&&($4>=1&&$4<=254))print "yes"}') 25 26 if [[ "$valid_check" == "yes" ]];then 27 active_check=$(wget --connect-timeout=2 -t2 $IP:22 -O /dev/null &> /dev/null;echo $?) 28 if [[ "$active_check" -ne 0 ]];then 29 return 2 30 else 31 return 0 32 fi 33 else 34 return 1 35 fi 36 } 37 38 ssh_keygen(){ 39 /usr/bin/expect << EOF 40 set timeout 5 41 spawn ssh-keygen -t rsa 42 expect { 43 "*save the key*" {send " ";exp_continue} 44 "Enter passphrase*" {send " ";exp_continue} 45 "*passphrase again:" {send " "} 46 } 47 expect eof 48 EOF 49 50 } 51 52 push_sshkey(){ 53 local host=$1 54 /usr/bin/expect << EOF 55 set timeout 10 56 spawn scp -p /root/.ssh/id_rsa.pub $ssh_user@$host:/root/.ssh/authorized_keys 57 expect { 58 "(yes/no)" {send "yes "; exp_continue} 59 "password:" {send "$ssh_passwd "} 60 "id_rsa.pub" {puts "(^_^) ";exit 2 } 61 } 62 expect eof 63 EOF 64 65 } 66 67 # main 68 read -p "Please enter a number of hosts that need to be operated: " Host_Num 69 echo "=============================================================" 70 71 # 通过函数check_int判断输入的字符是否为整型 72 check_int $Host_Num 73 74 if [ ! $? -eq 0 ];then 75 echo -e "