title: Expect, Bash and kpcli
body: <p>I have written a script which connects to a local keepass db using kpcli and expect, gets the credentials out of the database and then connects via ssh. The script works but after logging in successfully to the remote host via SSH the session dies after about 5 seconds. </p>
  
  <pre><code> #!/bin/bash
  
   firewall="$1"
   keepass_password="******"
   keepass_db="/media/sf_VM_shared/kdb.kdb"
   keepass_fw_dir="General/Network/Firewalls/SSH"
   firewall_user="admin"
  
  
  echo -e "\n"
  echo "Connecting to keepass Database..."
  
  function get_creds {
    expect &lt;&lt;- DONE
       set timeout 10
       spawn kpcli
       match_max 100000000
       expect  "kpcli:/&gt;"
       send    "open $keepass_db\n"
       expect  "password:"
       send    "$keepass_password\n"
       expect  "&gt;"
       send    "cd $keepass_fw_dir\n"
       expect  "SSH&gt;"
       send    "show -f $firewall\n"
       expect  "&gt;"
  DONE
  
  }
  
  credentials=$(get_creds)
  ssh_info=$(echo "$credentials" | grep 'Title:\|Pass:\|Notes:' | sed -e 's/^.*:       //')
  ip_address=$(echo "$ssh_info" | awk 'NR==3')
  firewall_name=$(echo "$ssh_info" | awk 'NR==1')
  firewall_pass=$(echo "$ssh_info" | awk 'NR==2')
  echo -e "\n"
  echo "------Firewall Information-------"
  echo -e Firewall IP:'\t \t'         "$ip_address"
  echo -e Firewall Name:'\t \t'           "$firewall_name"
  echo -e Firewall Password:'\t'          "$firewall_pass"
  echo "----------------------------------"
  echo -e "\n"
  echo "Connecting to firewall module with user "admin"..."
  
  function ssh_connect {
  expect &lt;&lt;- DONE
  spawn ssh -v -oStrictHostKeyChecking=no -oCheckHostIP=no admin@$ip_address
  expect  "password"
        sleep 5
        send  "$firewall_pass\n"
        expect continue
        expect eof
  
  DONE
  }
  
  ssh_connect
  </code></pre>