期望脚本覆盖文件

时间:2018-07-19 14:20:21

标签: shell for-loop while-loop expect

我正在尝试编写一个期望脚本并在包含ips列表的文件上运行它。

为清楚起见,我的文件ips.txt包含:

10.195.166.52
10.195.166.54

我的期望脚本是:

#!/usr/bin/expect -f

log_file myfile.log ;# <<< === append output to a file

set timeout 180
set username pippo
set old_password 0ldp455w0rd
set new_password n3wp455w0rd
set filedes [open "ips.txt" r]
gets $filedes ip

while {$ip != -1} {
spawn ssh $username@$ip
expect "$username"
send "$old_password\r"
expect "(current)"
send "$old_password\r"
expect "New"
send "$new_password\r"
expect "Retype"
send "$new_password\r"
expect "$username"
send "exit\r"
#expect eof ;#wait for the connection to close
}
close $filedes

我得到的是:

You are required to change your password immediately (root enforced)
Last login: Thu Jul 19 16:14:46 2018 from 10.222.233.139
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user pippo.
Changing password for pippo.
(current) UNIX password: 
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
Connection to 10.195.166.52 closed.
send: spawn id exp6 not open
    while executing
"send "exit\r""
    ("while" body line 12)
    invoked from within
"while {$ip != -1} {
spawn ssh $username@$ip
expect "$username"
send "$old_password\r"
expect "(current)"
send "$old_password\r"
expect "New"
send "$ne..."
    (file "./passwd.xp" line 12)

因此,脚本不会比第一个IP进行得更多。怎么了 ?我正在尝试期望命令的变体而没有成功。

0 个答案:

没有答案