sub
arpnip {
my
(
$self
,
$hostlabel
,
$ssh
,
$args
) =
@_
;
debug
"$hostlabel $$ arpnip()"
;
my
(
$pty
,
$pid
) =
$ssh
->open2pty;
unless
(
$pty
) {
debug
"unable to run remote command [$hostlabel] "
.
$ssh
->error;
return
();
}
my
$expect
= Expect->init(
$pty
);
my
(
$pos
,
$error
,
$match
,
$before
,
$after
);
my
$prompt
;
if
(
$args
->{enable_password}) {
$prompt
=
qr/>/
;
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(10, -re,
$prompt
);
$expect
->
send
(
"enable\n"
);
$prompt
=
qr/Password:/
;
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(10, -re,
$prompt
);
$expect
->
send
(
$args
->{enable_password} .
"\n"
);
}
$prompt
=
qr/#\s*$/
;
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(10, -re,
$prompt
);
$expect
->
send
(
"terminal pager 2147483647\n"
);
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(5, -re,
$prompt
);
$expect
->
send
(
"show names\n"
);
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(60, -re,
$prompt
);
my
@names
=
split
(m/\n/,
$before
);
$expect
->
send
(
"show arp\n"
);
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(60, -re,
$prompt
);
my
@lines
=
split
(m/\n/,
$before
);
my
@arpentries
= ();
my
$linereg
=
qr/[A-z0-9\-\.]+\s([A-z0-9\-\.]+)\s
([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})/
x;
foreach
my
$line
(
@lines
) {
if
(
$line
=~
$linereg
) {
my
(
$ip
,
$mac
) = ($1, $2);
if
(
$ip
!~ m/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
foreach
my
$name
(
@names
) {
if
(
$name
=~
qr/name\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\s([\w-]*)/
x) {
if
(
$ip
eq $2) {
$ip
= $1;
}
}
}
}
if
(
$ip
=~ m/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) {
push
@arpentries
, {
mac
=>
$mac
,
ip
=>
$ip
};
}
}
}
$expect
->
send
(
"show ipv6 neighbor\n"
);
(
$pos
,
$error
,
$match
,
$before
,
$after
) =
$expect
->expect(60, -re,
$prompt
);
@lines
=
split
(m/\n/,
$before
);
$linereg
=
qr/([0-9a-fA-F\:]+)\s+[0-9]+\s
([0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4})/
x;
foreach
my
$line
(
@lines
) {
if
(
$line
=~
$linereg
) {
my
(
$ip
,
$mac
) = ($1, $2);
push
@arpentries
, {
mac
=>
$mac
,
ip
=>
$ip
};
}
}
$expect
->
send
(
"exit\n"
);
$expect
->soft_close();
return
@arpentries
;
}
1;