expect實現ssh無密鑰登陸

來源:互聯網
上載者:User

expect實現ssh無密鑰登陸 一年多錢就用過expect,當時寫了個用expect實現ssh無密鑰登陸的指令碼,後來弄丟了。今晚和同學一起裝了個100個節點的叢集,實在是噁心了,於是又溫習了一遍。先安裝expect:[plain] # yum install expect   我用了兩個指令碼,一個bash指令碼(send_key.sh),在其中調用另外一個expect指令碼(scp_key_to_node.exp),兩個指令碼放在同一個目錄下:bash指令碼:[plain] #!/bin/bash  ssh-keygen -t dsa  for (( i = 1; i <= 100 ; i ++ ))  do    ./scp_key_to_node.exp $i  done   expect指令碼:[plain] #!/usr/bin/expect  set timeout 5  set hostno [lindex $argv 0]  spawn scp ~/.ssh/id_dsa.pub impala$hostno:~/.ssh/pub_key  expect "*password*"  send "111111\r"  spawn ssh impala$hostno "cat ~/.ssh/pub_key/ >> ~/.ssh/authorized_keys"  expect "*password*"  send "111111\r"  spawn ssh impala$hostno "chmod 600 ~/.ssh/authorized_keys"  expect "*password*"  send "111111\r"  expect eof   set可以設定逾時,或者設定一個變數的值spawn是執行一個命令expect等待一個匹配的輸出資料流中的內容send是匹配到之後向輸入資料流寫入的內容[lindex $argv 0]表示指令碼的第0個參數expect eof表示讀取到檔案結束符 在指令碼所在的目錄下執行:[plain] # ./send_key.sh   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.