LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2004, 01:27 AM   #1
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Rep: Reputation: 15
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

Last edited by Bassam; 03-11-2004 at 01:32 AM.
 
Old 03-11-2004, 02:00 AM   #2
Bassam
Member
 
Registered: Mar 2003
Location: Malaysia
Posts: 63

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
call a c program in a shell script jagman Programming 4 04-05-2005 04:58 PM
How to Call a C program from Perl CGI Script anoop_cn Programming 1 05-11-2004 04:37 PM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 07:31 PM
call shell or other program out of my_program lea Programming 3 10-09-2002 10:51 AM
how to put a shell call in a C/C++ program? Hano Programming 8 05-19-2002 02:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration