# How do i provide password for su command while using net/telnet in ruby

**URL:** <https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436>\
**Category:** Ruby\
**Created:** [October 14, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436 "2010-10-14T12:22:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 14, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/1 "2010-10-14T12:22:09Z")

</div>

Hii all ,  
Am using Net/TELNET to connect to remote host trying to run su  
command in order to get root privilage this is how i doing

require ‘net/telnet’  
localhost = Net::Telnet::new(“Host” =\> “192.147.217.27”,  
“Timeout” =\> 50,  
“Prompt” =\> /[$%#\>] \z/n)  
localhost.login(“dvsdkrp”, “dvsdkvrp”) { |c| print c }  
localhost.cmd(“cd /home/dvsdkrp/workdir/smruti”) { |c| print c }  
localhost.cmd(“su”) { |c| print c }  
localhost.puts(“passwd”){ |c| print c }

But am getting this error

Password: C:/Ruby/lib/ruby/1.8/net/telnet.rb:552:in `waitfor': timed out while waiting for more data (Timeout::Error) from C:/Ruby/lib/ruby/1.8/net/telnet.rb:679:in`cmd’  
from tel.rb:7

What should i do??

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 14, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/2 "2010-10-14T12:37:49Z")

</div>

On Oct 14, 2010, at 03:22 , Amit T. wrote:

> ```
> Am using Net/TELNET to connect to remote host trying to run su
> 
> ```
> 
> command in order to get root privilage this is how i doing

Please tell me/us that this isn’t across an unsecured network.  
Otherwise, you should switch from telnet to ssh.

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 14, 2010, 8:49pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/3 "2010-10-14T20:49:55Z")

</div>

Well if you are timing out maybe try to thread it.

$ ruby \<\<EOF

> thread = Thread.new { `sleep 5; echo \"Done.\"` }  
> puts “Waiting on telnet to catch it’s slow ass up.”  
> puts thread.value  
> EOF  
> Waiting on the thread…  
> Done.

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 14, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/4 "2010-10-14T22:06:48Z")

</div>

On 10/14/2010 5:22 AM, Amit T. wrote:

> localhost.cmd(“cd /home/dvsdkrp/workdir/smruti”) { |c| print c }  
> What should i do??  
> I want to think that the problem here is that the su program expects to  
> have a terminal so that it can disable echoing what you type for your  
> password and then directly read that password. Driving a telnet session  
> like this does not emulate a terminal and could cause problems for su.  
> I would expect su to error out rather than cause a timeout though.

Are you able to configure sudo on that host? If so you can configure it  
to allow your account to run the desired command(s) without a password  
and then invoke sudo instead of su.

-Jeremy

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 14, 2010, 12:45pm UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/5 "2010-10-14T12:45:57Z")

</div>

Ryan D. wrote in post #950107:

> On Oct 14, 2010, at 03:22 , Amit T. wrote:
> 
> > ```
> > Am using Net/TELNET to connect to remote host trying to run su
> > 
> > ```
> > 
> > command in order to get root privilage this is how i doing
> 
> Please tell me/us that this isn’t across an unsecured network.  
> Otherwise, you should switch from telnet to ssh.

Network security is not an issue for me and i just want to make su  
command to work

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 16, 2010, 3:16am UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/6 "2010-10-16T03:16:54Z")

</div>

On 10/15/2010 1:10 AM, Amit T. wrote:

> > Are you able to configure sudo on that host? If so you can configure it  
> > }  
> > localhost.cmd(“passwd”)  
> > Now its working fine

Actually, your solution of looking for su’s prompt and then sending the  
password as a “command” proves that my assertion was mostly incorrect,  
but I’m glad you found what you were looking for.

-Jeremy

---

<div class="post-metadata">

**Author:** ![amit-singh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9f8e36/32.png) [@amit-singh](https://www.ruby-forum.com/u/amit-singh)\
**Post date:** [October 15, 2010, 8:10am UTC](https://www.ruby-forum.com/t/how-do-i-provide-password-for-su-command-while-using-net-telnet-in-ruby/197436/7 "2010-10-15T08:10:08Z")

</div>

Jeremy B. wrote in post #950260:

> On 10/14/2010 5:22 AM, Amit T. wrote:
> 
> > localhost.cmd(“cd /home/dvsdkrp/workdir/smruti”) { |c| print c }  
> > What should i do??  
> > I want to think that the problem here is that the su program expects to  
> > have a terminal so that it can disable echoing what you type for your  
> > password and then directly read that password. Driving a telnet session  
> > like this does not emulate a terminal and could cause problems for su.  
> > I would expect su to error out rather than cause a timeout though.
> 
> Are you able to configure sudo on that host? If so you can configure it  
> to allow your account to run the desired command(s) without a password  
> and then invoke sudo instead of su.
> 
> -Jeremy

Hi Jeremy  
You are right this is how i solved it

su\_prompt = "Password: "  
localhost.cmd(“String” =\> “su”, “Match” =\> /#{su\_prompt}/) { |c| print  
}  
localhost.cmd(“passwd”)  
Now its working fine
