Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: expect.pm header

by GotToBTru (Prior)
on Apr 07, 2015 at 15:09 UTC ( #1122710=note: print w/replies, xml ) Need Help??


in reply to Re^2: expect.pm header
in thread expect.pm header

You need to replace $system with a placeholder - it could be the word 'system' - and then, just before you print the header, use s/// to replace the placeholder with the value you want, which will be defined at that time.

my $header = "\n\n======= system =======\n"; ... sub change_password { my $system = shift; $header =~ s/system/$system/; ...
Dum Spiro Spero

Replies are listed 'Best First'.
Re^4: expect.pm header
by amagana (Acolyte) on Apr 07, 2015 at 18:14 UTC
    Thank you GotToBTru, I am missing the mark I have my script like this now but I still don't see a header in my log file just other information that was a result of the script in debug. For example:
    ^[[01m[root@remotehost ~ ]^[[m # hostname remotehost ^[[01m[root@remotehost ~ ]^[[m # uptime 1:03pm up 9 day(s), 13:47, 3 users, load average: 5.83, 5.33, 5.1 +3 ^[[01m[root@remotehost ~ ]^[[m # passwd amagana New Password: Re-enter new Password: passwd: password successfully changed for amagana ^[[01m[root@remotehost ~ ]^[[m # exit logout $
    My script is current like this now:
    #!/usr/bin/perl -w use warnings; use strict; use Expect; #my $system = shift; my $filename = "/var/tmp/expect_script.log"; my $header = "\n\n======= system =======\n"; my $timeout = 60; my @servers = qw( remotehost ); for my $server (@servers) { # do your thing with $server change_password($server); } sub change_password { my $system = shift; $header =~ s/system/$system/; #my $filename = "/var/tmp/expect_script.log"; my $ssh = Expect->new('ssh amagana@' . $system); $ssh->log_file($filename); $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("mycurrentpassword\n"); } $ssh->expect(60, '$'); $ssh->send("su - root\n"); $ssh->expect(60, 'Password:'); $ssh->send("rootpassword\n"); $ssh->expect(60, '#'); $ssh->send("hostname\n"); $ssh->expect(60, '#'); $ssh->send("uptime\n"); $ssh->expect(60, '#'); $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New Password:'); $ssh->send("newpassword\n"); $ssh->expect(60, 'Re-enter new Password:'); $ssh->send("newpassword\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close(); }

      Try inserting
           $ssh->print_log_file($header);
      after
           $ssh->debug(1);

        I tried to use the $ssh->print_log_file($header); and got a bunch of ^M characters in my log.

        M ^[[01m[root@host ~ ]^[[m # hostname^M prdoradb03^M ^[[01m[root@host ~ ]^[[m # uptime^M 3:15pm up 9 day(s), 15:59, 3 users, load average: 5.04, 5.59, 5.3 +2^M ^[[01m[root@host ~ ]^[[m # passwd amagana^M New Password: ^M Re-enter new Password: ^M passwd: password successfully changed for amagana^M ^[[01m[root@host ~ ]^[[m # exit^M logout^M $
        $ssh->log_file($filename); $ssh->debug(1); $ssh->print_log_file($header); $ssh->expect ( $timeout, [ qr/Password:/], [ qr/Are you sure you want to continue connecting \(yes\/no\)?/] );

        The script stops and says su: incorrect password, these are Linux boxes at when it sends the passwords during the expect/send of su - root.

        Is it because I will have to escape the special characters when it starts to send the strings with special characters, I did not have to do this with Solaris boxes?

        Is there a way to just alway ignore special characters only during the send part of expect?

        1 #!/usr/bin/perl -w 2 3 use warnings; 4 use strict; 5 use Expect; 6 7 8 my $filename = "/var/tmp/expect_script.log"; 9 my $header = "\r\n\r\n======= system =======\r\n"; 10 my $timeout = 60; 11 12 13 #This will open a file and push it to array 14 my @servers; 15 16 #open (my $file, '<', $ARGV[0]) or die $!; 17 18 open (my $file, '<', "/home/amagana/scripts/lists/list_b2") or + die $!; 19 20 while(<$file>) { 21 push (@servers, $_); #push each line of the file to array 22 } 23 24 print "$_\n" for(@servers); 25 26 27 #This is an array 28 #my @servers = qw( 29 # 30 # 31 # 32 # 33 #); 34 35 36 for my $server (@servers) { 37 # do your thing with $server 38 39 change_password($server); 40 41 } 42 43 44 sub change_password { 45 46 my $system = shift; 47 my $ssh = Expect->new('ssh amagana@' . $system); 48 49 50 $ssh->debug(22); 51 $ssh->log_file("$filename"); 52 my $my_header = $header; 53 $my_header =~ s/system/$system/; 54 $ssh->print_log_file($my_header); 55 56 $ssh->expect ( $timeout, 57 [ qr/password:/], 58 [ qr/Are you sure you want to continue connecting \(yes\ +/no\)?/] 59 60 ); 61 62 if ($ssh->match() =~ m/Are you sure you want to continue conne +cting \(yes\/no\)?/ ) { 63 $ssh->send("yes\r"); 64 } 65 66 elsif ($ssh->match() =~ m/password:/ ) { 67 68 $ssh->send("mycurrentpassword\n"); #no problem with the specia +l characters at this line 69 70 } 71 72 $ssh->expect(60, '$'); 73 $ssh->send("su - root\n"); 74 $ssh->expect(60, 'Password:'); 75 $ssh->send( "rootpasswordwithspecialcharacters\n" ); #but my s +cript quits here 76 $ssh->expect(60, '#'); 77 $ssh->send("hostname\n"); 78 $ssh->expect(60, '#'); 79 $ssh->send("uptime\n"); 80 $ssh->expect(60, '#'); 81 $ssh->send("passwd amagana\n"); 82 $ssh->expect(60, 'New UNIX Password:'); #linux 83 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 84 $ssh->expect(60, 'Retype new UNIX password:'); #linux 85 $ssh->send( "mynewpasswordwithspecialcharacters\n" ); 86 $ssh->expect(60, '#'); 87 $ssh->send("exit\n"); 88 $ssh->expect(60, '$'); 89 $ssh->send("exit\n"); 90 $ssh->close(); 91 }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1122710]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2020-04-01 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (186 votes). Check out past polls.

    Notices?