Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to su to root after telnet on Unix 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I am using the Net::Telnet module to log into a remote machine and run some commands. My problem is the commands need to be run as root. I am looking for a way to su to root after I telnet into the system as my user id, and then run the commands as root.

Here is my code so far:

#!/opt/perl/bin/perl

use Net::Telnet;
@arr = qw (mercury aurora apollo);
foreach (@arr) {
$host = $_;
$telnet = new Net::Telnet ( Timeout => 10,
Errmode => 'die',
Prompt => '/\$ $/i');
$telnet->open ("$host.hphc.org");
$telnet->login ('xxxxx', 'xxxxx');
@arr2=$telnet->cmd ('opcagt -status');
print "@arr2\n";
}
 
Hmmmm.

Considering that the telnet command basically runs a session, you can do it just as you normally would from console, I would think.

Make sure you change the permissions on your script so that no one can access it to see what the root password is.
That being said, $telnet->cmd('su'); should get you ready, and then use waitfor and use a regular expression, ie. /password: $/i, and then go ahead and use print to get the password in there.

Should be good to go after that.

I will say though, there are MUCH better ways of doing this than having it telnet in.

Why not have your script simply execute another script, that will be hosted on that box, that would be much more efficient and secure.

But then again, if this is a router, or some other passive device that hasn't got an OS capable of scripting -- then I understand, but even then so, most routers and other devices have special API's and protocols for passing commands to them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top