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: Edit Log



SteffenBaier
Novice

Aug 2, 2012, 4:54 AM


Views: 1254
Repeat Perl Sub routine based on Argument

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)


Edit Log:
Post edited by SteffenBaier (Novice) on Aug 2, 2012, 8:07 AM


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

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