09-27-2018 05:41 PM - edited 03-01-2019 03:27 AM
I would like to send the following commands to a 3560-CX 8-Port Switch:
enable interface range GigabitEthernet 0/1-8 power inline static no cdp enable exit exit copy running-config startup-config
But prior to being able to enter any commands, I must press enter. Can I send a return carriage in a .txt file to pass to a minicom command?
I tried '/r' and "/r" which did not work. I also tried a simple expect script:
#!/usr/bin/expect -f
send "\r"
Which also did not work.
The goal is to just call the script like such:
minicom -S testScript.sh #or testScript.txt
Any help is greatly appreciated
09-28-2018 04:36 PM
Try this. - change the variable as per your setup.
#!/usr/bin/expect -f
set timeout 20
set 10.10.10.1
set Username "username"
set Password "password"
set Directory /home/Desktop/logs
log_file -a /home/debug.log
spawn ssh -o "StrictHostKeyChecking no" $Username@10.10.10.1
expect "*assword: "
send "$Password\r"
expect "#"
send "enable conf t\r"
expect "(config)#"
send "interface range GigabitEthernet 0/1-8 \r"
expect "(config-if-range)#"
send "power inline static \r"
expect "(config-if-range)#"
send "no cdp enable \r"
expect "(config-if-range)#"
send "end"
expect "#"
send "wr mem\r"
expect "#"
send "exit\r"
sleep 1
exit
10-01-2018 02:27 PM
Thank you for your help!!
I do have a few questions in regard to the code you posted.
I will also comment below the error message I received. I placed comments below each line in your code.
#!/usr/bin/expect -f set timeout 20 set 10.10.10.1 set Username "username" set Password "password
This will be the username and password to login to my RPi running Kali, correct?
set Directory /home/Desktop/logs
I do not have this directory. I have changed the directory to:
/root/Desktop/logs (I made the logs folder using mkdir)
log_file -a /home/debug.log
Does this command create a log file named debug.log?
I changed the path to: /root/debug.log (there is no .log file in the directory even after I run the script)
spawn ssh -o "StrictHostKeyChecking no" $Username@10.10.10.1 expect "*assword: " send "$Password\r" expect "#"
Is there supposed to be a space after the colon?
send "enable conf t\r"
It is at this point that I saved the .sh file and called it using minicom. I wanted to see if the switch> prompt would turn to switch# but it did not.
I actually got the error: "script "testScript.sh" line 4: unknown variable "root"
Any advice?
Thanks again for your help!
10-02-2018 01:02 PM
One of post with more details - hope this helps you.
http://www.balajibandi.com/2015/10/03/cisco-automate-the-config-with-expect-command/
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide