Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: expect.pm header

by sn1987a (Deacon)
on Apr 07, 2015 at 18:23 UTC ( [id://1122726]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^6: expect.pm header
by amagana (Acolyte) on Apr 07, 2015 at 20:42 UTC

    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\)?/] );

      It worked for my. I have modified you program to work in my environment, but the core is still the same. Note how I made a local copy of the header before doing the substitution so the original is available for the other servers

      Progam:
      #!/usr/bin/env perl use strict; use Expect; my $header_template = "\n\n======== system ========\n"; do_date($_) for qw( vftp vwww2 ); sub do_date { my ($system) = @_; my $header = $header_template; $header =~ s/system/$system/; my $timeout = 10; my $ssh = Expect->new("ssh $system"); $ssh->log_file("$system.log"); $ssh->debug(1); $ssh->print_log_file($header); $ssh->expect( $timeout, [qr/password/] ); if ( $ssh->match() =~ m/password/ ) { $ssh->send("youcantseeme\n"); } $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("hostname\n"); $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("date\n"); $ssh->expect( 60, [qr/\$\s*/] ); $ssh->send("exit\n"); $ssh->close(); }


      vftp.log:

      ======== vftp ======== jwk@vftp's password: Welcome to Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-48-generic x86_64) Last login: Tue Apr 7 22:48:33 2015 from 10.1.1.134 jwk@vftp:~$ hostname vftp jwk@vftp:~$ date Tue Apr 7 22:53:09 CDT 2015 jwk@vftp:~$


      vwww2.log:

      ======== vwww2 ======== jwk@vwww2's password: Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-48-generic x86_64) Last login: Tue Apr 7 22:48:35 2015 from 10.1.1.134 jwk@vwww2:~$ hostname vwww2 jwk@vwww2:~$ date Tue Apr 7 22:53:10 CDT 2015 jwk@vwww2:~$

        Thank you for your example I am still trying to avoid getting the ^M characters in my log also I have not figured out where to place the
        $header =~ s/system/$system/;
        inside my script. I am learning as I am blowing up my script. Could you give a illustration with my script on how to get my header to work inside my log file?
expect.pm how to ignore special characters
by amagana (Acolyte) on Apr 15, 2015 at 14:59 UTC

    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 }

      Do the passwords have the same special characters on the different machines. Some "special" characters are treated differently inside double quotes. To avoid that use single quotes.

      $ssh->send( 'rootpasswordwithspecialcharacters' . "\n" );

      The single quotes are used to protect that characters in the root password. The newline is then concatenated to the end, since \n depends on being in double quotes.

      Please follow up on the new thread you started. See my response at Re: How do I get expect.pm to ignore special characters?.

        Thank you sn1987a

        It looks like that worked for the root password but I used the same single quotes for when I get my password changed. The script stops now at when I do a expect/send after sending passwd amagana at New UNIX Password like this..

        [root@Remotehost ~]# Starting EXPECT pattern matching... at /usr/lib/perl5/site_perl/5.8.8/Expect.pm line 561 Expect::expect('Expect=GLOB(0xc4cc170)', 60, 'New UNIX Passwor +d:') called at test-a-filehandle.pl line 82 main::change_password('linux-host\x{a}') called at test-a-file +handle.pl line 39 passwd amagana Changing password for user amagana. New UNIX password:
        $ssh->send("passwd amagana\n"); $ssh->expect(60, 'New UNIX Password:'); #linux $ssh->send( 'XxX*%xx,^xxxxXx_' . "\n"); $ssh->expect(60, 'Retype new UNIX password:'); #linux $ssh->send( 'XxX*%xx,^xxxxXx_' . "\n"); $ssh->expect(60, '#'); $ssh->send("exit\n"); $ssh->expect(60, '$'); $ssh->send("exit\n"); $ssh->close();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found