Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Solaris Another User Problem

by anshumangoyal (Scribe)
on Dec 19, 2012 at 16:46 UTC ( #1009593=perlquestion: print w/replies, xml ) Need Help??
anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am connected to a production environment with my user-name. From my user i go to a super user account 'superuser' using command "sudo su superuser" and it logs me in as super user. It seems some ssh key authentication is used here and that's why i am able to login without any password.

Now I want to use perl to automatically log me in to this user so that i can fire commmands. When I am logged in as non-superuser, i am not able to issue sudo su superuser command and it returns error.

Is there any way possible that I ssh to my own user and then log in to other user using any SSH Module. I tried net::ssh::expect but there is no success.

Replies are listed 'Best First'.
Re: Solaris Another User Problem
by tobyink (Abbot) on Dec 19, 2012 at 18:23 UTC

    sudo has nothing to do with ssh. It's a program that allows you to run another command (in this case su superuser) as root. sudo can be configured (in /etc/sudoers) to prompt for a password or not.

    In turn, the su superuser bit it being run as root. Normally, su also prompts for a password, but it does not prompt when it's being run by root.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Solaris Another User Problem
by Illuminatus (Curate) on Dec 19, 2012 at 18:25 UTC
    Your description is a little confusing. I need some clarification:
    1. You have a set of commands you need to run on a production machine as user 'superuser' (ie, userid 0)
    2. You can do this interactively by
      • logging onto the production machine as yourself
      • executing 'sudo su superuser'
      • executing the commands
    3. You have attempted to write a perl script to connect to the production machine and do this, but cannot figure out the logic
    it would be helpful if you provided a sample of what you've tried in Net::SSH::Expect. I have made a lot of assumptions here, but if they are correct, it boils down to attempting to execute the commands as 'superuser'. The easiest way I can think of to do this would be something like:
    #! /usr/bin/perl use strict; use warnings; use Fcntl qw( :mode ); # other code here... my $file = "/tmp/su.cmds"; open (CMDS, ">$file"); print CMDS "#! /bin/bash\n"; print CMDS "command 1\n"; print CMDS "command 2\n"; close CMDS; chmod S_IRWXU | S_IXGRP | S_IXOTH, $file; system ("sudo superuser $file"); # script continues
    This script obviously assumes you can run a perl script on the target system. You could just as easily do these using 'shell commands' via Expect (ie create a command file and use 'sudo superuser cmdfile' to run it).


Re: Solaris Another User Problem
by glasswalk3r (Friar) on Dec 19, 2012 at 18:07 UTC

    You should investigate this authentication matter further. It looks like Sudo configuration allows your user to change user without asking for a password.

    To allow the Perl script to work as expected, verify if it is possible that you use SSH keys to connect automatically with the user that you need to. Even better, verify which commands that you need root powers and ask to configure them at the sudoers file, without asking for password.

    Alceu Rodrigues de Freitas Junior
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009593]
Approved by Illuminatus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-08-20 21:44 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (317 votes). Check out past polls.