title: Expect: How can I open a telnet session from an ssh session?
body: <p>I want to write a script that automatically connects me via SSH to a given IP, and after that opens a telnet session from where it just connected.</p>
  
  <p>My expect code till now:</p>
  
  <pre><code># $1 = ssh root@111.111.111.111
  # $2 = password
  # $3 = telnet 123.123.123.123 10023
  
  (expect -c "
              set timeout 20
              spawn $1
              expect {
                      \"Password:\" { send \"$2\r\" }
                      timeout { send_error \"\nTimeout!\n\"; exit 1; } 
              }
              spawn $3
              interact
              "  )
  </code></pre>
  
  <p>My problem is that I cannot spawn the telnet in the ssh session, the script is just "telnetting" from my home directory. Maybe there is a way with session ids, but I could not find helpful information.</p>
  
  <p>Would be nice if someone of you could suggest some solution or workaround,
  thanks in advance and please excuse my bad English skills :)</p>
  
  <p>Edit:</p>
  
  <p>What helped with my problem, was:</p>
  
  <pre><code>(expect -c "
              set timeout 20
              spawn ssh root@server telnet server2
              expect {
                      \"Password:\" { send \"$2\r\" }
                      timeout { send_error \"\nTimeout!\n\"; exit 1; } 
              }
              interact
              "  )
  </code></pre>