linux - 用于 cisco 设备静态路由更改的 Linux sh+expect 脚本
问题描述
我有以下脚本,现在我在使用此脚本的一些网络更改中遇到了以下问题。
.sh 脚本
echo -n "Enter the SSH username :"
read -e username
echo -ne '\n'
echo -n "Enter the SSH password :"
read -s -e password
echo -ne '\n'
for device in `cat /var/scripts/list.txt`; do
/var/scripts/config_changer_multi_device/configure-cisco.exp $device $password $username ;
done
并遵循 .expect 脚本:
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set username [lindex $argv 2]
set password [lindex $argv 1]
send_user "\n"
send_user ">>>>> Working on $hostname @ [exec date] <<<<<\n"
send_user "\n"
spawn ssh -o StrictHostKeyChecking=no $username\@$hostname
expect {
timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
"*#" {}
"*assword:" {
send "$password\n"
}
}
expect "*#"
send "enable\n"
expect "*#"
send "conf t\n"
expect "(config)#"
send "ip route 8.8.8.8 255.255.255.255 $gateway\n"
expect "(config)#"
send "exit\n"
expect "*#"
send "write mem\n"
expect "*#"
send "exit\n"
expect ":~\$"
exit
我运行 .sh 脚本,为 ssh 连接提供用户登录名和密码。当我有静态网关而不是变量时,这工作正常。我想要的是在静态路由添加的地方有变量 $gateway 从文件中获取值。例如,如果我有路由器 IP 为 10.100.100.50 的文件 list.txt,我希望脚本也从文件 gateway.txt 中获取第一个值,然后从 list.txt 和 gateway.txt 中获取第二个位置,依此类推,因为在每个路由器上,网关 IP 都是另一个.
提前感谢您的帮助。
解决方案
也许你想要
#!/usr/bin/env bash
read -p "Enter the SSH username : " username
read -p "Enter the SSH password : " -s -e password
cd /var/scripts # or perhaps: cd "$(dirname "$0")"
while IFS=$'\t' read -r device gateway; do
./config_changer_multi_device/configure-cisco.exp "$device" "$gateway" "$password" "$username"
done < <(
paste ./list.txt ./gateway.txt
)
为了增加一点安全性,密码不会出现在ps
输出中供任何人看到:
#!/usr/bin/env bash
read -p "Enter the SSH username : " username
read -p "Enter the SSH password : " -s -e password
export username password
cd /var/scripts # or perhaps: cd "$(dirname "$0")"
while IFS=$'\t' read -r device gateway; do
./config_changer_multi_device/configure-cisco.exp "$device" "$gateway"
done < <(
paste ./list.txt ./gateway.txt
)
而期望脚本看起来像
#!/usr/bin/expect -f
lassign $argv hostname gateway
set username $env(username)
set password $env(password)
# ...
推荐阅读
- jenkins - 从另一个 Jenkinsfile 运行一个 Jenkinsfile
- php - 为什么在 Laravel 5.6 的编辑视图文件中没有更新图像?
- laravel - 我的 Laravel 雄辩查询有什么问题
- events - Java 事件过滤器占用多少内存?
- git - 相关变更集的 Git 工作流程
- angular - Angular 5 / Jasmine 2 测试 setTimeout 函数只有一次
- excel - 如何在 VBA 中模拟 Python 的“继续”语句的错误 AKA 进行下一次迭代?
- android - 在 ViewPager 中保存 Glide 图像 onClick
- java - 单击链接时如何传输数据?
- angular - 当我将 'e' 字符放入输入 type="number" 时,数字被放入字段 angular