How to send an IP broadcast message using Perl - perl

I've tried both Net::RawIP and Net::Write::Layer3. It works fine if i supply a specific ip address in the network. while i'm getting either
sendto() at /usr/lib/perl5/Net/RawIP.pm line 630. shell returned 13
or
Net::Write::Layer::send: Permission denied
if i change the destination address to 66.66.66.255
any ideas?
the code i'm using is here
use Net::Write::Layer qw(:constants);
use Net::Write::Layer3;
use NetAddr::IP;
use Net::RawIP;
$message = "Foo";
# using Net::RawIP
$n = Net::RawIP->new({
ip => {
tos => 0xC0,
daddr => '66.66.66.2',
protocol => 2,
},
generic => {
data => $message
}
});
$n->send;
# using Net::Write::Layer3
my $desc = Net::Write::Layer3->new(
dst => '66.66.66.2',
protocol => '2',
family => NW_AF_INET,
);
$desc->open;
$desc->send($message);
$desc->close;

Error 13 is usually EACCES - i.e. you do not have sufficient permission to send to a broadcast socket.

Related

Using Perl's Net::Frame::Simple module to rewrite ethernet header

I am using below code (copied from http://search.cpan.org/~gomor/Net-Frame-Simple-1.06/lib/Net/Frame/Simple.pm) to rewrite the ethernet src and dst mac information but its generating bad IP packet.
#!/usr/bin/perl
use Net::Frame::Simple;
use Net::Frame::Layer::IPv4;
use Net::Frame::Layer::TCP;
use Net::Frame::Layer::ETH;
use Net::Frame::Device;
use Net::Write::Layer3;
use Net::Frame::Dump::Online;
use Net::Write::Layer2;
my $src = '100.1.1.39';
my $target = '200.2.2.97';
my $port = 22;
my $eth = Net::Frame::Layer::ETH->new(src => "00:0c:29:d1:03:06", dst => "03:03:03:03:03:03");
my $ip4 = Net::Frame::Layer::IPv4->new(src => $src,dst => $target);
my $tcp = Net::Frame::Layer::TCP->new(dst => $port, options => "\x02\x04\x54\x0b",payload => 'test');
my $oSimple = Net::Frame::Simple->new(layers => [$eth,$ip4,$tcp],);
# Now, the frame is ready to be send to the network
# We open a sender object, and a retriever object
my $oWrite = Net::Write::Layer2->new(dev => 'eth0');
$oWrite = Net::Write::Layer3->new(dst => $target);
#my $oDump = Net::Frame::Dump::Online->new(dev => $oDevice->dev);
#$oDump->start;
$oWrite->open;
# We send the frame
$oSimple->send($oWrite);
If i remove the ethernet part ($eth) when defining layers in $oSimple, it works.
Any suggestions what i am missing or doing wrong?
I was able to make it work
For anyone's reference, if remove the line "Net::Write::Layer3" and just do the writing at layer 2 (Net::Write::Layer2) in above code, you can change the src and dst MACs.

TCP server ignores my SYN request from NET::RawIP

I use small perl script:
#!/usr/bin/perl -w
use Net::RawIP;
$a = new Net::RawIP({
ip =>
{
daddr => '192.168.0.100'
},
tcp =>
{
source => 41218,
dest => 80,
syn => 1
}
});
$a->send;
and server doesn't send any response.
More detailed information from wireshark:
screen
I expected to get response from server. So, what's wrong ?
Validate the checksum, packets are dropped if the checksum is invalid

connect to localhost failed (Connection refused) no (more) retries

I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};

Trying to send authenticated email using Net::SMTP::SSL

I'm attempting to send an authenticated email with the Net::SMTP::SSL module to a comcast email server. I'm using the following code.
#!/usr/bin/perl
use Net::SMTP::SSL;
use MIME::Base64;
$smtp = Net::SMTP::SSL->new
(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
);
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();
# Mailbox info
$smtp->datasend(encode_base64('username')); # username
$smtp->response();
$smtp->datasend(encode_base64('password')); # password
$smtp->response();
# Email from
$smtp->mail('user\#comcast.net');
# Email to
$smtp->to('user\#host.com');
$smtp->data();
$smtp->datasend("To: user\#host.com\n");
$smtp->datasend("From: user\#comcast.net\n");
$smtp->datasend("Subject: Test");
# Line break to separate headers from body
$smtp->datasend("\n");
$smtp->datasend("Blah\n");
$smtp->dataend();
$smtp->quit();
exit;
I'm basically following the code from here for comcast.
I've ran telnet and can connect to the smtp server on the port and I can issue the AUTH LOGIN and successfully login, but issuing the
$smtp->datasend("AUTH LOGIN\n");
always results in:
Can't call method "datasend" on an undefined value
I've also tried executing the auth method to login and that fails as well.
What am I missing here? I know it's something simple I'm overlooking.
Pretty much all of the Net::SMTP methods optionally set error codes, so I suspect that Net::SMTP::SSL does the same. Try the following for your constructor:
use Net::SMTP::SSL;
use MIME::Base64;
use strict;
use warnings;
my $smtp = Net::SMTP::SSL->new(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
) or die "Failed to connect to mail server: $!";
Also, the fact that your smtp server is different than your Hello is a little suspect to me.
For email services that use STARTTLS, it's best to use the newer NET::SMTPS module. Try the following code:
my $msg = MIME::Lite ->new (
From => 'from#bellsouth.net',
To => 'to#bellsouth.net',
Subject => 'Test Message',
Data => 'This is a test',
Type => 'text/html'
);
my $USERNAME = 'from#bellsouth.net';
my $PASSWORD = 'abc123';
my $smtps = Net::SMTPS->new("smtp.mail.att.net", Port => 587, doSSL => 'starttls', SSL_version=>'TLSv1');
$smtps->auth ( $USERNAME, $PASSWORD ) or die("Could not authenticate with bellsouth.\n");
$smtps ->mail('from#bellsouth.net');
$smtps->to('to#bellsouth.net');
$smtps->data();
$smtps->datasend( $msg->as_string() );
$smtps->dataend();
$smtps->quit;
Originally from http://www.skipser.com/p/2/p/send-email-using-perl-via-live.com.html
Try this:
Email from
Change
$smtp->mail('user\#comcast.net'); to $smtp->mail('user#comcast.net'); without '\'
Email to
Change
$smtp->to('user\#host.com'); to $smtp->to('user#host.com'); without '\'

Can't call method "headers" on an undefined value at C:/Perl/site/lib/Net/Stomp. pm line 122

I am trying to send EMS message from Perl to queue running in the EMS server. I'm using STOMP module to connect to EMS queue for sending message. Here is my code -
JMSQUEUE.pl:
use Net::Stomp;
use Net::Stomp::Frame;
my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '7222' } );
$stomp->connect( { login => 'admin', passcode => '' } );
$stomp->send( { destination => '/queue/pradeepexp', body => 'test message' } );
$stomp->disconnect;
and in my module - STOMP.PM:
sub connect {
my ( $self, $conf ) = #_;
my $frame =
Net::Stomp::Frame->new( { command => 'CONNECT', headers => $conf } );
$self->send_frame($frame);
$frame = $self->receive_frame;
# Setting initial values for session id, as given from
# the stomp server
$self->session_id( $frame->headers->{session} );
$self->_connect_headers($conf);
return $frame;
}
Any settings I need to do before calling connect?
I had the same problem with sending messages from Perl to ApacheMQ.
(Perl + Net-Stomp-0.45 + apache-activemq-5.8)
It was just a little mistake.
It is important to set the correct transportConnectors in this file harddisk\apache-activemq-5.8\conf\activemq.xml.
<transportConnectors>
<transportConnector name="stomp" uri="stomp://localhost:61616"/>
</transportConnectors>
After that it works fine :D
For more information: http://activemq.apache.org/stomp.html
Maybe there are similar files in EMS.
This happens because the Stomp library receives an invalid (or no) response from the message broker.
Try to telnet to the message broker and see if it speaks Stomp at all.