Hi I'm trying to use expect for network troubleshooting between two devices.in summary, What I'm trying to achieve is to run a script using expect which enters 2 devices and apply commands.
- the script asks user to enter the 2 hostnames
- Variables user,password and prompt are set
- spawn process to ssh into the first device
- show commands for troubleshooting purposes
All the above works fine....the problem comes when trying to ssh the other hostname, basically my script timeouts after 10 seconds and never ssh the other device.
Thanks in advance to this community for your great work helping
here is the code
#!/usr/local/bin/expect -fexec tput clear >@ stdoutset timeout 10puts "Enter hostname to connect"set c [gets stdin]puts "which is the destination hostname?"set dhostname [gets stdin]#setting password variable, reading it from fileset pa [open "pass.txt" r]set pass [read $pa]close $pa#setting user variableset user xxxx#setting prompt variableset prompt "(%|#|\\$|>|~|:) $"spawn -noecho ssh $user@$c;expect {"Are you sure you want to continue connecting (yes/no)" {send {yes};send "\r"exp_continue}"assword:" { send "$pass\r" expect -re $prompt}}##commands to execute on the devicesend "show system uptime\r";expect "$prompt"send "exit\r";expect -re $promptsend "ssh $user@$dhostname";