欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Shell批量SSH免交互登錄認證-創(chuàng)新互聯(lián)

腳本實現(xiàn)功能:批量或單個SSH免交互登錄認證

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供三沙網(wǎng)站建設、三沙做網(wǎng)站、三沙網(wǎng)站設計、三沙網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、三沙企業(yè)網(wǎng)站模板建站服務,10余年三沙做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

腳本應用場景:當部署集群時,大多數(shù)實現(xiàn)要配置好管理節(jié)點與從節(jié)點的SSH免交互登錄,針對這樣的情況,寫了下面腳本,簡化工作。

腳本支持系統(tǒng):Ubuntu和CentOS

#!/bin/bash # Description: configuration local host and remote host ssh keypair authentication, Support Ubuntu and CentOS operation system. # Blog: http://lizhenliang.blog.51cto.com   function color_echo() {     if [ $1 == "green" ]; then         echo -e "\033[32;40m$2\033[0m"     elif [ $1 == "red" ]; then         echo -e "\033[31;40m$2\033[0m"     fi } function os_version() {     local OS_V=$(cat /etc/issue |awk 'NR==1{print $1}')     if [ $OS_V == "\S" -o $OS_V == "CentOS" ]; then         echo "CentOS"     elif [ $OS_V == "Ubuntu" ]; then         echo "Ubuntu"     fi } function check_ssh_auth() {     if $(grep "Permission denied" $EXP_TMP_FILE >/dev/null); then         color_echo red "Host $IP SSH authentication failure! Login password error."         exit 1     elif $(ssh $INFO 'echo yes >/dev/null'); then         color_echo green "Host $IP SSH authentication successfully."     fi     rm $EXP_TMP_FILE >/dev/null } function check_pkg() {     local PKG_NAME=$1     if [ $(os_version) == "CentOS" ]; then         if ! $(rpm -ql $PKG_NAME >/dev/null 2>&1); then             echo no         else             echo yes         fi     elif [ $(os_version) == "Ubuntu" ]; then         if ! $(dpkg -l $PKG_NAME >/dev/null 2>&1); then             echo no         else             echo yes         fi     fi } function install_pkg() {     local PKG_NAME=$1     if [ $(os_version) == "CentOS" ]; then         if [ $(check_pkg $PKG_NAME) == "no" ]; then             yum install $PKG_NAME -y             if [ $(check_pkg $PKG_NAME) == "no" ]; then                 color_echo green "The $PKG_NAME installation failure! Try to install again."                 yum makecache                 yum install $PKG_NAME -y                 [ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit 1             fi         fi     elif [ $(os_version) == "Ubuntu" ]; then         if [ $(check_pkg $PKG_NAME) == "no" ]; then             apt-get install $PKG_NAME -y             if [ $(check_pkg $PKG_NAME) == "no" ]; then                 color_echo green "$PKG_NAME installation failure! Try to install again."                 apt-get autoremove && apt-get update                 apt-get install $PKG_NAME --force-yes -y                 [ $(check_pkg $PKG_NAME) == "no" ] && color_echo red "The $PKG_NAME installation failure!" && exit 1             fi         fi     fi } function generate_keypair() {     if [ ! -e ~/.ssh/id_rsa.pub ]; then         color_echo green "The public/private rsa key pair not exist, start Generating..."         expect -c "             spawn ssh-keygen             expect {                 \"ssh/id_rsa):\" {send \"\r\";exp_continue}                 \"passphrase):\" {send \"\r\";exp_continue}                 \"again:\" {send \"\r\";exp_continue}             }         " >/dev/null 2>&1         if [ -e ~/.ssh/id_rsa.pub ]; then             color_echo green "Generating public/private rsa key pair successfully."         else             color_echo red "Generating public/private rsa key pair failure!"             exit 1         fi     fi } EXP_TMP_FILE=/tmp/expect_ssh.tmp if [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@.* ]]; then     install_pkg expect ; generate_keypair     for i in $@; do         USER=$(echo $i|cut -d@ -f1)         IP=$(echo $i|cut -d@ -f2)         PASS=$(echo $i|cut -d@ -f3)         INFO=$USER@$IP         expect -c "             spawn ssh-copy-id $INFO             expect {                 \"(yes/no)?\" {send \"yes\r\";exp_continue}                 \"password:\" {send \"$PASS\r\";exp_continue}             }         " > $EXP_TMP_FILE  # if login failed, login error info append temp file         check_ssh_auth     done elif [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}@.* ]]; then     install_pkg expect ; generate_keypair     START_IP_NUM=$(echo $1|sed -r 's/.*\.(.*)-(.*)@.*/\1/')     END_IP_NUM=$(echo $1|sed -r 's/.*\.(.*)-(.*)@.*/\2/')     for ((i=$START_IP_NUM;i<=$END_IP_NUM;i++)); do         USER=$(echo $1|cut -d@ -f1)         PASS=$(echo $1|cut -d@ -f3)         IP_RANGE=$(echo $1|sed -r 's/.*@(.*\.).*/\1/')         IP=$IP_RANGE$i         INFO=$USER@$IP_RANGE$i         expect -c "             spawn ssh-copy-id $INFO             expect {                 \"(yes/no)?\" {send \"yes\r\";exp_continue}                 \"password:\" {send \"$PASS\r\";exp_continue}             }         " > $EXP_TMP_FILE         check_ssh_auth     done else     echo "Example1: $0 <root@192.168.1.10-15@password>"     echo "Example2: $0 <root@192.168.1.10@password>"     echo "Example3: $0 [root@192.168.1.10@password root@192.168.1.11@password root@192.168.1.12@password ...]" fi

Shell批量SSH免交互登錄認證

Shell批量SSH免交互登錄認證

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前名稱:Shell批量SSH免交互登錄認證-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article48/dediep.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)電子商務虛擬主機自適應網(wǎng)站軟件開發(fā)動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設