linux内核获取本机网卡的mac和ip地址

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/in.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>   /** type  */
#include <net/ip.h>
#include <net/netevent.h>
#include <linux/inetdevice.h>

/**
 *  sudo  arp-scan -I eth0  --localnet
 */


#define HBUFFERLEN 30
struct net_device *mynet_device;

int myprobe(struct platform_device *pdev) {
	int k, j;
	char hbuffer[HBUFFERLEN];
	struct in_ifaddr    *ip;
	mynet_device = dev_get_by_name(&init_net, "eth0");
	pr_info("mynet_device name: %s\n", mynet_device->name);
	pr_info("mynet_device type: %d\n", mynet_device->type);

	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < mynet_device->addr_len; j++) {
		hbuffer[k++] = hex_asc_hi(mynet_device->perm_addr[j]);
		hbuffer[k++] = hex_asc_lo(mynet_device->perm_addr[j]);
		hbuffer[k++] = ':';
	}
	if (k != 0)
		--k;
	hbuffer[k] = 0;
	pr_info("mac: %s\n", hbuffer);
	ip  = 	mynet_device->ip_ptr->ifa_list;
	printk(KERN_INFO "ipaddr=%pI4\n",
		       &(ip->ifa_local));
	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner =
THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

这里是全部的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值