ssh expect 非交互 密碼 免登錄

由於要實現非交互ssh登陸,每臺機器都去作公鑰顯然太麻煩,因此想簡單的把密碼寫在腳本中,直接登陸執行命令。shell

sshpass原本是首選,可是部分機器須要安裝C compiler, 這也不符合預期,因此決定採用expectbash

網上搜索了一下,基本上都是單獨的expect腳本,不多有在shell中嵌套expect,這樣的話須要兩個腳原本執行。ssh

如今寫Shell中嵌套expect的用法,首先要肯定/usr/bin/expect存在spa

 

#!/bin/bash

 
passwd="12345678"
 
expect <<!
   set time 5
   spawn ssh user@192.168.1.2
     expect {
       "*yes/no" { send "yes\r"; exp_continue }
       "*password:" { send "$passwd\r" }
     }
     expect "*#" { send "df\r" }
	 expect "*#" { send "exit\r" }
	 expect eof
!
相關文章
相關標籤/搜索