From: Dobrica Pavlinusic Date: Thu, 17 Sep 2015 12:31:10 +0000 (+0200) Subject: first working version of chatting with Dell switch over ssh X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;ds=sidebyside;h=cdd290ae716924064ec25af855729e2eacf083c8;p=dell-switch first working version of chatting with Dell switch over ssh --- cdd290ae716924064ec25af855729e2eacf083c8 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e11a863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.pl diff --git a/config.pl-dist b/config.pl-dist new file mode 100644 index 0000000..7426382 --- /dev/null +++ b/config.pl-dist @@ -0,0 +1,2 @@ +$login = 'admin'; +$passwd = 'admin'; diff --git a/dell-switch.pl b/dell-switch.pl new file mode 100755 index 0000000..081a197 --- /dev/null +++ b/dell-switch.pl @@ -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 = 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: , Quit: q, One line: }{} ) { + print $pty " "; + } elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) { + } +} + +__DATA__ +show arp +show vlan +show running-config +show bridge address-table