1. 程式人生 > >shell 指令碼中經常要用到ssh。怎麼能不互動的輸密碼呢?

shell 指令碼中經常要用到ssh。怎麼能不互動的輸密碼呢?

1、登入A機器
2、ssh-keygen -t [rsa|dsa],將會在~/.ssh下生成金鑰檔案和私鑰檔案 id_rsa,id_rsa.pubid_dsa,id_dsa.pub
3、將 .pub 檔案複製到B機器的 .ssh 目錄, 並

cat id_dsa.pub >> ~/.ssh/authorized_keys

4、大功告成,從A機器登入B機器的目標賬戶,不再需要密碼了(直接執行 #ssh 192.168.1.100

面交互輸入,就得使用expect指令碼,例:

#!/bin/bash
 passwd='123456'
 /usr/bin/expect <<-EOF
 set
time 30 spawn ssh [email protected]192.168.1.100 expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$passwd\r" } } expect "*#" send "cd /home/trunk\r" expect "*#" send "ls\r" expect "*#" send "exit\r" interact expect eof EOF