如何将 ARP 绑定从文件获取到数组中
我正在尝试将 Linux 中的 ARP 表获取到一个数组,代码如下。我总是在变量 ip 和 mac 中获取地址,但是当分配给数组时,它只是显示一些疯狂的数字。我做错了什么吗? (我不太擅长编程)
struct ARP_entry
{
char IPaddr;
char MACaddr;
char ARPstatus;
int timec;
};
static struct ARP_entry ARP_table[ARP_table_vel];
void getARP()
{
int i=0;
const char filename[] = "/proc/net/arp";
char ip[16], mac[18], output[128];
FILE *file = fopen(filename, "r");
if ( file )
{
char line [ BUFSIZ ];
fgets(line, sizeof line, file);
while ( fgets(line, sizeof line, file) )
{
char a,b,c,d;
if ( sscanf(line, "%s %s %s %s %s %s", &ip, &a, &b, &mac, &c, &d) < 10 )
{
if ( ARP_table_vel > i)
{
ARP_table[i].IPaddr = ip;
ARP_table[i].MACaddr = mac;
ARP_table[i].ARPstatus = STATUS_CON;
i++;
}
}
}
}
else
{
perror(filename);
}
I am trying to get the ARP table in Linux to an array with code posted below. I always get the addresses in the variables ip and mac, but when assigning to the array, it just shows some crazy numbers. Am I doing sth wrong? (I am not really skilled in programming)
struct ARP_entry
{
char IPaddr;
char MACaddr;
char ARPstatus;
int timec;
};
static struct ARP_entry ARP_table[ARP_table_vel];
void getARP()
{
int i=0;
const char filename[] = "/proc/net/arp";
char ip[16], mac[18], output[128];
FILE *file = fopen(filename, "r");
if ( file )
{
char line [ BUFSIZ ];
fgets(line, sizeof line, file);
while ( fgets(line, sizeof line, file) )
{
char a,b,c,d;
if ( sscanf(line, "%s %s %s %s %s %s", &ip, &a, &b, &mac, &c, &d) < 10 )
{
if ( ARP_table_vel > i)
{
ARP_table[i].IPaddr = ip;
ARP_table[i].MACaddr = mac;
ARP_table[i].ARPstatus = STATUS_CON;
i++;
}
}
}
}
else
{
perror(filename);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要修复您的结构并将
char
变量放入char
数组:然后您需要对数据进行正确的复制,以便可以保留它们:
最后,ARP 表有标题,因此您需要丢弃第一行。
You need to fix your structure and make the
char
variables intochar
arrays:Then you need to do proper copies of the data so you can preserve them:
Finally, the ARP table has a header, so you'll need to discard the first row.