轉自:http://blog.51cto.com/lizhenliang/1607723
注意:使用expect腳本時,需要把腳本添加執行權限,然后./test.sh直接執行,不能用sh或者source執行,否則不能找到expect內置命令。
Linux下實現免交互登陸一般有兩種:
1. SSH無密碼認證方式
客戶端使用ssh-keygen生成密鑰對,將公鑰復制到服務端(authorized_keys),SSH提供公鑰登陸,當SSH訪問服務端時,服務端先在本機尋找客戶端的公鑰,然后把客戶端發來的公鑰進行比較,如果一致,則用公鑰加密給客戶端,客戶端再用私鑰進行解密,實現加密所有傳輸的數據。
1>.在客戶機上創建密鑰對
# ssh-keygen -t rsa #一路回車
2>.登陸ssh服務器,創建.ssh目錄及設置權限
1
2
|
# mkdir /root/.ssh
# chmod 700 /root/.ssh
|
3>.將公鑰上傳到服務器並重命名為authorized.keys
1
|
# scp /root/.ssh/id_rsa.pub root@服務端IP:/root/.ssh/authorized_keys #id_rsa.pub可以追加多個客戶端的公鑰
|
4>.設置ssh服務器
1
2
3
4
5
6
|
# vi /etc/ssh/sshd_config
RSAAuthentication
yes
#這三行取消注釋,開啟密鑰對驗證
PubkeyAuthentication
yes
AuthorizedKeysFile .
ssh
/authorized_keys
PasswordAuthentication no
#關閉密碼驗證
# service sshd restart
|
5>.免交互登陸測試,並查看遠程主機磁盤分區
1
|
# ssh root@服務端IP 'df -h'
|
2. 利用expect工具自動實現交互任務
Expect是一個免費的編程工具語言,用來實現自動和交互式任務進行通信,而無需人的干預。
CentOS安裝:yum install expect
Ubuntu安裝:sudo apt-get install expect
1>.免交互登陸,查看遠程主機磁盤分區
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/expect
set
ip 192.168.1.156
set
pass 123.com
set
timeout 30
spawn
ssh
root@$ip
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$pass\r"
}
}
expect
"root@*"
{send
"df -h\r"
}
expect
"root@*"
{send
"exit\r"
}
expect eof
# interact
|
2>.在Shell腳本中嵌入Expect語法
方法1:使用EOF,將內容段讓expect執行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/bin/bash
user=root
pass=
'123'
ip=
'192.168.1.154'
/usr/bin/expect
<< EOF
set
timeout 30
spawn
ssh
$user@$ip
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$pass\r"
}
}
expect
"root@*"
{send
"df -h\r"
}
expect
"root@*"
{send
"exit\r"
}
expect eof
EOF
|
1
2
3
4
5
6
7
8
9
10
11
|
#!/bin/bash
user=root
pass=
'123'
ip=
'192.168.1.154'
expect -c "
spawn
ssh
$user@$ip
expect {
\"(
yes
/no
)\" {send \"
yes
\r\"; exp_continue}
\"password:\" {send \"$pass\r\"; exp_continue}
\"root@*\" {send \"
df
-h\r
exit
\r\"; exp_continue}
}"
|
方法2:將expect腳本獨立出來
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# vi login.exp #免交互登陸腳本
#!/usr/bin/expect
set
ipaddress [lindex $argv 0]
set
username [lindex $argv 1]
set
password [lindex $argv 2]
if
{ $argc != 3 } {
puts
"Usage: expect login.exp ipaddress username password"
exit
1
}
set
timeout 30
spawn
ssh
$username@$ipaddress
expect {
"(yes/no)"
{send
"yes\r"
; exp_continue}
"password:"
{send
"$password\r"
}
}
expect
"$username@*"
{send
"df -h\r"
}
expect
"$username@*"
{send
"exit\r"
}
expect eof
|
1
2
3
4
5
6
7
8
9
10
11
|
# vi user_info #用戶信息文件
192.168.1.156 user user
192.168.1.154 root 123.com
# vi expect.sh #讀取用戶信息並賦值到變量
#!/bin/bash
for
ip
in
`
awk
'{print $1}'
user_info`
do
user=`
awk
-
v
I=
"$ip"
'{if(I==$1)print $2}'
user_info`
pass=`
awk
-
v
I=
"$ip"
'{if(I==$1)print $3}'
user_info`
expect login.exp $ip $user $pass
done
|
普通自動切換到root腳本,並且在shell中嵌入expect
#!/bin/bash
/usr/bin/expect << EOF
spawn /bin/su -
expect "Password: "
send "123\r"
expect "*#"
interact
expect eof
EOF
或者單獨寫一個腳本,實現普通用戶自動切換到root用戶,去掉上面腳本中的#!/bin/bash,和EOF,然后把這個腳本放在/usr/bin目錄中,把腳本名改為root,然后執行root命令就可以實現切換。
參數說明:
set:可以設置超時,也可以設置變量
timeout:expect超時等待時間,默認10S
spawn:執行一個命令
expect "":匹配輸出的內容
exp_continue:繼續執行下面匹配
\r:可以理解為回車
$argc:統計位置參數數量
[lindex $argv 0]:腳本后第一個參數,類似於shell中$1,以此類推
puts:打印字符串,類似於echo
awk -v I="$ip":賦值變量
expect{...}:輸入多行記錄
其他參數說明:
timeout -1:永不超時退出
log_file /var/log/expect.log:記錄交互信息,一般crontab時使用
interact:交互后不退出遠程終端,如果加要把expect "root@*" {send "exit\r"}注釋掉,如果不加,就直接退出
將spawn ssh root@$ip換成spawn ssh -o StrictHostKeyChecking=no root@ip既不會再提示是否將服務器計算機密鑰加入本地known_hosts