如何向expect腳本里面傳遞參數
比如下面腳本用來做ssh無密碼登陸,自動輸入確認yes和密碼信息,用戶名,密碼,hostname通過參數來傳遞
ssh.exp
Python代碼
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
spawn ssh-copy-id -i .ssh/id_rsa.pub $username@$hostname
expect "yes/no"
send "yes\r"
expect "password:"
send "$password\r"
expect eof
執行腳本./ssh.exp root pasword hostname1
expect接收參數的方式和bash腳本的方式不太一樣,bash是通過$0 ... $n 這種方式,而expect是通過set <變量名稱> [lindex $argv <param index>],例如set username [lindex $argv 0]