我有一个 bash 脚本,可以登录到 Cisco 交换机并关闭/noshut 我在其中定义的任何端口。
我想要的是我可以添加变量,就像我可以在这样的命令中定义端口号:
./cisco.sh 10 #10 is the port number
但该脚本不允许我添加任何变量并给出以下错误:
can't read "1": no such variable
while executing "set PORT "$1""
这是我正在使用的脚本的代码:
#!/usr/bin/expect -f
set timeout 20
set IPaddress "192.168.0.1"
set Username "zaib"
set Password "zaib"
set PORT "$1"
spawn ssh -o "StrictHostKeyChecking no" $Username@$IPaddress
expect "*assword: "
send "$Password\r"
expect ">"
send "enable\r"
expect "*assword: "
send "$Password\r"
send "conf term\r"
send "interface gigabitEthernet 1/0/$PORT\r"
expect "#"
send "shut\r"
expect "#"
send "exit\r"
expect "#"
send "exit\r"
send "wr\r"
send "exit\r"
exit
答案1
也许我太着急了。我必须添加
set port [lindex $argv 0]
获取$1
脚本中添加的变量。
谢谢 :)