LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to call the arp linux shell command from within a perl program (https://www.linuxquestions.org/questions/programming-9/how-to-call-the-arp-linux-shell-command-from-within-a-perl-program-156159/)

Bassam 03-11-2004 01:27 AM

How to call the arp linux shell command from within a perl program
 
Hi Guys,
I am trying to write a perl function that add or delete and ARP cache entry. I wrote my own code as down, but unfortunately it doesn't work as I expected. I want to call arp function from within my function so I can add different entries every time I call my function. so is there any different solution for my problem?

If I use the "system(arp -i eth1 -s $NODE_IP $NODE_MAC)" instead of

Code:


#!/usr/bin/perl

sub add_mac_address{
        my($OPERATION, $NODE_IP, $NODE_MAC) = @_;
        open (MAC, "| arp") || die("Unable to write to the ARP cache");
        $OPERATION =~ s/ADD/add/;
        $OPERATION =~ s/DELETE/delete/;
        if ($OPERATION eq "add"){
                print MAC ("-i eth1 -s $NODE_IP $NODE_MAC");
        }
        elsif($OPERATION eq "delete"){
                print MAC ("-i eth1 -d $NODE_IP");
        }
        else{
                die ("Bad operation paramete\n");
        }
        close(MAC);
}

add_mac_address("ADD", "192.168.0.2", "00:30:84:9E:C8:9E");

Thanks for support
Bassam

Bassam 03-11-2004 02:00 AM

Found it
 
Ok Guys, I found the solution. The code should be like this:

Code:

#!/usr/bin/perl

sub add_mac_address{
        my($OPERATION, $NODE_IP, $NODE_MAC) = @_;
        #open (MAC, "| arp") || die("Unable to write to the ARP cache\n");
        $OPERATION =~ s/ADD/add/;
        $OPERATION =~ s/DELETE/delete/;
        if ($OPERATION eq "add"){
                #print MAC ("-i eth1 -s $NODE_IP $NODE_MAC\n");
                system("arp -i eth1 -s $NODE_IP $NODE_MAC");
        }
        elsif($OPERATION eq "delete"){
                #print MAC ("-i eth1 -d $NODE_IP\n");
                system("arp -i eth1 -d $NODE_IP");
        }
        else{
                die ("Bad operation paramete\n");
        }
        #close(MAC);
}

add_mac_address("ADD", "192.168.0.2", "00:30:84:9E:C8:9E");

My mistake is that I did not surround the parameters of system function with double qoute :)

Regards
Bassam


All times are GMT -5. The time now is 04:08 AM.