title: Create a Telnet connection via SSH using Ruby
body: <p>I am having a slight issue. First and foremost, I had never written a single line of Ruby until being given this task so it is somewhat outwith my remit and comfort zone. This may well be a stupid question but I have searched for what feels like an eternity and found nothing.</p>
  
  <p>I have found a wealth of information on how to telnet from my local machine using Net::Telnet and how to create a Telnet session based on the current SSH session using Net::SSH:Telnet but haven't found anything specifically dealing with my issue.</p>
  
  <p>I need to SSH onto a machine using a set of credentials; this is no issue:</p>
  
  <pre><code>objSsh=Net::SSH.start("xxx.xxx.xxx.xx",
                          "sshUser", 
                          :Password=&gt;"sshPass")
  </code></pre>
  
  <p>The above works a treat and I can get, for example, the output from ls (I know, the simplest of simpleton tasks).</p>
  
  <p>The issue that I am having is that I need to Telnet from the SSH session using different credentials, so the below does not fit my purpose:</p>
  
  <pre><code>ssh = Net::SSH.start("xxx.xxx.xxx.xxx",
        :username=&gt;"sshUser",
        :password=&gt;"sshPass"
  )
  s = Net::SSH::Telnet.new(
        "Dump_log" =&gt; "/dev/stdout",
        "Session" =&gt; ssh
  )
  </code></pre>
  
  <p>That would Telnet using the same credentials and so is not what I require.</p>
  
  <p>But at the same time I cannot use the below because it would just try to Telnet from the executing machine, which is not what is required.</p>
  
  <pre><code>s = Net::SSH::Telnet.new(
      "Dump_log" =&gt; "/dev/stdout",
      "Host" =&gt; "0",
      "Username" =&gt; "telnetUser",
      "Password" =&gt; "telnetUser"
  )
  </code></pre>
  
  <p>Basically, I need to SSH to a machine then, using different credentials, Telnet to the localhost using "telnet 0 xxxxx" but can't figure out for the life of me how to do it.</p>
  
  <p>I've tried running telnet 0 xxxxx at the ssh prompt but that just hangs, presumably because the username/password prompts are shown.</p>
  
  <p>I am wandering in confused circles, here. Any assistance with regards to a kick in the right direction would be appreciated.</p>
  
  <p>Thanks,</p>
  
  <p>CJL.</p>
  
  <p>Update 1: </p>
  
  <p>Thank you cherrysoft for your rapid and helpful replies. I'll look into whether or not we are able to chuck files onto their servers to trigger via SSH directly. It would make life simpler!</p>
  
  <p>Update 2:</p>
  
  <p>OK, so in desperation I tried something and it got me a step further than I was getting before. The following code connects via SSH, creates a Telnet session based on the SSH, ensures everything is still working by outputting a simple SSH command, tries to kick off a Telnet session....and then fails.</p>
  
  <pre><code>#Create SSH connection.
  objSsh=Net::SSH.start("xxx.xxx.xxx.xxx",
                      "sshPass", 
                      :password=&gt;"sshPass")
  #Crete a Net:SSH:Telnet object based on the SSH connection.
  objTelnet = Net::SSH::Telnet.new("Session" =&gt; objSsh)
  #Ensure simple SSH commands can still be run by outputting ls results.
  p objTelnet.cmd("ls")
  #Try to kick off a telnet session and show what is displayed.
  p objTelnet.cmd("telnet 0 xxxxx")
  #Wait for the Username prompt.
  objTelnet.waitfor("Match" =&gt; /.*?Username:.$/)
  #Enter username.
  objTelnet.puts("telnetUser")
  #Wait for password prompt.
  objTelnet.waitfor("Match" =&gt; /.*?Password:.$/)
  #enter password.
  objTelnet.puts("telnetPass")
  #Try the command.
  p objTelnet.cmd("cluster stats")
  </code></pre>
  
  <p>It gets as far as the Telnet connection but the Waitfor calls don't seem to be working. When the below line runs the result in the output is a prompt for Username:</p>
  
  <pre><code>objTelnet.cmd("telnet 0 xxxxx")
  
  "telnet 0 xxxxx\nTrying 0.0.0.0...\r\nConnected to 0.\r\nEscape character is '^]'.\r\nUsername: "
  </code></pre>
  
  <p>I would have expected the Waitfor to match what was shown in the output and for the Username then to be entered. Is this possibly a RegEx issue? I have run it through Rubular and it seems to match but I may be missing something syntax related?</p>
  
  <p>Thanks again for your time.</p>
  
  <p>CJL.</p>
  
  <p><strong>UPDATE 3 AND RESOLUTION:</strong></p>
  
  <p>Thank you Kimmo Lehto. With only minor tweeking that worked perfectly. It was still getting stuck at Telnet loggin but when passing credentials as shown below (as opposed to user/pass) it works a treat!</p>
  
  <pre><code>loginInfo = {"Name" =&gt; telnetUser,
            "Password" =&gt; telnetpass,
            "LoginPrompt" =&gt; /Username:/
           }
  objGateway = Net::SSH::Gateway.new(hostIp, user, :password =&gt; password)
  objLocalPort = objGateway.open("localhost", port)
  objTelnet = Net::Telnet::new("Host" =&gt; "127.0.0.1", "Port" =&gt; objLocalPort)
  objTelnet.login(loginInfo) 
  objTelnet.cmd("cluster lrm res sum") { |c| print c}
  objTelnet.close
  objGateway.shutdown!
  </code></pre>
  
  <p>Thanks again for your time and effort in resolving this issue.</p>
  
  <p>CJL out...</p>