title: How to use Expect inside a Bash script
body: <p>This is the code snippet I am using in <a href="http://codepad.org/4qE7qP5X" rel="nofollow noreferrer">the following</a> Bash script:</p>
  <pre><code>  for user_input in `awk '{print}' testfile_$$.txt`
      do
      ipaddress=`echo $user_input | cut -d';' -f 1`
      command=&quot;${config_mode}`echo $user_input | cut -d';' -f 2-`&quot;
              ping -w 1 $ipaddress 1&gt; /dev/null 2&gt; $ERR_LOG_FILE 1&gt; $LOG_FILE
      if [ $? -eq 0 ];then
              ssh &quot;$USERNAME@$ipaddress&quot; &quot;$command&quot;
                                                    &gt;&gt; $LOG_FILE
  
  
      fi
      done
  </code></pre>
  <p>How do I use Expect to automate the SSH login in this script?</p>
  <p>I am very new to Expect and started testing this (it failed):</p>
  <pre><code>#!/usr/bin/bash
  
  
  set force_conservative 0  ;# Set to 1 to force conservative mode even if
                            ;# script wasn't run conservatively originally
  if {$force_conservative} {
          set send_slow {1 .1}
          proc send {ignore arg} {
                  sleep .1
                  exp_send -s -- $arg
          }
  }
  
  #
  
  set timeout -1
  spawn ssh auto21@10.38.227.229 {uname -a; df -h}
  match_max 100000
  expect &quot;*?assword: &quot;
  send -- &quot;bar01\r&quot;
  expect eof
  </code></pre>
  <p>Do I need to write the Bash script all over again in an Expect script or can Expect be used inside a Bash script?</p>
  <p>If it can be done:</p>
  <p>Moreover, I need to get the Bash variables <strong>$command</strong>, <strong>$username</strong>, <strong>$password</strong>, and <strong>$ipaddress</strong> and use it in the Expect part.</p>
  <p>What solution would you suggest?</p>
  <p>Or can I create an Expect script and call it from the Bash script just for login, error handling, execution, and logfiles.</p>