|
From: MAGANA, A. S I C. U. A. 72 ABW/S. <and...@us...> - 2015-04-03 15:22:12
|
Okay it works so far and thank you for the help and guidance I have re-read your e-mail about the if elsif statement and I have put it in this way in my script my next goal is to try to have a list or read from different lists of servers. Thank you again so very much getting this far with me.
#!/usr/bin/perl
use strict;
use Expect;
my $timeout = 60;
my $filename = "/var/tmp/expect_script.log";
my $ssh = Expect->new('ssh amagana@remotehost');
$ssh->debug(1);
$ssh->expect ( $timeout,
[ qr/Password:/],
[ qr/Are you sure you want to continue connecting \(yes\/no\)?/]
);
if ($ssh->match() =~ m/Are you sure you want to continue connecting \(yes\/no\)?/ ) {
$ssh->send("yes\r");
}
elsif ($ssh->match() =~ m/Password:/ ) {
$ssh->send("mypassword)^\n");
}
This is my output from the debugging:
Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, 'ARRAY(0x6e6a00)', 'ARRAY(0x6e6a50)') called at ./tester-a line 9
The authenticity of host 'remotehost (x.x.x.x)' can't be established.
RSA key fingerprint is x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x.
Are you sure you want to continue connecting (yes/no)? Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, 'Password:') called at ./tester-a line 22
yes
Warning: Permanently added 'remotehost,x.x.x.x' (RSA) to the list of known hosts.
Password: Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, '$') called at ./tester-a line 25
\e[01m[\u@\h \W ]\e[m $ Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, 'Password:') called at ./tester-a line 27
su - root
Password: Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, '#') called at ./tester-a line 29
root@remotehost #Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, 'New Password:') called at ./tester-a line 31
passwd amagana
New Password: Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, 'Re-enter new Password:') called at ./tester-a line 33
Re-enter new Password: Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0xa310c0)', 60, '#') called at ./tester-a line 35
passwd: password successfully changed for amagana
root@remotehost #Closing spawn id(3).
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 1431
Expect::hard_close('Expect=GLOB(0xa310c0)') called at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 1621
Expect::DESTROY('Expect=GLOB(0xa310c0)') called at ./tester-a line 0
eval {...} called at ./tester-a line 0
Pid 7475 of spawn id(3) terminated, Status: 0xFF00
//SIGNED//
Andy Magaña
UNIX Systems Administrator
Diligent Contractor, 72nd Air Base Wing
Tinker Air Force Base, Oklahoma
Commercial: (405) 734-0341
-----Original Message-----
From: MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT
Sent: Friday, April 03, 2015 9:24 AM
To: 'ESRY JR., DON'; Matt Zagrabelny
Cc: exp...@li...
Subject: RE: [Expectperl-discuss] expect.pm not updating password
Hello Don,
I was away from my if elsif and tried modifying my script but so far it is not sending the yes after I get the question Are you sure you want to continue connecting (yes/no)?
This my script with debugging on.
use Expect;
my $timeout = 60;
my $filename = "/var/tmp/expect_script.log";
my $ssh = Expect->new('ssh amagana@remotehost'); $ssh->debug(1);
if ($ssh->match() =~ m/Are you sure you want to continue connecting \(yes\/no\)?/ ) { $ssh->send("yes\r"); }
elsif ( $ssh->match() =~ m/Password:/ ) {
$ssh->send("mypassword\n");
}
This my output I get so far from the debugging it just sits at Are you sure.
Starting EXPECT pattern matching...
at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561
Expect::expect('Expect=GLOB(0x174b2db0)', 60, 'Password:') called at ./tester-a line 21 The authenticity of host 'remotehost (0.0.0.0)' can't be established.
RSA key fingerprint is x:x:x:x:x:x:x:x:x:x:x:x:x:x:x:x.
Are you sure you want to continue connecting (yes/no)?
//SIGNED//
Andy Magaña
UNIX Systems Administrator
Diligent Contractor, 72nd Air Base Wing
Tinker Air Force Base, Oklahoma
Commercial: (405) 734-0341
-----Original Message-----
From: ESRY JR., DON [mailto:de...@at...]
Sent: Wednesday, April 01, 2015 10:45 AM
To: MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT; Matt Zagrabelny
Cc: exp...@li...
Subject: RE: [Expectperl-discuss] expect.pm not updating password
You will need to expect multiple things and then check to see what you got and handle that.
It has been a long time since I did this on solaris so I may have the order wrong...
my $timeout = 60;
$ssh->send("passwd amagana\n");
$ssh->expect( $timeout,
[ qr/New Password/],
[ qr/ Are you sure you want to continue connecting/]
);
If ( $ssh->match() =~ m/New Password/ ) {
$ssh->send("mynewpassword\n");
}
elsif ($ssh->match() =~ m/ Are you sure you want to continue connecting/ ) {
$ssh->send("yes\n");
# then here put in what you expect to get next such as "New Password"
# then send it the response such as "mynewpassword\n"
}
# continue on with the rest of your script $ssh->expect(60, 'Re-enter new Password:');
-----Original Message-----
From: MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT [mailto:and...@us...]
Sent: Wednesday, April 01, 2015 10:24 AM
To: ESRY JR., DON; Matt Zagrabelny
Cc: exp...@li...
Subject: RE: [Expectperl-discuss] expect.pm not updating password
Now that I have a working script and thanks very much to you Matt and Don,
I am trying to put in my script an if else because sometimes my script will encounter this :
Are you sure you want to continue connecting (yes/no)?')
what I did create are some variables is this correct and may I see an example if statement so that the script can make a decision and keep going?
use Expect;
my $knownhost = $ssh->expect(60, 'Are you sure you want to continue connecting (yes/no)?'); my $answer = $ssh->send("yes\n"); my $filename = "/var/tmp/expect_script.log";
//SIGNED//
Andy Magaña
UNIX Systems Administrator
Diligent Contractor, 72nd Air Base Wing
Tinker Air Force Base, Oklahoma
Commercial: (405) 734-0341
-----Original Message-----
From: ESRY JR., DON [mailto:de...@at...]
Sent: Tuesday, March 31, 2015 4:16 PM
To: Matt Zagrabelny; MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT
Cc: exp...@li...
Subject: RE: [Expectperl-discuss] expect.pm not updating password
I think you will want a log file to identify where the script failed.
I recommend that you take out the 'w' from the $ssh->log_file($filename, 'w'); so it will append to the file rather than over writing it for each server.
And then put in some sort of header for each server, something like:
my $header = "\n\n======= $system =======\n"; $ssh->print_log_file($header);
Or if you prefer a separate file for each server, then my $filename = "/var/tmp/expect_script_". $system ".log";
little stuff like this can be very frustrating.
-----Original Message-----
From: Matt Zagrabelny [mailto:mzagrabe@d.umn.edu]
Sent: Tuesday, March 31, 2015 4:56 PM
To: MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT
Cc: exp...@li...
Subject: Re: [Expectperl-discuss] expect.pm not updating password
On Tue, Mar 31, 2015 at 3:37 PM, MAGANA, ANDREAS S I CTR USAF AFMC 72 ABW/SCOOT <and...@us...> wrote:
> Thank you Matt,
>
> I just don't know how to put it in a working way I learn by examples I
> am a novice on perl and the modules.
Maybe spend a day or two writing some basic perl programs so you feel more comfortable with this stuff. I'm sure there are truckloads of perl tutorials out there.
I added some context below. You'll need to clean up the leading '>' characters.
-m
>
> #!/usr/bin/perl
> use strict;
>
> use Expect;
>
> # my @servers = qw(
> # server1.example.net
> # server2.example.net
> # server3.example.net
> # server4.example.net
> # server5.example.net
> # );
> #
> # for my $server (@servers) {
> # # do your thing with $server
change_password($server);
> # }
>
sub change_password {
my $system = shift;
my $filename = "/var/tmp/expect_script.log";
my $ssh = Expect->new('ssh amagana@' . $system);
> $ssh->debug(1);
> $ssh->log_file($filename, 'w');
> $ssh->expect(60, 'Password:');
> $ssh->send("mycurrentpassword\n");
> $ssh->expect(60, '$');
> $ssh->send("su - root\n");
> $ssh->expect(60, 'Password:');
> $ssh->send("myrootpassword\n");
> $ssh->expect(60, '#');
> $ssh->send("passwd amagana\n");
> $ssh->expect(60, 'New Password:');
> $ssh->send("mynewpassword\n");
> $ssh->expect(60, 'Re-enter new Password:');
> $ssh->send("mynewpassword\n"); $ssh->expect(60, '#');
> $ssh->send("exit\n"); $ssh->close();
}
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored by Intel and developed in partnership with Slashdot Media, is your hub for all things parallel software development, from weekly thought leadership blogs to news, videos, case studies, tutorials and more. Take a look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Expectperl-discuss mailing list
Exp...@li...
https://lists.sourceforge.net/lists/listinfo/expectperl-discuss
|