title: Sending Commands through Telnet via Bash Script
body: <p>I am attempting to automate a telnet session in Debian, but I am having difficulty when it comes to sending commands.</p>
  
  <p>By looking <a href="https://stackoverflow.com/questions/7013137/automating-telnet-session-using-bash-scripts">here</a>, I had a good understanding of what I needed to do.</p>
  
  <pre><code>spawn telnet localhost
  expect "login:"
  send "test\n"
  expect "Password:"
  send "password\n"
  sleep 10 //Time for the connection to be established
  send "ls\n"
  </code></pre>
  
  <p>Running the script, it correctly inputed the username.  It got to the password field and i'm assuming it entered it in (though I can't be for sure since passwords are left blank in Debian when entered).  It stalled for the wait, then brought me back to my account (root).  It did not ever connect to "test".</p>
  
  <p>Modifying my code to be more like my reference:</p>
  
  <pre><code>spawn telnet localhost
  expect "login:"
  send "test\n"
  expect "Password:"
  send "password\n"
  interact
  </code></pre>
  
  <p>This led to it immediately logging in and connecting in my "test" account.
  From here I tried adding commands after interact using send, echo, and just typing the command.  Nothing would happen until I exited the telnet session, then I would get this error.</p>
  
  <p>For send:</p>
  
  <pre><code>send: spawn id exp6 not open
      while executing
  "send "ls\n""
      (file ".telnet.sh" line 8)
  </code></pre>
  
  <p>For echo:</p>
  
  <pre><code>invalid command name "echo"
      while executing
  "echo "ls\n""
      (file ".telnet.sh" line 8)
  </code></pre>
  
  <p>With just the command name "ls"
      invalid command name "ls"
          while executing
      "ls"
          (file ".telnet.sh" line 8)</p>
  
  <p>I realize interact gives control back to the user, but for some reason it is the only way it will actually have me connect into the account.</p>
  
  <p>Does anyone know why it is having trouble connecting without interact?  If not, why I cannot send those commands until the telnet session has ended with interact?</p>
  
  <p><strong>EDIT</strong>
  After some research I found this <a href="http://forums.anandtech.com/showthread.php?t=2325260" rel="nofollow noreferrer">link</a>, which at the end comes to a resolve that an expect script ends immediately after everything is finished, closing the telnet session.  They said they fixed the issue by adding a expect "$ " to let the script know more commands are being sent its way.  I tried just using the '$' symbol like the site suggested, but it ended kicking me again.  So I tried this.</p>
  
  <p>Here is my updated code:</p>
  
  <pre><code>#!/usr/bin/expect
  spawn telnet localhost
  expect "debian login"
  send "test\n"
  expect "Password:"
  send "password\n"
  expect "test@debian:~$"
  send "ls\n"
  </code></pre>
  
  <p>Now I am still in the telnet session for about 10 seconds, and then it kicks me off.  If I try to enter in a command in that time, it pauses for that time, then runs the command I typed after the telnet session closes.</p>
  
  <p>Progress is being made.  Slowly but surely.</p>