I have a bash script which can login to Cisco switch and shut/noshut any port I have defined in it.

What I want is that I can add variable, like I can define port number in a command like this:

./cisco.sh 10 #10 is the port number

But the script is not letting me add any variable and gives the following error:

can't read "1": no such variable
    while executing "set PORT "$1""

Here is the code of the script I am using:

#!/usr/bin/expect -f
set timeout 20
set IPaddress ""
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"

Maybe I was too much in a hurry. I had to add

set user [lindex $argv 0]

to get $1 variable added in the script.

Thanks :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.