I have a group of SNMP enabled devices on a remote network, separated from me by a NAT firewall that I need SNMP access to. I am on the outside of the firewall so I have no route to the devices, unless I NATed them out, which I won't do for security reasons. I do have a management server on the inside that is locked down and NATed to the outside that I can SSH into and run my snmp queries. What I am attempting to do is automate this process with a client-server type script. I need to pass the server an IP and a command (what to get) and wait for the server to respond.
I have that basic piece of code written as follows:
server
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $socket = IO::Socket::INET->new('LocalPort' => 7890,
'Proto' => 'tcp',
'Listen' => SOMAXCONN)
or die "Can't create socket ($!)\n";
print "[Server Started]\n";
while (my $client = $socket->accept) {
if ($client->peeraddr) { print "[Connect from " . gethostbyadd
+r($client->peeraddr, AF_INET) . "]\n"; }
my $reply = "test";
my $act = <$client>;
print $act;
print $client "$reply\n";
close $client or die "Can't close ($!)\n";
}
die "Can't accept socket ($!)\n";
client
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $host = shift || die "No host";
my $port = 7890;
my $action = shift || die "No action";
my $ip = shift || die "No ip";
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp')
or die "Can't Bind :$@!\n\n";
print $sock "$action ";
print $sock "$ip\n";
while( <$sock> ) {
print $_;
}
close $sock;
I haven't written the meat of the code to perform the SNMP actions, but they will just be subs in a module used in the server program.
This works okay for passing pings and pongs back and forth, but I am looking to pass more data. Is there a way I can pass an array or a hash across the socket without formatting it to ASCII first then having to reassemble on the other end? If not, I might just use the SSH module instead and save having to code a server.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.