溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

expect一鍵實現集群ssh免密登入

發布時間:2020-06-25 10:38:14 來源:網絡 閱讀:1580 作者:jiege333 欄目:建站服務器

expect具有非交互式功能

yum -y install expect

mkpasswd -l 20   #<==生成隨機字符串,-l參數指定生成字符串的長度


非交互密鑰分發

添加用戶(所有機器)

useradd jiege1

echo 123456|passwd --stdin jiege1

id jiege1


10創建密鑰對

su - jiege1

ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa>/dev/null 2>&1


10一鍵分發公鑰expect腳本沒指定主機

vim fenfa_sshkey.exp

#!/usr/bin/expect

if { $argc != 2 } {

  send_user"usage:expect fenfa_sshkey.exp file host\n"

  exit

}

#define var

set file [lindex $argv 0]

set host [lindex $argv 1]

set password "123456"

#spawn scp /etc/hosts root@10.0.0.142:/etc/hosts

#spawn scp -P6666 $file jiege1@$host:$dir

spawn ssh-copy-id -i $file "-p6666 jiege1@$host"

expect {

   "yes/no"   {send "yes\r";exp_continue}

   "*password"   {send "$password\r"}

}

expect eof

 

exit -onexit {

  send_user"jiege say good bye to you!\n"

}


expect fenfa_sshkey.exp .ssh/id_dsa.pub 192.168.169.11

#需手動輸入公鑰和ip,但是不用手動輸yes和密碼了(expect非交互式功能)可以把fenfa_sshkey.exp寫入腳本一鍵實現集群通過主服務器ssh免密登入 


批量寫入多臺主機

vim fenfa_sshkey.sh

#!/bin/sh

. /etc/init.d/functions

for ip in 11 12 13 14 15

do

  expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 192.168.169.$ip >/dev/null 2>&1

  if [ $? -eq 0 ];then

     action "$ip" /bin/true

  else

     action "$ip" /bin/false

  fi

done


注意此腳本要和fenfa_sshkey.exp一個目錄

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女