CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Repeat Perl Sub routine based on Argument

 



SteffenBaier
Novice

Aug 2, 2012, 4:54 AM

Post #1 of 5 (1114 views)
Repeat Perl Sub routine based on Argument Can't Post

Hi all,

I am repeat a perl script a defined amount of times based on an ARGV value.

The code so far:


Code
#!/usr/bin/perl  
use Socket;

# IP Address of target phone
my $phoneip = $ARGV[0];
# SIP Extension of target phone
my $number = $ARGV[1];
# Times to repeat the Invite and Cancel
my $amount = $ARGV[2];


# define counter
$count = 0;



while ($count < $amount) {

SendSIPTo("$phoneip"); # IP of the phone

#This section is simply to display the Call Setup visually in the command line#
# and can be completely removed as it is for displaying the Information only#

print "\n";
print "IP Address of Phone to be called: $phoneip\n";
print "\n";
print "Number that the Phone should call: $number\n";
print "\n";
print "Unique Call ID: $tm\n";
print "\n";
print "Times this has been done $count times";
print "\n";


#####################################
#This section creates the SIP INVITE#
#####################################

sub SendSIPTo {
$phone_ip = shift;

$MESG1="INVITE sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 INVITE
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";

$proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
$iaddr = inet_aton("0.0.0.0");
$paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
$port=5060;
$hisiaddr = inet_aton($phone_ip) ;
$hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG1, 0,$hispaddr ) || warn "send $host $!\n";


# Wait 2 seconds
sleep(2);

#####################################
#This section creates the SIP CANCEL#
#####################################

$MESG2="CANCEL sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 CANCEL
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";


$proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
$iaddr = inet_aton("0.0.0.0");
$paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
$port=5060;
$hisiaddr = inet_aton($phone_ip) ;
$hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG2, 0,$hispaddr ) || warn "send $host $!\n";


}
$count++;
}


The part that is just printed to the screen works and is repeated and the count amount also works but the sub routine is only actioned once.

Any little tips for a perl beginner ;-)

THX

Steff


(This post was edited by SteffenBaier on Aug 2, 2012, 8:07 AM)


Laurent_R
Enthusiast / Moderator

Aug 2, 2012, 5:04 AM

Post #2 of 5 (1113 views)
Re: [SteffenBaier] Repeat Perl Sub routine based on Argument [In reply to] Can't Post

It looks like you are defining the subroutine within the while loop. Move this definition afther the closing curly brace of the while loop.


SteffenBaier
Novice

Aug 2, 2012, 5:28 AM

Post #3 of 5 (1111 views)
Re: [Laurent_R] Repeat Perl Sub routine based on Argument [In reply to] Can't Post

Hi Laurent,

its actually the subroutine part I would like to repeat the ARGV defined amount. I am not to bothered about the PRINT part.

Any more / different ideas ?

I tried to move the Subroutine between the last two } but is still only prints the PRINT part and actions the subroutine once and then repeats the PRINT part for the defined amount.

Steff


FishMonger
Veteran / Moderator

Aug 2, 2012, 7:36 AM

Post #4 of 5 (1105 views)
Re: [SteffenBaier] Repeat Perl Sub routine based on Argument [In reply to] Can't Post

I fixed some of the problems in your code, but there are more that you should work on.

Try this:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Socket;

# IP Address of target phone
my $phoneip = $ARGV[0];
# SIP Extension of target phone
my $number = $ARGV[1];
# Times to repeat the Invite and Cancel
my $amount = $ARGV[2];

# Random Number to be used for unique call ID and calling Phone Number
my $tm = time();

#This section is simply to display the Call Setup visually in the command line#
# and can be completely removed as it is for displaying the Information only#

print "IP Address of Phone to be called: $phoneip\n\n";
print "Number that the Phone should call: $number\n\n";
print "Unique Call ID: $tm\n\n";
print "This will been done $amount times\n\n";


for (1..$amount) {

SendSIPTo($phoneip); # IP of the phone

}


sub SendSIPTo {

my $phone_ip = shift;
my $host = '0.0.0.0.0';

#####################################
#This section creates the SIP INVITE#
#####################################

my $MESG1="INVITE sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 INVITE
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";

my $proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
my $iaddr = inet_aton("0.0.0.0");
my $paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
my $port=5060;
my $hisiaddr = inet_aton($phone_ip) ;
my $hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG1, 0,$hispaddr ) || warn "send $host $!\n";


# Wait 2 seconds
sleep(2);

#####################################
#This section creates the SIP CANCEL#
#####################################

my $MESG2="CANCEL sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 CANCEL
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";


$proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
$iaddr = inet_aton("0.0.0.0");
$paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
$port=5060;
$hisiaddr = inet_aton($phone_ip) ;
$hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG2, 0,$hispaddr ) || warn "send $host $!\n";

}



SteffenBaier
Novice

Aug 2, 2012, 8:03 AM

Post #5 of 5 (1099 views)
Re: [FishMonger] Repeat Perl Sub routine based on Argument [In reply to] Can't Post

Hi FishMonger,

thanks for this !

I had my own little "bug" within it as I use the $tm to get the linux time in order to have a unique number that can be used as the call id and also as a simulated Phone Number.

Using this before the "loop" will keep the same value and the phone will reject subsequent invites due the same value as the previous.

I changed it to this and it is working for my own needs


Code
#!/usr/bin/perl  

use strict;
use warnings;
use Socket;

# IP Address of target phone
my $phoneip = $ARGV[0];
# SIP Extension of target phone
my $number = $ARGV[1];
# Times to repeat the Invite and Cancel
my $amount = $ARGV[2];

# Random Number to be used for unique call ID and calling Phone Number
my $tm = time();

#################################################################################
# This section is simply to display the Call Setup visually in the command line #
# and can be completely removed as it is for displaying the Information only #
#################################################################################

print "\n";
print "IP Address of Phone to be called: $phoneip\n\n";
print "Number that the Phone should call: $number\n\n";
# print "Unique Call ID: $tm\n\n";
print "This will been done $amount times\n\n";


for (1..$amount) {

SendSIPTo($phoneip); # IP of the phone

}


sub SendSIPTo {

my $phone_ip = shift;
my $host = '10.252.32.100';
# Random Number to be used for unique call ID and calling Phone Number
my $tm = time();

print "Unique Call ID: $tm\n\n";

#####################################
#This section creates the SIP INVITE#
#####################################

my $MESG1="INVITE sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 INVITE
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";

my $proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
my $iaddr = inet_aton("0.0.0.0");
my $paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
my $port=5060;
my $hisiaddr = inet_aton($phone_ip) ;
my $hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG1, 0,$hispaddr ) || warn "send $host $!\n";


# Wait 2 seconds
sleep(2);

#####################################
#This section creates the SIP CANCEL#
#####################################

my $MESG2="CANCEL sip:$tm\@$phone_ip SIP/2.0
Via: SIP/2.0/UDP 10.252.75.119;branch=z9hG4bK13579926
From: \"Asterisk\" <sip:$tm\@10.252.75.119>;tag=as370e68ea
To: <sip:\$number\@$phone_ip>
Call-ID: $tm
CSeq: 1 CANCEL
Contact: sip:10.252.75.119:5060
Max-Forwards: 10
Content-Type: application/sdp
#INVITE-To: <sip:$number\@10.252.75.119>";


$proto = getprotobyname('udp');
socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) ;
$iaddr = inet_aton("0.0.0.0");
$paddr = sockaddr_in(5060, $iaddr);
bind(SOCKET, $paddr) ;
$port=5060;
$hisiaddr = inet_aton($phone_ip) ;
$hispaddr = sockaddr_in($port, $hisiaddr);
send(SOCKET, $MESG2, 0,$hispaddr ) || warn "send $host $!\n";

# Wait 2 seconds
sleep(2);

}


I may work on the actual output so wireshark can recognize it as a SIP package but at present it works for what I had in mind.

Above simply calls a phone and then hangs up in order to get the missed calls counter up.

THX

Steffen

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives