copy startup-config ftp:// example and prompt response
[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 use lib '.';
15 require 'config.pl';
16
17 #$Net::OpenSSH::debug = ~0;
18
19 my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n";
20 my @commands = @ARGV;
21 @commands = <DATA> unless @commands;
22
23 warn "\n## ssh $ip\n";
24 my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd);
25 my ($pty ,$pid) = $ssh->open2pty();
26 if ( ! $pty ) {
27         warn "ERROR: can't connect to $ip, skipping";
28         exit 0;
29 }
30
31 my $buff;
32
33 sub send_pty {
34         my $string = shift;
35         sleep 0.1; # we really need to wait for slow PowerConnect 5324
36         foreach (split //, $string) {
37                 print STDERR "[$_]" if $debug;
38                 syswrite $pty, $_;
39                 #$pty->flush;
40                 sleep 0.05;
41
42                 sysread $pty, my $echo, 1;
43                 print STDERR $echo;
44                 $buff .= $echo;
45         }
46 }
47
48 mkdir 'log' unless -d 'log';
49
50 chdir 'log';
51
52 sub save_log {
53         my ($ip, $hostname, $command, $buff) = @_;
54
55         return unless $command;
56         return if $ENV{NO_LOG};
57         
58         my $file = "${ip}_${hostname}_${command}.log";
59         open my $log, '>', $file;
60         $buff =~ s/\r//gs; # strip CR, leave LF only
61         print $log $buff;
62         if ( -e '.git' ) {
63                 system 'git', 'add', $file;
64                 system 'git', 'commit', '-m', "$ip $hostname", $file;
65         }
66 }
67
68 my $command;
69 my @commands_while = ( @commands );
70
71 while() {
72         my $data;
73         my $read = sysread($pty, $data, 1);
74         print STDERR $data;
75         $buff .= $data;
76         if ( $buff =~ m/User Name:/ ) {
77                 send_pty "$login\n";
78                 $buff = '';
79         } elsif ( $buff =~ m/Password:/ ) {
80                 send_pty "$passwd\n";
81                 $buff = '';
82         } elsif ( $buff =~ m/([\w\-\(\)]+)#$/ ) {
83                 my $hostname = $1;
84                 if ( $buff ) {
85                         save_log $ip, $hostname, $command, $buff;
86                         $buff = '';
87                 }
88                 if ( $command = shift @commands_while ) {
89                         $command =~ s/[\n\r]+$//;
90                         send_pty "$command\n";
91                         $buff = '';
92                 } else  {
93                         send_pty "exit\n";
94                         close($pty);
95                         last;
96                 }
97         } elsif ( $buff =~ m/% Unrecognized command/ ) {
98                 exit 1;
99         } elsif ( $buff =~ s{More: <space>,  Quit: q.*One line: <return>\s*}{} ) {
100                 send_pty " ";
101         } elsif ( $buff =~ s{\Q--More-- or (q)uit\E}{} ) {
102                 send_pty " ";
103         } elsif ( $buff =~ s{\e\[0m\s*\r\s+\r}{} ) {
104                 # nop
105         } elsif ( $buff =~ m/^[\r\n]+[\w\-]+>$/ ) {
106                 send_pty "enable\n";
107         } elsif ( $buff =~ m{\QOverwrite file [startup-config] ?[Yes/press any key for no]....\E} ) {
108                 send_pty "y";
109                 $buff = '';
110         } elsif ( $buff =~ s{Management access will be blocked for the duration of the transfer.*Are you sure you want to start\? \(y/n\) }{}s ) {
111                 send_pty 'y';
112         }
113 }
114
115 __DATA__
116 show system
117 show arp
118 show vlan
119 show running-config
120 show bridge address
121 show interfaces status
122