# Telnet - attempting to initiate a telnet session within an established telnet session

**URL:** <https://www.ruby-forum.com/t/telnet-attempting-to-initiate-a-telnet-session-within-an-established-telnet-session/122279>\
**Category:** Ruby\
**Created:** [December 6, 2007, 4:29am UTC](https://www.ruby-forum.com/t/telnet-attempting-to-initiate-a-telnet-session-within-an-established-telnet-session/122279 "2007-12-06T04:29:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carcarius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@Carcarius](https://www.ruby-forum.com/u/Carcarius)\
**Post date:** [December 6, 2007, 4:29am UTC](https://www.ruby-forum.com/t/telnet-attempting-to-initiate-a-telnet-session-within-an-established-telnet-session/122279/1 "2007-12-06T04:29:48Z")

</div>

My company builds a product that has a dual slot architecture for  
redundancy. Each slot has 2 significant Ethernet interfaces; one for  
network connectivity and one for connectivity between slots. Only one  
of the slots is accessible from the network at a time (the other is  
meant to be a backup). The two slots communicate via a common  
Ethernet backplane and each slot has a different IP address (10.0.0.9  
and 10.0.0.10).

I am trying to build a script with Ruby that allows me to first telnet  
into the active slot using it’s network address (easy - mission  
accomplished) but then to also telnet into the backup slot over the  
Ethernet backplane within the same telnet session. I want to issue  
commands in order to gather data such as uptime, disk usage, etc…

I am getting hung up on the password prompt when trying to connect to  
the second slot. I can’t seem to send the password info with carriage  
return so I can gain access to the second slot and issue additional  
commands.

The code I have written is in the testing phase and is clearly not  
perfect.

Here is my code:

require ‘net/telnet’

top\_node = ARGV[0]

tn = Net::Telnet::new( “Host” =\> top\_node,  
“Timeout” =\> 10,  
“Prompt” =\> /[$%#\>] \z/n)  
tn.login(“root”, “lab”) { |c| print c }  
tn.cmd(“date”) { |c| print c }  
print ARGV[0]  
print " - "  
tn.cmd("./uptime") { |c| print c }  
test = tn.cmd(“ifconfig eth0 | ifconfig eth0 | awk ‘/inet addr:/  
{print $2 }’”)

if test.include?(“addr:10.0.0.9”)  
tnbackup = “10.0.0.10”  
else  
tnbackup = “10.0.0.9”  
end

print "\n\nTelnetting to Backup HFM " + “(” + tnbackup + “)\n\n”

tn.cmd(“telnet " + tnbackup + " -l root”) { |c| print c }

tn.waitfor(/Password:\s\*/im) { |c| print c }  
tn.puts(“lab”)  
tn.cmd(“ifconfig eth0 | ifconfig eth0 | awk ‘/inet addr:/ {print  
$2 }’”) { |c| print c }  
tn.cmd(“exit”) { |c| print c }  
tn.close

Has anyone successfully telnet into other devices from within an  
established telnet session? Let me know if I am not clear with my  
problem and if I need to clarify it more.

Any help is greatly appreciated.

Thanks,

Heath
