Send trap with Perl (net-snmp) - perl

I run snmptrapd and can see incoming trap, when send trap via snmptrap
snmptrap -c public -v 2c localhost "" 1.3.3.3.3.3.3.3 1.2.2.2.2.2.2 s "Aliens here"
But I no have trap, when send via Perl script
use SNMP;
$sess = new SNMP::Session(DestHost => '127.0.0.1', RemotePort => "162" );
$sess->trap(enterprise=>'.1.3.6.1.4.1.2021', # or 'ucdavis' [default]
agent => '127.0.0.1', # or 'localhost',[dflt 1st intf on host]
generic => specific, # can be omitted if 'specific' supplied
specific => 5, # can be omitted if 'generic' supplied
uptime => 1234, # dflt to localhost uptime (0 on win32)
[[ifIndex, 1, 1],[sysLocation, 0, "here"]]);
What's wrong?

Your second version i.e. the perl one is not specifying community or version, unlike the first one. Try adding them
$sess = new SNMP::Session(DestHost => '127.0.0.1',
RemotePort => "162",
Community => "public,
Version => 2);
Also see http://www.remothelast.altervista.org/SNMP_Perl.html
and http://www.net-snmp.org/docs/perl-SNMP-README.html for SNMP::Session usage.

Related

Perl mail attachment file size limit

I am using MIME::Lite module to send attachments in email and everything works fine until I realized attachment larger than 15mb cannot be sent successfully. Any suggestion on other module which not having size limit?
You should consider using a different module because that's what the author recommends (try Email::MIME, MIME::Entity or Email::Sender). However, it's not the module that determines the attachment size restriction.
The size limit you're seeing is set by your SMTP server. If you're not explicitly configuring your SMTP server you're using your local service which might differ depending on your OS. You're probably using sendmail or postfix.
See size limit in postfix
postconf | grep message_size_limit
See size limit in sendmail
grep MaxMessageSize /etc/mail/sendmail.cf
If you're using an external SMTP server they usually have their own size limits. Google has a 25MB size limit for their SMTP server smtp.gmail.com. If that's sufficient for you you could send your mail via Google by authenticating.
$msg->send(
'smtp', 'smtp.gmail.com',
Port => 465,
SSL => 1,
AuthUser => $user,
AuthPass => $password,
);
EDIT: I had some issues using MIME::Lite to work properly with Gmail SMTP server so I here's an example on how to use gmail.smtp.com with an alternative pacakge.
#!/usr/bin/env perl
use warnings;
use strict;
use Email::Sender::Simple qw( sendmail );
use Email::Sender::Transport::SMTP;
use Email::Simple;
my $user = 'username#gmail.com';
my $password = 'app-password';
my $host = 'smtp.gmail.com';
my $port = 465;
my $transport = Email::Sender::Transport::SMTP->new(
{
host => $host,
port => $port,
ssl => 1,
sasl_username => $user,
sasl_password => $password,
}
);
my $email = Email::Simple->create(
header => [
To => 'someone#example.com',
From => 'me#localhost',
Subject => 'Hello...',
],
body => "World!\n",
);
sendmail( $email, { transport => $transport } );

Error IMAPClient with message_string() function

you have an idea on how to solve this problem that I met with the function message_string () of Mail::IMAPClient library, here is my code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Mail::IMAPClient;
use IO::Socket::SSL;
# Create the object connexion with socket SSL + LOG ON
my $imap = Mail::IMAPClient->new(
#Debug => 1,
User => 'xxxxx',
Password => 'yyyyy',
Uid => 1,
Peek => 1, # set \Seen flag
Socket => IO::Socket::SSL->new(
Proto => 'tcp',
PeerAddr => 'zzzzzzz',
PeerPort => 993,
)
);
die "$0: connect: $#" if defined $#;
my $nm=$imap->unseen_count("INBOX") ;
# Select INBOX dossier
$imap->select("INBOX");
my $msg = $imap->message_string('47') or die " $#\n";
the error obtained is the following:
message_string() expected 304627 bytes but received 304718 you may need the IgnoreSizeErrors option
The error message tells you exactly how to cope with this. Some IMAP servers calculate the message size incorrectly -- in particular, many (such as notably GMail) examine the local message size, then change the line terminators to CRLF when sending the message over IMAP, resulting in a slightly different actual size than what the server told the client to expect. By default, IMAPClient will throw an error when this happens, but you can tell it not to by saying IgnoreSizeErrors => 1 when you create an instance.
my $imap = Mail::IMAPClient->new(
#Debug => 1,
User => 'xxxxx',
Password => 'yyyyy',
Uid => 1,
Peek => 1, # set \Seen flag
Socket => IO::Socket::SSL->new(
Proto => 'tcp',
PeerAddr => 'zzzzzzz',
PeerPort => 993,
),
# See here
IgnoreSizeErrors => 1
);

perl sending email works on one system but not another

I have a simple sending email perl code.
use strict;
use warnings;
use Time::Format;
use Email::MIME;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;
my $message = Email::MIME->create(
header_str => [
From => 'me#provider.com',
To => 'me#provider.com',
Subject => 'test',
],
attributes => {
encoding => 'quoted-printable',
charset => 'ISO-8859-1',
},
body_str => 'stefy test',
);
my $transport = Email::Sender::Transport::SMTP->new({
host => 'my.server.smtp',
port => 25,
});
sendmail($message, { transport => $transport });
I can it run with success in one system but not another.
Windows 7 professional -> success
Window Server 2008 -> failure
this is the exception I get:
unable to establish SMTP connection to my.server.smtp port 25
Trace begun at D:\strawberryperl\perl\site\lib\Email\Sender\Transport\SMTP.pm line 193 Email::Sender::Transport::SMTP::_throw('Email::Sender::Transport::SMTP=HASH(0x38
0fef8)', 'unable to establish SMTP connection to my.server.smtp port 25') called at D:\strawberryperl\perl\site\lib\Email\Sender\Transport\SMTP.pm line 143
Any idea ?
thank you
update of the perl version fixed the problem.
This is perl 5, version 26, subversion 2 (v5.26.2) built for MSWin32-x64-multi-t
hread

How to pass IPv6 parameter for snmp session creation using

I am using SNMP package to create SNMPv3 session. But I am not sure how to pass transport information as UDP6 as I think default behavior is UDP. As far as I know that when use Net::SNMP to create session then we have "domain" parameter to classify between ipv4 and ipv6 address . But the same is not applicable to SNMP::Session. Can you please let me know how to pass transport information?
new SNMP::Session(DestHost => $self->{'ip'},
version => 3,
RemotePort => $self->{'port'},
SecEngineId => ‘0x8888233356',
SecName => $hashVal{'userName'},
AuthProto => $authProto,
AuthPass => $hashVal{'authPW'},
PrivProto => $privProto,
PrivPass => $hashVal{'privPW'},
SecLevel => $hashVal{‘secLevel’');

How to specify -v option in openssh in perl

I'm using Net::OpenSSH module to connect to a node. But while connecting to the node, I need to specify -v for ssh as ssh -v admin#hostname. Tried using master_opts and default_ssh_opts. But they didn't work.
my $ssh = Net::OpenSSH->new("$user_name\:$password\#$server",strict_mode => 0, default_ssh_opts => [-o => "-v"]);
How can this be achieved?
my $ssh = Net::OpenSSH->new($server,
user => $user_name,
password => $password,
strict_mode => 0,
master_opts => ['-v']);