How to change mac address on Irix

发表于:2007-05-26来源:作者:点击数: 标签:
ItestitatmyO200,itworkwell. #includestdio.h #includeunistd.h #includesys/ioctl.h #includesys/types.h #includesys/socket.h #includenet/raw.h #includenet/if.h #includenet/soioctl.h main(intargc,char**argv) { intcnt,ret,sock,a1,a2,a3,a4,a5,a6

I test it at my O200, it work well.  
   
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/raw.h>
#include <net/if.h>
#include <net/soioctl.h>

main(int argc,char **argv)
{
        int cnt,ret,sock,a1,a2,a3,a4,a5,a6;
        struct ifreq ifdat;
        struct sockaddr_raw sr;

        if (argc<3)
        {
                printf("usage : %s <interface> <eaddr>\n",argv[0]);
                exit(1);
        }

        sock=socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);

        if (sock<0) {
                perror("socket:");
                exit(1);
        }

        strcpy(ifdat.ifr_name, argv[1]);
        ret=ioctl(sock, SIOCGIFADDR, &ifdat ;
        if (ret<0)
        {
                printf("ret=%d\n", ret);
                perror("ioctl:");
                exit(1);
        }

        printf("Current address : %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
                ifdat.ifr_ifru.ifru_enaddr[2], ifdat.ifr_ifru.ifru_enaddr[3],
                ifdat.ifr_ifru.ifru_enaddr[4], ifdat.ifr_ifru.ifru_enaddr[5],
                ifdat.ifr_ifru.ifru_enaddr[6], ifdat.ifr_ifru.ifru_enaddr[7]);


        sscanf(argv[2],"%x:%x:%x:%x:%x:%x", &a1,&a2,&a3,&a4,&a5,&a6);
        printf("setting %s to %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
                                argv[1],a1,a2,a3,a4,a5,a6);

        ifdat.ifr_ifru.ifru_enaddr[2]=a1; ifdat.ifr_ifru.ifru_enaddr[3]=a2;
        ifdat.ifr_ifru.ifru_enaddr[4]=a3; ifdat.ifr_ifru.ifru_enaddr[5]=a4;
        ifdat.ifr_ifru.ifru_enaddr[6]=a5; ifdat.ifr_ifru.ifru_enaddr[7]=a6;

        ret=ioctl(sock, SIOCSIFADDR, &ifdat ;
        if (ret<0)
        {
                printf("ret=%d\n", ret);
                perror("ioctl:");
                exit(1);
        }
        printf("Done\n");
        return 0;
}

原文转自:http://www.ltesting.net