Ruby script to run command as root on remote linux system

J

James Dinkel

This is really sort of an offshoot of this thread:
http://www.ruby-forum.com/topic/150015

I WAS just trying to pass on the password to sudo being run over
Net::SSH on a remote linux server. I eventually gave up on that and set
sudo to run with no password, only to find out that does not work
either! For some reason there is a problem running sudo commands over
Net::SSH.

So I am just wonder if anybody has found ANY possible way to run remote
commands with root privileges on the remote computer.

here is my script as it is which seems to just skip over the sudo
command:

-----------
require 'rubygems'
require 'net/ssh'


Net::SSH.start( 'server1',
:username=>'myuser',
:password=>'mypass' ) do |session|


session.open_channel do |channel|
channel.on_data do |ch, data|
puts data
end
channel.exec 'touch myfile'
end
session.loop

session.open_channel do |channel|
channel.on_data do |ch, data|
puts data
end
channel.exec 'sudo touch rootfile'
end
session.loop

end
 
P

Peña, Botp

RnJvbTogSmFtZXMgRGlua2VsIFttYWlsdG86amRpbmtlbEBnbWFpbC5jb21dIA0KIyBTbyBJIGFt
IGp1c3Qgd29uZGVyIGlmIGFueWJvZHkgaGFzIGZvdW5kIEFOWSBwb3NzaWJsZSB3YXkgdG8gDQoj
IHJ1biByZW1vdGUNCiMgY29tbWFuZHMgd2l0aCByb290IHByaXZpbGVnZXMgb24gdGhlIHJlbW90
ZSBjb21wdXRlci4NCg0KdHJ5IGNhcGlzdHJhbm8NCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=
 
P

princeofnigeria

Depends on what you ar looking to do. Do you have valid credentials on the
remote machine? Do you need to just connect and run commands through a
script and then disconnect? Give me s few more details and Ill see what I
can do. I have a few scripts i use but depends on what your trying to do.
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

I WAS just trying to pass on the password to sudo being run over
Net::SSH on a remote linux server. I eventually gave up on that and set
sudo to run with no password, only to find out that does not work
either! For some reason there is a problem running sudo commands over
Net::SSH.

Strange. It works for me:
|session|
?> session.open_channel do |channel|
?> channel.on_data do |ch, data|
?> puts data
celtic@sohma:~$ ls -l ~test
total 0
-rw-r--r-- 1 test test 0 2008-04-19 14:16 a
lrwxrwxrwx 1 test test 26 2008-04-19 14:14 Examples ->
/usr/share/example-content/
-rw-r--r-- 1 root root 0 2008-04-19 14:17 rootfile
celtic@sohma:~$

Are you sure the user can always sudo without password?

Arlen
 
J

James Dinkel

Ken said:
SSH also allows you to create single-purpose keys.
See http://pkeck.myweb.uga.edu/ssh/

--Ken

That's all good to know. Since Arlen says it is working for him. I
think I'm going to set up another server to test this out on (I don't
want to go much further on a production machine, probably shouldn't have
even messed with it this much).

James
 
J

James Dinkel

Ok, a little more testing, and I think I've found the problem. This is
in the sudoers file on RHEL 5.0 by default:
"Defaults requiretty"

So I either need to get rid of that, or somehow get ruby net-ssh to open
a tty and execute the command in that. I have no clue if the latter is
even possible, and I really don't want to do the former, at least not
globally.

If I could remove the requiretty option for just this user, or even
better would be for just this user AND just this command, then that
would be great, and I bet possible. However, I have no idea how to
configure sudoers for that.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top