NorthenMonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi This is what I'm trying to do: Using the Net::Telnet module I want to log on to a linux-server and run the shutdown command. Problem: To run the shutdown command I need to be root. To become root I need to run the su - command. But who do I supply the password??? Is there any other ways to shutdown a remote linux server? Thanks

Replies are listed 'Best First'.
Re: shutting down a linux-server
by rob_au (Abbot) on Oct 04, 2002 at 12:26 UTC
    There are a few ways by which you could achieve this functionality ...

    The first would be to continue using Net::Telnet and incorporate usage of the Expect module to respond to the shell prompts with the appropriate commands. The problem with this approach is that the root password of the destination server must be stored in the Perl script and is transmitted over the wire in an unencrypted form.

    There are two ways by which this transmission of the root password in an unencrypted form could be avoided - The first would be to install SSH on the destination server and make use of the Net::SSH::Perl module in place of Net::Telnet. Alternatively, you could install sudo on your destination server which would allow you to assign permissive rights to non-root users to perform privileged tasks.

    I believe your best bet would be to make use of both Net::SSH::Perl and sudo to achieve your desired goal in a direct and secure fashion.

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111000111")),"\n"'

Re: shuting down a linux-server
by zigdon (Deacon) on Oct 04, 2002 at 12:16 UTC

    perhaps you could use sudo? then you can set up a sudoers file, allowing your user to run the shutdown command (as root) without requiring a password.

    As a side note, just thinking of sending the root password over an unencrypted telnet gives me the shivers! Have you thought of using ssh?

    -- Dan

      It's supposed to be used in our internal network. We got several servers running. And in case of a power-failure we want to shutdown the servers before the UPS-battery is empty. So I don't think we will be using ssh.
        Yes! You should use ssh. If you were using ssh you could set up a key pair that allowed the local user to log in as root, on the remote machine, in a secure fashion, without sending any password over the network. ssh is almost always simpler and, not to forget, more secure than any other way. Take the initial hurdle of learning how to set up private/public keys and you will see that everything else becomes much simpler.
Re: shutting down a linux-server
by grinder (Bishop) on Oct 04, 2002 at 16:38 UTC

    A "low-tech" solution to shut down a computer is to create a special account that does just that. Arrange to have your /etc/passwd file to contain an entry like

    doreboot:x:77:77:Reboot Account:/dev/null:/sbin/shutdown -r now

    Now all you have to do is log into the server with this username and the machine will reboot itself, no sudo or anything else needed. Do yourself a favour and give it a difficult password to guess.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: shuting down a linux-server
by semio (Friar) on Oct 04, 2002 at 21:55 UTC
    Hi NorthenMonk

    Although I agree wholeheartedly with other monk's suggestions of using ssh, I believe this will give you what you're looking for.

    #!d:/perl/bin/perl -w use strict; my $username = "dave"; my $passwd = "mypass"; my $debug = "debug"; my $root = "su"; my $rootpass = "myrootpass"; use Net::Telnet (); my $t = new Net::Telnet (Timeout => 10, Dump_Log => $debug, ); $t->open("IP address here"); $t->login($username, $passwd); $t->print($root); $t->waitfor('/Password:/'); $t->print($rootpass); $t->waitfor('/#/'); my @lines = $t->cmd("whoami"); print @lines;
    Dump_Log will be very useful for you as well when working through this problem. Hope this helps.

    cheers, -semio

      Thank you!