Skip to content

Commit cdd290a

Browse files
committed
first working version of chatting with Dell switch over ssh
0 parents  commit cdd290a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.pl

config.pl-dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$login = 'admin';
2+
$passwd = 'admin';

dell-switch.pl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 List::Util qw(first);
9+
use Time::HiRes;
10+
11+
our $login;
12+
our $passwd;
13+
14+
require 'config.pl';
15+
16+
#$Net::OpenSSH::debug = ~0;
17+
18+
my $ip = shift @ARGV || '10.20.0.2';
19+
my @commands = @ARGV;
20+
@commands = <DATA> unless @commands;
21+
22+
my $ssh = Net::OpenSSH->new('auto@'.$ip);
23+
my ($pty ,$pid) = $ssh->open2pty();
24+
25+
open my $log, '>', '/tmp/dell.log';
26+
27+
my $buff;
28+
29+
while(1) {
30+
my $data;
31+
my $read = sysread($pty, $data, 1);
32+
print STDERR $data;
33+
$buff .= $data;
34+
if ( $buff =~ m/User Name:/ ) {
35+
print $pty "$login\n";
36+
$buff = '';
37+
} elsif ( $buff =~ m/Password:/ ) {
38+
print $pty "$passwd\n";
39+
$buff = '';
40+
} elsif ( $buff =~ m/#$/ ) {
41+
if ( $buff ) {
42+
print $log $buff;
43+
$buff = '';
44+
}
45+
if ( my $command = shift @commands ) {
46+
$command .= "\n" unless $command =~ m/\n$/;
47+
warn ">> $command\n";
48+
print $pty "$command";
49+
$buff = '';
50+
} else {
51+
print $pty "exit\n";
52+
close($pty);
53+
last;
54+
}
55+
} elsif ( $buff =~ m/% Unrecognized command/ ) {
56+
exit 1;
57+
} elsif ( $buff =~ s{More: <space>, Quit: q, One line: <return> }{} ) {
58+
print $pty " ";
59+
} elsif ( $buff =~ s{\e\[0m\r\s+\r}{} ) {
60+
}
61+
}
62+
63+
__DATA__
64+
show arp
65+
show vlan
66+
show running-config
67+
show bridge address-table

0 commit comments

Comments
 (0)