通过预期在bash中通过SCP更新IOS(Updating IOS's via SCP in bash with expect)

编程入门 行业动态 更新时间:2024-10-27 13:26:36
通过预期在bash中通过SCP更新IOS(Updating IOS's via SCP in bash with expect)

美好的一天。 我正在尝试创建/运行一个脚本,允许我将更新的IOS从服务器发送到我的网络设备。 当我在“:flash”命令之前输入手动IP地址时,以下代码有效。

#!/user/bin/expect set IOSroot "/xxxxx/xxx/c3750e-universalk9-mz.150-2.SE10a.bin" set pw xxxxxxxxxxxxxxxxxxx spawn scp $IOSroot 1.1.1.1:flash:/c3750e-universalk9-mz.150-2.SE10a.bin expect "TACACS Password:" send "$pw\r" interact

那里的代码很好,并且符合预期。 当我尝试使用名为“ioshost”的文件和IP列表并在此脚本中使用它来获得一些自动化时,会出现问题。 我尝试过各种各样的东西让它发挥作用。 其中一些如下:

设置变量

IPHosts=$(cat ioshost) set IPHost 'cat ioshost'

一起尝试使用read / do命令......

while read line; do spawn scp $IOSroot $line:flash:/c3750e-universalk9-mz.150-2.SE10a.bin done < ioshost

这些似乎都不起作用,我正在寻找指导。 请注意我明白设置密码不是最佳做法,但不允许设置其他文章中提到的RSA密钥,所以我不得不这样做。

感谢您的时间。

Good day. I am attempting to create/run a script that will allow me to send an updated IOS from a server to my network devices. The following code works when I put in a manual IP address right before the ":flash" command.

#!/user/bin/expect set IOSroot "/xxxxx/xxx/c3750e-universalk9-mz.150-2.SE10a.bin" set pw xxxxxxxxxxxxxxxxxxx spawn scp $IOSroot 1.1.1.1:flash:/c3750e-universalk9-mz.150-2.SE10a.bin expect "TACACS Password:" send "$pw\r" interact

The code there works great and as expected. The issue arises when I try to use a file called "ioshost" with a list of IP's and use that within this script to get some automation. I have tried various things to get this to work. Some of them are as follows:

Settings Variables

IPHosts=$(cat ioshost) set IPHost 'cat ioshost'

Along with trying to use the read/do command...

while read line; do spawn scp $IOSroot $line:flash:/c3750e-universalk9-mz.150-2.SE10a.bin done < ioshost

None of these seem to work and I am looking for guidance. Please note I understand that setting a password is not best practice but setting RSA keys as mentioned in other articles is not allowed so I am forced to do it this way.

Thank you for your time.

最满意答案

您可以使用一个Expect脚本和一个Bash脚本。

首先更新您的Expect脚本:

#!/user/bin/expect set IOSroot "/xxxxx/xxx/c3750e-universalk9-mz.150-2.SE10a.bin" set pw xxxxxxxxxxxxxxxxxxx spawn scp $IOSroot [lindex $argv 0]:flash:/c3750e-universalk9-mz.150-2.SE10a.bin # ^^^^^^^^^^^^^^^^ expect "TACACS Password:" send "$pw\r" interact

然后写一个简单的Bash for循环:

for host in $(<ioshost); do expect /your/script.exp $host done

You can use one Expect script and one Bash script.

First update your Expect script a bit:

#!/user/bin/expect set IOSroot "/xxxxx/xxx/c3750e-universalk9-mz.150-2.SE10a.bin" set pw xxxxxxxxxxxxxxxxxxx spawn scp $IOSroot [lindex $argv 0]:flash:/c3750e-universalk9-mz.150-2.SE10a.bin # ^^^^^^^^^^^^^^^^ expect "TACACS Password:" send "$pw\r" interact

Then write a simple Bash for loop:

for host in $(<ioshost); do expect /your/script.exp $host done

更多推荐

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

发布评论

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

>www.elefans.com

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