first working version of chatting with Dell switch over ssh
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 17 Sep 2015 12:31:10 +0000 (14:31 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 17 Sep 2015 13:49:47 +0000 (15:49 +0200)
.gitignore [new file with mode: 0644]
config.pl-dist [new file with mode: 0644]
dell-switch.pl [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..e11a863
--- /dev/null
@@ -0,0 +1 @@
+config.pl
diff --git a/config.pl-dist b/config.pl-dist
new file mode 100644 (file)
index 0000000..7426382
--- /dev/null
@@ -0,0 +1,2 @@
+$login = 'admin';
+$passwd = 'admin';
diff --git a/dell-switch.pl b/dell-switch.pl
new file mode 100755 (executable)
index 0000000..081a197
--- /dev/null
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use autodie;
+
+use Net::OpenSSH;
+use Data::Dump qw(dump);
+use List::Util qw(first);
+use Time::HiRes;
+
+our $login;
+our $passwd;
+
+require 'config.pl';
+
+#$Net::OpenSSH::debug = ~0;
+
+my $ip = shift @ARGV || '10.20.0.2';
+my @commands = @ARGV;
+@commands = <DATA> unless @commands;
+
+my $ssh = Net::OpenSSH->new('auto@'.$ip);
+my ($pty ,$pid) = $ssh->open2pty();
+
+open my $log, '>', '/tmp/dell.log';
+
+my $buff;
+
+while(1) {
+       my $data;
+       my $read = sysread($pty, $data, 1);
+       print STDERR $data;
+       $buff .= $data;
+       if ( $buff =~ m/User Name:/ ) {
+               print $pty "$login\n";
+               $buff = '';
+       } elsif ( $buff =~ m/Password:/ ) {
+               print $pty "$passwd\n";
+               $buff = '';
+       } elsif ( $buff =~ m/#$/ ) {
+               if ( $buff ) {
+                       print $log $buff;
+                       $buff = '';
+               }
+               if ( my $command = shift @commands ) {
+                       $command .= "\n" unless $command =~ m/\n$/;
+                       warn ">> $command\n";
+                       print $pty "$command";
+                       $buff = '';
+               } else  {
+                       print $pty "exit\n";
+                       close($pty);
+                       last;
+               }
+       } elsif ( $buff =~ m/% Unrecognized command/ ) {
+               exit 1;
+       } elsif ( $buff =~ s{More: <space>,  Quit: q, One line: <return> }{} ) {
+               print $pty " ";
+       } elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) {
+       }
+}
+
+__DATA__
+show arp
+show vlan
+show running-config
+show bridge address-table