title: Can not find channel named "" Expect Script
body: <p>I have two scripts I use in conjunction. This is because I have large amounts of commands that need to be sent to Routers/Switches, in large batches. </p>
  
  <p>Full Disclosure I did not write these scripts, they were written by someone who no longer works with me, and I have very limited knowledge of expect. </p>
  
  <p>The first script (Script A) is what allows me to execute a script on many devices at once from a central box, while outputting the results of the script to a file.</p>
  
  <pre><code>#!/bin/bash
  
  let j=0;
  let sleeptime=20;
  let numberInstance=20;
  if [ $# -lt "3" ]
  then
      echo "Missing Parameters:"
      echo "start &lt;script-name&gt; &lt;output-directory&gt; &lt;sites-file&gt;"
      exit 1
  fi
  
  
  for s in `cat ${3}`
  do
  
          echo Processing: $s on `date`;
          ./$1 ${s}${4}  &gt; ./$2/${s}-${4} &amp;
          let j+=1;
  
  
  if [ `expr $j % $numberInstance` -eq 0 ]
          then
  echo sleeping for ${sleeptime} seconds...sites processed: $j;
                  sleep $sleeptime;
  fi
  done
  </code></pre>
  
  <p>The second script (Script B) is an expect script that spawns and SSH session to a device and sends a list of commands to the device.</p>
  
  <p>This works just fine but in cases where it cannot reach the device Script A returns an error from Script B:</p>
  
  <pre><code>$ ./start_sw_FAST LH108_S2 Results/LH108_S2 inputlist   
  Processing: Site1 on Thu Mar 19 14:31:07 EDT 2020
  Processing: Site2 on Thu Mar 19 14:31:07 EDT 2020
  Processing: Site3 on Thu Mar 19 14:31:07 EDT 2020
  Processing: Site4 on Thu Mar 19 14:31:07 EDT 2020
  $ can not find channel named ""
      while executing
  "send "edit\r""
      (file "./LH108_S2" line 69)
  can not find channel named ""
      while executing
  "send "edit\r""
      (file "./LH108_S2" line 69)
  can not find channel named ""
      while executing
  "send "edit\r""
      (file "./LH108_S2" line 69)
  </code></pre>
  
  <p>In this case I know that these locations will fail. But when I am running this on hundreds at a time, i'd like for the prompt to be able to say something like:</p>
  
  <pre><code>Failed to execute script on Site1.
  Failed to execute script on Site2.
  Failed to execute script on Site3.
  Failed to execute script on Site4.
  </code></pre>
  
  <p>I'm not sure how I can make Script A aware of Script B's failures.</p>
  
  <p>Expect Script:</p>
  
  <pre><code>#!/usr/bin/expect
  
  proc _jgetRouterIPs {} {
  set file [open router-list r]
    while {![eof $file]} {
                 set buff [read $file ]
                }
  close $file
  return $buff
  }
  
  proc ssh2rtr { rtr usr pwd } {
   spawn ssh -l $usr $rtr;
  set sess_id $spawn_id;
  set ppret [ processPrompts $rtr $usr $pwd $sess_id 0 ];
  if { $ppret == 0 } {
  return $spawn_id;
  else { return -1;}
  }}
  
  
  proc processPrompts { rtr usr pwd sess_id status } {
        set timeout 666;
        set spawn_id $sess_id;
        set fw_prompt  {\nuser@[0-9]{4}\-[A-Z]{2}-S2[&gt;|#]{1}};
        set fwprompt1  {user@[0-9]{4}-[A-Z]{2}-S2\%\s};
  
  if { $status &lt; 2 } {
        expect {
        -re $fw_prompt { return 0;}
        -re $fwprompt1 {send "cli\r"; return 0;}
  
        "no)?" {  send "yes\r";
         processPrompts $rtr $usr $pwd $sess_id 0;
          }
  
  
        "assword:" {
                  send "${pwd}\r";
         if { $status} { return 1;}
         processPrompts $rtr $usr $pwd $sess_id 1;
  
          }
  
       timeout     { send_user "\nTimeout on SSH!!!\n"; return 8; } ;#no response from telnet/ssh
       default     { send_user "\nSSH Unknown Error!!!\n"; return 9;  } ;#unknown error has occurred.
  } ;#end expect 
  } ;#end if
  }
  
  
  
  set site [lindex $argv 0];
  set sw1 "${site}s1";
  set sw2 "${site}s2";
  set fw "${site}fw1";
  set fw_prompt  {\nuser@[0-9]{4}\-[A-Z]{2}-S2[&gt;#]{1}};
  set fw_prompt1  {user@[0-9]{4}-[A-Z]{2}-S2%\s};
  
  set sshret  [ ssh2rtr $sw2 user NotMyPassword ]
  if { $sshret != -1 } {
                  set spawn_id $sshret;
  } else {
                  send_user "Error Connecting...!!!";
  }
          set timeout 666;
  
  
  send "edit\r";
  expect -re $fw_prompt;
  send "rollback\r";
  expect -re $fw_prompt;
  *more commands will go here*
  </code></pre>