1. 程式人生 > >分發系統expect遠程登錄,執行命令,傳遞參數

分發系統expect遠程登錄,執行命令,傳遞參數

分發系統expect遠程登錄 執行命令自動退出 傳遞參數

分發系統:shell 上線腳本expect實現遠程傳輸文件,執行命令,系統上線等功能
expect 腳本遠程登錄
vim 1.expect
#! /usr/bin/expect
set host "192.168.91.129"
set passwd "1q2w3e"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact *停留在遠程機器上,如果想登錄到遠程機器上退出來,使用expect或者eof,如果都不加,立馬就退出來
技術分享圖片

登錄到aminglinux02上
技術分享圖片

expect腳本遠程執行命令
vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "1q2w3e"
spawn ssh [email protected]

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]"
send "touch /tmp/12.txt\r"

expect "]"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
[root@aminglinux-001 ~]# chmod a+x 2.expect
技術分享圖片

expect傳遞參數
#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "1q2w3e"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {

"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]"
send "$cm\r"
expect "]
"
send "exit\r"
[root@aminglinux-001 ~]# chmod a+x 3.expect
只傳遞一個參數
技術分享圖片
傳遞多個參數
技術分享圖片

分發系統expect遠程登錄,執行命令,傳遞參數