How to su in a script if su needs a pswd ?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default How to su in a script if su needs a pswd ?

    Hello,

    I am running a command as part of my script on a Unix box for which I need
    to su to a different username.

    This su requires a password as well.

    I could not find any option of passing the password in su.

    Tried the shell script solution of here docs :

    su <<TillEnd
    admin
    test
    TillEnd

    But even this does not work.

    Could anyone please guide me how to su in a script (shell / perl) ?

    I cannot have my username added to the /etc/sudoers to execute commands as
    root...

    Thanks in advance
    -aman


    Aman Guest

  2. Similar Questions and Discussions

    1. 6.1 script not working on 7.0 server. Script used towork!
      I've a problem with some coldfusion 6.1 scripts running on a server with coldfusion 7. It seems that it isn't accepting the hidden type for the tag...
    2. CF Grid / Java Script / Action Script
      Hi, Does anyone know of a good reference for the attributes CFgrid exposes in a flash form? eg. I would like to select the first row on load. ...
    3. ksh script - Preventing multiple runs of a particular script
      I am having trouble with a ksh script I am writing. I need to make sure that the script is not already running when it is kicked off in cron. ...
    4. Batch Script returns "Could Not Run script - File was not found"
      I need to batch process some images to convert them to jpgs. When I try to set it up I get "Could Not Run script - File was not found". This also...
    5. Execute shell script from a perl script
      Hi, How can I executed a Unix shell script from a Perl script. The shell script is a dump of a oracle table to a file. The perl script is for...
  3. #2

    Default RE: How to su in a script if su needs a pswd ?

    > Hello, 

    Take a look at the Expect module. Here is some code from a script i did a
    long time ago. It was a wrapper script for su. It may help:
    --
    use strict;
    use Expect;
    use IO:Socket;

    # grab the user if entered
    my $user = $ARGV[0] if $ARGV[0];
    my $pw;

    # might fail if it's su -
    if ($user && $user !~ /root/) {
    real_su();
    }
    # &real_su unless $user =~ /root/;

    print "Password: ";
    $pw = <STDIN>;
    chomp $pw;

    # send the captured password to be cyphered
    cypher($pw);

    # tunnel($string);

    # now that we cypher and sent the pw to us go ahead and run the

    &real_su();


    sub real_su
    {

    my $su = Expect->spawn('/bin/su',$user) or die "Can't start Program:
    $!\n";
    # stop the output from going to STDOUT
    $su->log_stdout(0);
    # capture pw and die with appropriate string if pw is wrong
    my $error = ($su->expect(10, 'password:'))[1];
    if ($error)
    {
    print $su "Sorry.\r";
    print STDOUT "Sorry.\r";
    $su->soft_close();
    }
    print $su "$pw\r";
    $su->soft_close();
    exit 0;
    }

    ........



    James Guest

  4. #3

    Default Re: How to su in a script if su needs a pswd ?

    >>>>> "Aman" == Aman Thind <co.in> writes:

    Aman> I am running a command as part of my script on a Unix box for which I need
    Aman> to su to a different username.

    Aman> This su requires a password as well.

    A question just like this was posted to the Perl-Beginners Yahoo
    mailing list.

    If this was you, your question was already asked and answered,
    and should not be re-asked in a different forum. You're wasting
    valuable resources.

    If it wasn't you, please look at the archives, located at
    <http://groups.yahoo.com/group/perl-beginner/messages>, and search for
    the subject lines similar to what you asked here.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal Guest

  5. #4

    Default Re: How to su in a script if su needs a pswd ?



    Can you 'setuid' the script that needs to be called with the special
    privileges? Then shell out to that script for the processing it needs
    to do...

    I am not much of a setuid expert but this should work based on my
    understanding of it.

    http://danconia.org

    Wiggins Guest

  6. #5

    Default RE: How to su in a script if su needs a pswd ?

    Hi James,

    Thanks for the pointer to the Expect module.

    I'll set it up and hope I get it working...else I'll come running back to
    you :p

    Can I establish an SSH connection and do something like Sshexec and Scp
    tasks do in ant 1.6.x using Expect ?

    Thanks
    -aman

    -----Original Message-----
    From: Kipp, James [mailto:com]
    Sent: 17 February 2004 19:35
    To: 'Thind, Aman'; Perl Beginners
    Subject: RE: How to su in a script if su needs a pswd ?

     

    Take a look at the Expect module. Here is some code from a script i did a
    long time ago. It was a wrapper script for su. It may help:
    --
    use strict;
    use Expect;
    use IO:Socket;

    # grab the user if entered
    my $user = $ARGV[0] if $ARGV[0];
    my $pw;

    # might fail if it's su -
    if ($user && $user !~ /root/) {
    real_su();
    }
    # &real_su unless $user =~ /root/;

    print "Password: ";
    $pw = <STDIN>;
    chomp $pw;

    # send the captured password to be cyphered
    cypher($pw);

    # tunnel($string);

    # now that we cypher and sent the pw to us go ahead and run the

    &real_su();


    sub real_su
    {

    my $su = Expect->spawn('/bin/su',$user) or die "Can't start Program:
    $!\n";
    # stop the output from going to STDOUT
    $su->log_stdout(0);
    # capture pw and die with appropriate string if pw is wrong
    my $error = ($su->expect(10, 'password:'))[1];
    if ($error)
    {
    print $su "Sorry.\r";
    print STDOUT "Sorry.\r";
    $su->soft_close();
    }
    print $su "$pw\r";
    $su->soft_close();
    exit 0;
    }

    ........


    Aman Guest

  7. #6

    Default RE: How to su in a script if su needs a pswd ?

    Hello Randal,

    In your -

    If ( aman eq "same person who asked in a diff forum")
    {
    print "JERK";
    }
    Else
    {
    print "Valuable archive of answers to similar question" ;
    }

    I pass through the else block as you had earlier answered to one Joe
    Catanzaro though his question was strikingly similar to mine.

    One of the most valuable resources that I would be wasting by asking the
    same question in diff forums for which I've already received the answer is
    my own time !

    Thanks for the link though...the discussion was enlightening indeed.

    -aman

    -----Original Message-----
    From: com [mailto:com]
    Sent: 17 February 2004 20:30
    To: org
    Subject: Re: How to su in a script if su needs a pswd ?

     [/ref][/ref]

    Aman> I am running a command as part of my script on a Unix box for which I
    need
    Aman> to su to a different username.

    Aman> This su requires a password as well.

    A question just like this was posted to the Perl-Beginners Yahoo
    mailing list.

    If this was you, your question was already asked and answered,
    and should not be re-asked in a different forum. You're wasting
    valuable resources.

    If it wasn't you, please look at the archives, located at
    <http://groups.yahoo.com/group/perl-beginner/messages>, and search for
    the subject lines similar to what you asked here.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
    training!

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>

    Aman Guest

  8. #7

    Default Re: How to su in a script if su needs a pswd ?

    >>>>> "Aman" == Thind, Aman <co.in> writes:

    Aman> I pass through the else block as you had earlier answered to one Joe
    Aman> Catanzaro though his question was strikingly similar to mine.

    I just find it odd that nobody asks a question like that for at least
    six months in my recollection, and then it pops up twice within two
    days in two different similar forums. So shoot me. :)

    More often than not, it's someone who is interested in wasting a lot
    of free resources.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal Guest

  9. #8

    Default RE: How to su in a script if su needs a pswd ?

    :)
    By the way your stress on avoiding to store the pswd in any readable disk
    file really made me look at my problem and possible solutions from a whole
    new perspective...

    Cheers !
    -aman

    -----Original Message-----
    From: com [mailto:com]
    Sent: 17 February 2004 21:53
    To: Thind, Aman
    Cc: org
    Subject: Re: How to su in a script if su needs a pswd ?

     [/ref][/ref]

    Aman> I pass through the else block as you had earlier answered to one Joe
    Aman> Catanzaro though his question was strikingly similar to mine.

    I just find it odd that nobody asks a question like that for at least
    six months in my recollection, and then it pops up twice within two
    days in two different similar forums. So shoot me. :)

    More often than not, it's someone who is interested in wasting a lot
    of free resources.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
    training!
    Aman Guest

  10. #9

    Default RE: How to su in a script if su needs a pswd ?

    Please bottom post....
     

    You could, but in the case of SSH you would be better off using
    Net::SSH::Perl or Net::SFTP. Expect should be an absolute last resort,
    screen scraping is fine when there is no other published interface, in
    the case of SSH that isn't the case.

    http://danconia.org
     
    >
    > Take a look at the Expect module. Here is some code from a script i did a
    > long time ago. It was a wrapper script for su. It may help:
    > --
    > use strict;
    > use Expect;
    > use IO:Socket;
    >
    > # grab the user if entered
    > my $user = $ARGV[0] if $ARGV[0];
    > my $pw;
    >
    > # might fail if it's su -
    > if ($user && $user !~ /root/) {
    > real_su();
    > }
    > # &real_su unless $user =~ /root/;
    >
    > print "Password: ";
    > $pw = <STDIN>;
    > chomp $pw;
    >
    > # send the captured password to be cyphered
    > cypher($pw);
    >
    > # tunnel($string);
    >
    > # now that we cypher and sent the pw to us go ahead and run the
    >
    > &real_su();
    >
    >
    > sub real_su
    > {
    >
    > my $su = Expect->spawn('/bin/su',$user) or die "Can't start Program:
    > $!\n";
    > # stop the output from going to STDOUT
    > $su->log_stdout(0);
    > # capture pw and die with appropriate string if pw is wrong
    > my $error = ($su->expect(10, 'password:'))[1];
    > if ($error)
    > {
    > print $su "Sorry.\r";
    > print STDOUT "Sorry.\r";
    > $su->soft_close();
    > }
    > print $su "$pw\r";
    > $su->soft_close();
    > exit 0;
    > }
    >
    > .......
    >
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]


    Wiggins Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139