Quantcast
Channel: Active questions tagged expect - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 159

using expect to ssh 1 host, apply some commands, exit the device and ssh another device to apply some other commands

$
0
0

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.

  1. the script asks user to enter the 2 hostnames
  2. Variables user,password and prompt are set
  3. spawn process to ssh into the first device
  4. 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";

Viewing all articles
Browse latest Browse all 159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>