# How to telnet to a host?

**URL:** <https://www.ruby-forum.com/t/how-to-telnet-to-a-host/147943>\
**Category:** Ruby\
**Created:** [September 7, 2008, 11:33am UTC](https://www.ruby-forum.com/t/how-to-telnet-to-a-host/147943 "2008-09-07T11:33:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 7, 2008, 11:33am UTC](https://www.ruby-forum.com/t/how-to-telnet-to-a-host/147943/1 "2008-09-07T11:33:40Z")

</div>

I use this code to telnet to a Windows Xp machine.

tn=Net::Telnet.new(“Host” =\> “localhost”,“Timeout” =\> 10,“Prompt” =\>  
/[$%#\>] \z/n)  
tn.login(“user”, “123456”){|c| print c}

The output will be:

"  
Welcome to Microsoft Telnet Service

login: zhaoyi

password:  
"  
and then timeout.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [September 7, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/how-to-telnet-to-a-host/147943/2 "2008-09-07T12:32:28Z")

</div>

I know what wrong with these code. It is because the password prompt is  
different with the default one. It works when I set the PasswordPrompt  
value. But after login, when I run a command I will get time out error.  
Please see my code:

tn=Net::Telnet.new(“Host” =\> “localhost”,“Timeout” =\> 10,“Prompt” =\>  
/[$%#\>] \z/n){|c| print c}  
tn.login({“Name”=\>“user”,“Password” =\>  
“123456”,“LoginPrompt”=\>/[Ll]ogin[:]_\z/n,“PasswordPrompt”=\>/password[:  
]_\z/n}){|c| print c}

tn.cmd(“dir”){|c| print c}
