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

Hi Monks,

I've been trying to create a simple script to auto login to a remote machine, send "sudo -s" and then give control back to the user in an interactive session. Basically I don't want to have to type "sudo -s" each time I login to a device....and yes I know keeping plain text passwords etc is not a good thing!

The problem is that I can login and hand off an interactive session to the user just fine, but I can not automatically send "sudo -s" and attach it to my interactive $ssh file handle. My current working code (without any sudo command) is:
#!/usr/bin/perl use Net::SSH::Perl; use Term::ReadKey; $ip=$ARGV[0]; $user = "username"; $pass = "password"; print "connecting to $ip\n"; my $ssh = Net::SSH::Perl->new("$ip", debug=>"1"); $ssh->login ($user, $pass); ReadMode('raw'); $ssh->shell; ReadMode('restore');
I have tried sending the command prior to "$ssh->shell" and also requesting a pty session during login, but nothing really works. Do any of you monks have an idea how this can be done?

Many thanks

Replies are listed 'Best First'.
Re: Interactive SSH and sudo -s
by ELISHEVA (Prior) on Feb 24, 2009 at 14:34 UTC

    Why do you want to do this, other than the annoyance of typing sudo over and over?

    I know it can be tiresome typing sudo over and over, but avoiding the effort of typing it basically defeats half the purpose of using sudo. One half of the purpose of sudo is to allow users selective access to root-only (or other privileged user) commands. The other half of sudo is to make you conscious each time you are doing something that is potentially risky.

    Best, beth

      Beth - as you said, this is simply to stop repeatedly typing in sudo each time I login. I know its not the best, but for the last seven years I've had root access and now having to use sudo is a real pain and I just want to make life as easy as possible!
        Why don't you just log in as root, then? Running sudo -s will effectively do that. And if the reason is that your ssh is configured to disallow root logins (and many are for security reasons), is doing it via sudo really a good idea? What makes it better doing it via sudo?

        I'd like to hear more on your thoughts about this.

        Best, beth

Re: Interactive SSH and sudo -s
by salva (Canon) on Feb 24, 2009 at 15:33 UTC
    use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host, user => $user, password => $passwor +d); $ssh->system({tty => 1}, 'sudo -s');
Re: Interactive SSH and sudo -s
by hbm (Hermit) on Feb 24, 2009 at 14:36 UTC
      hbm - I don't think this example does what I'm after...but I'll continue to take a look at it. Many thanks for the suggestion.

      If anyone else out there has any ideas please let me know!