Q:

C++ program to set MAC address in Linux Devices

0

C++ program to set MAC address in Linux Devices

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

#include <iostream>
using namespace std;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void  setMac(char * mac)
{
	char cmd[64];
	//network interface
	char nwkInf[5]="eth0";

	memset(cmd,0X00,64);
	
	//command to link down network
	sprintf((char *)cmd,(const char *)"ip link set %s down",nwkInf);
	system((const char *)cmd); 
	usleep(500);
	
	memset(cmd,0X00,64);
	//command to set MAC address
	sprintf((char *)cmd,(const char *)"ifconfig %s hw ether %s",nwkInf,mac);
	system((const char *)cmd);
	usleep(500);

	memset(cmd,0X00,64);
	//command to link up network
	sprintf((char *)cmd,(const char *)"ip link set %s up",nwkInf);
	system((const char *)cmd);
	usleep(500);

}

int main()
{
    //funcion call to set MAC address
	setMac("67:45:C4:AC:2F:CA");
	return 0;
}

Here, "67:45:C4:AC:2F:CA" is MAC address to be assigned.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to pad octets of IP Address with Zeros... >>
<< C++ program to get MAC address of Linux based netw...