期待从文件中读取脚本问题(Expect Scripting Issue Reading From File)

编程入门 行业动态 更新时间:2024-10-12 18:18:06
期待从文件中读取脚本问题(Expect Scripting Issue Reading From File)

我试图在一堆基于Linux的路由器上从一个列表中提取IP来更改用户的密码。 iplist.txt只是一个ips列表(每行一个)。 这是我尝试使用的代码:

#!/usr/bin/expect set timeout 20 #Edit for User set user username #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set hosts [split [read $f] "\n"] close $f foreach host $hosts { spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$host expect "assword:" send "$old\r" expect ">" send "user set $user password=$new\r" expect ">" send "quit\r" expect eof close }

哪个适用于列表中的第一个ip但是在第二个ip上发送此错误:

spawn_id: spawn id exp4 not open

我按照我想要的方式让它以这种方式工作:

#!/usr/bin/expect set timeout 30 #Edit for User set user user #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set data [read $f] close $f foreach line [split $data \n] { if {$line eq {}} continue spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line expect "assword:" send "$old\r" expect ">" send "user set $user password=$new\r" expect ">" send "\r" expect ">" send "quit\r" send "\r" expect eof }

我遇到的下一个问题是,如果设备没有旧密码,或者如果linux盒子无法通过ssh到达设备,那么当它到达那个IP时它将会出现相同的spawn id exp *错误输出不继续列表中的下一个IP。 无论如何我有一个声明说,如果“assword:”第二次出现进入下一个IP,如果“>”出现就像它应该继续使用脚本,那么在spawn命令之后添加一行如果它没有收到第一个期望的“assword:”,它将移动到列表中的下一个IP?

任何帮助,将不胜感激。 我是新手,但似乎是脚本中大规模ssh进程的一个非常好的工具。 只是在调整它时不能将错误输出到1个作业而不是在出错时转移到下一个作业。

I am trying to change the password for a user on a bunch of linux based routers pulling IPs from a list. The iplist.txt is just a list of ips (one per line). This is the code I am trying to use:

#!/usr/bin/expect set timeout 20 #Edit for User set user username #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set hosts [split [read $f] "\n"] close $f foreach host $hosts { spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$host expect "assword:" send "$old\r" expect ">" send "user set $user password=$new\r" expect ">" send "quit\r" expect eof close }

Which works for the first ip on the list but send this error on the second:

spawn_id: spawn id exp4 not open

I got it to work this way as I wanted:

#!/usr/bin/expect set timeout 30 #Edit for User set user user #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set data [read $f] close $f foreach line [split $data \n] { if {$line eq {}} continue spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line expect "assword:" send "$old\r" expect ">" send "user set $user password=$new\r" expect ">" send "\r" expect ">" send "quit\r" send "\r" expect eof }

The next issue I run into is if the device doesn't have the old password or if the linux box cannot reach the device via ssh, it will error out with the same spawn id exp* not open when it gets to that IP and will not continue onto the next IP in the list. Is there anyway I can a statement that says if "assword:" comes up a second time to move onto next IP and if ">" comes up like its supposed to keep going with the script, then add a line after the spawn command that will move to the next IP in list if it doesn't receive the first expect "assword:"?

Any help would be appreciated. I am new to expect, but seems to be a really good tool for mass ssh processes in a script. Just having trouble tweaking it to not error out on 1 job instead of moving to next job upon error.

最满意答案

#!/usr/bin/expect set timeout 30 #Edit for User set user user #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set data [read $f] close $f foreach line [split $data \n] { if {$line eq {}} continue spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line expect { "assword:" { send "$old\r" expect { "assword:" { close continue }} expect { "*" { send "user set $user password=$new\r" expect ">" send "quit\r" close continue }}}} expect { "*" { close continue }} expect eof }

可能有点脏脚本,但它的工作原理。 现在,如果我能弄清楚如何导出成功,错误的密码和超时日志,那么我知道是否有任何错误输出。

#!/usr/bin/expect set timeout 30 #Edit for User set user user #Edit for Old Password set old oldpassword #Edit for New Password set new newpassword #get IP List from iplist.txt set f [open "/iplist.txt"] set data [read $f] close $f foreach line [split $data \n] { if {$line eq {}} continue spawn -noecho ssh -q -o "StrictHostKeyChecking=no" $user@$line expect { "assword:" { send "$old\r" expect { "assword:" { close continue }} expect { "*" { send "user set $user password=$new\r" expect ">" send "quit\r" close continue }}}} expect { "*" { close continue }} expect eof }

Probably a bit dirty scripting, but it works. Now if I can figure out how to export successful, wrong password, and timeout logs so I know if any error-ed out.

更多推荐

本文发布于:2023-04-29 02:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   期待   文件   Expect   File

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!