#! /bin/expect
set timeout 20
set user [lindex $argv 0]
set password [lindex $argv 1]
set prompt "$ " ;# -- main activity
proc dostuff { currenthost} {
;# do something with currenthost
send -- "ls -lrt\r" return} ;# -- start of task
set fd [open ./hostlist r]
set hosts [read -nonewline $fd]
close $fd
foreach host [split $hosts "\n" ] {
spawn /usr/bin/ssh $user@$host
while (1) {
expect {
"no)? " {
send -- "yes\r"
}
"password: " {
send -- "$password\r"
}
"$prompt" {
dostuff { $host }
break
}
}
}
expect "$prompt"
send -- "exit\r"
}
expect eof
我在网上浏览了这段代码,但如果它被排列在 2 列中,它会读取数据。
HOST TFTP_SERVER_ADDRESS
xx XXYY
aa AABB
... ...
... ....
等等
我想将每个 TFTP_SERVER_ADDRESS 分配给一个变量并在我的脚本中使用它。
此外,这段代码是否安全:
# grab the password
stty -echo
send_user -- "Password for $user@$host: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
#... later
send -- "$pass\r"