pass username and password throgh ssh
[dell-switch] / dell-switch.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 use Net::OpenSSH;
7 use Data::Dump qw(dump);
8 use Time::HiRes qw(sleep);
9
10 our $login;
11 our $passwd;
12 our $debug = $ENV{DEBUG} || 0;
13
14 require 'config.pl';
15
16 #$Net::OpenSSH::debug = ~0;
17
18 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
19 my @commands = @ARGV;
20 @commands = <DATA> unless @commands;
21
22 warn "## $ip\n";
23 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd);
24 my ($pty ,$pid) = $ssh->open2pty();
25 if ( ! $pty ) {
26         warn "ERROR: can't connect to $ip, skipping";
27         exit 0;
28 }
29
30 my $buff;
31
32 sub send_pty {
33         my $string = shift;
34         sleep 0.1; # we really need to wait for slow PowerConnect 5324
35         foreach (split //, $string) {
36                 print STDERR "[$_]" if $debug;
37                 syswrite $pty, $_;
38 #               $pty->flush;
39                 sysread $pty, my $echo, 1;
40                 print STDERR $echo;
41                 $buff .= $echo;
42         }
43 }
44
45 mkdir 'log' unless -d 'log';
46
47 chdir 'log';
48
49 sub save_log {
50         my ($ip, $hostname, $command, $buff) = @_;
51
52         return unless $command;
53         
54         my $file = "${ip}_${hostname}_${command}.log";
55         open my $log, '>', $file;
56         $buff =~ s/\r//gs; # strip CR, leave LF only
57         print $log $buff;
58         if ( -e '.git' ) {
59                 system 'git', 'add', $file;
60                 system 'git', 'commit', '-m', "$ip $hostname", $file;
61         }
62 }
63
64 my $command;
65
66 while(1) {
67         my $data;
68         my $read = sysread($pty, $data, 1);
69         print STDERR $data;
70         $buff .= $data;
71         if ( $buff =~ m/User Name:/ ) {
72                 send_pty "$login\n";
73                 $buff = '';
74         } elsif ( $buff =~ m/Password:/ ) {
75                 send_pty "$passwd\n";
76                 $buff = '';
77         } elsif ( $buff =~ m/([\w\-]+)#$/ ) {
78                 my $hostname = $1;
79                 if ( $buff ) {
80                         save_log $ip, $hostname, $command, $buff;
81                         $buff = '';
82                 }
83                 if ( $command = shift @commands ) {
84                         $command =~ s/[\n\r]+$//;
85                         send_pty "$command\n";
86                         $buff = '';
87                 } else  {
88                         send_pty "exit\n";
89                         close($pty);
90                         last;
91                 }
92         } elsif ( $buff =~ m/% Unrecognized command/ ) {
93                 exit 1;
94         } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return> }{} ) {
95                 send_pty " ";
96         } elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) {
97         }
98 }
99
100 __DATA__
101 show arp
102 show vlan
103 show running-config
104 show bridge address-table