我正在尝试使用expect脚本来更改多个服务器上的密码,但我对如何将服务器列表传递给它感到困惑。
我正在使用的脚本如下:
#!/usr/bin/expect -f
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd
set username [lindex $argv 0]
set password [lindex $argv 1]
set serverid [lindex $argv 2]
set newpassword [lindex $argv 3]
spawn ssh -t $serverid passwd
expect "assword:"
sleep 3
send "$password\r"
expect "UNIX password:"
sleep 3
send "$password\r"
expect "password:"
sleep 3
send "$newpassword\r"
expect "password:"
sleep 3
send "$newpassword\r"
expect eof
我正试图这样运行:
[blah@blah ~]$ ./setkey1 blah password 'cat serverlist' meh
给出了以下输出:
spawn ssh -t cat serverid passwd
ssh: cat serverid: Name or service not known
send: spawn id exp6 not open
while executing
"send "$password\r""
(file "./setkey1" line 13)
然后我尝试运行以下for循环:
[blah@blah ~]$ for i in serverid; do `cat serverid`; ./setkey1 blah password $i meh; done
这给了我以下内容:
-bash: staging01v: command not found
spawn ssh -t serverid passwd
ssh: serverid: Name or service not known
send: spawn id exp6 not open
while executing
"send "$password\r""
(file "./setkey1" line 13)
如果我尝试使用expect脚本,只输入一个服务器名称,它就可以......嗯...预期。
我做错了什么?
答案 0 :(得分:1)
有很多方法可以解决这个问题。我会改变你的参数的顺序,以便能够传入多个服务器。
在期待计划中:
foreach {username password newpassword} $argv break
set servers [lrange $argv 3 end]
foreach serverid $servers {
# your existing code goes here
}
然后从shell中调用它
./setkey1 userid pass newpass $(cat servers.txt)
如果您使用bash,则可以
./setkey1 userid pass newpass $(<servers.txt)
答案 1 :(得分:0)
我做了类似的事情就是我的方式
set servers "server1 server2 server3"
set users "user1 user2 user3"
set passwords "password1 password2 password3"
set newpasswords "new1 new2 new3"
foreach server $servers user $users password $passwords newpassword $newpasswords
{
Your commands using the variables server/user/password/newpassword
}
这将在循环中为集合中的每个元素运行命令 所以第一个服务器第一个用户在循环中使用第一个密码,然后转到第二个等等。