ifconfig.docx
- 文档编号:6195433
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:15
- 大小:16.01KB
ifconfig.docx
《ifconfig.docx》由会员分享,可在线阅读,更多相关《ifconfig.docx(15页珍藏版)》请在冰豆网上搜索。
ifconfig
#include
#include
#include
#include
#include
#include
intinfo_list(intall);
intget_interface_info(char*interface_name);
intset_interface(char*interface_name);
intget_ipaddr(char*interface_name);
intget_mac(char*interface_name);
intget_netmask(char*interface_name);
intget_broadcast(char*interface_name);
intset_ipaddr(char*interface_name,char*ip);
intset_mac(char*interface_name,char*mac);
intset_netmask(char*interface_name,char*netmask);
intset_broadcast(char*interface_name,char*broadcast);
intinterface_up(char*interface_name);
intinterface_down(char*interface_name);
intusage();
intmain(intargc,char*argv[])
{
if(argc==1)
{
info_list(0);
return0;
}
if(argc==2)
{
if(strcmp(argv[1],"all")==0)
{
info_list
(1);
return0;
}
get_interface_info(argv[1]);
return0;
}
if(argc==3)
{
if(strcmp(argv[2],"set")==0)
{
set_interface(argv[1]);
return0;
}
if(strcmp(argv[2],"up")==0)
{
interface_up(argv[1]);
return0;
}
if(strcmp(argv[2],"down")==0)
{
interface_down(argv[1]);
return0;
}
usage();
return0;
}
usage();
return0;
}
intinfo_list(intall)
{
intlen;
char*ifreq_pointer;
len=10*sizeof(structifreq);
ifreq_pointer=(char*)malloc(len);
structifconfget_info;
get_info.ifc_len=len;
get_info.ifc_ifcu.ifcu_buf=ifreq_pointer;
intsockfd;
sockfd=socket(PF_INET,SOCK_STREAM,0);
ioctl(sockfd,SIOCGIFCONF,&get_info);
intcount;
count=get_info.ifc_len/sizeof(structifreq);
structsockaddr_in*return_sockaddr;
structifreq*result=(structifreq*)ifreq_pointer;
inti=0;
if(all==1)
for(i;i { printf("%s/n",result[i].ifr_name); get_ipaddr(result[i].ifr_name); get_mac(result[i].ifr_name); get_netmask(result[i].ifr_name); get_broadcast(result[i].ifr_name); } else { structifreqtmp; ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); exit(-1); } for(i;i { strcpy(tmp.ifr_name,result[i].ifr_name); if(ioctl(s,SIOCGIFFLAGS,&tmp)<0) { perror("ioctl"); exit(-1); } if((tmp.ifr_ifru.ifru_flags&IFF_UP)==0) continue; printf("%s/n",tmp.ifr_name); get_ipaddr(tmp.ifr_name); get_mac(tmp.ifr_name); get_netmask(tmp.ifr_name); get_broadcast(tmp.ifr_name); } } exit(0); } intget_ipaddr(char*interface_name) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); if(ioctl(s,SIOCGIFADDR,&ifr)<0) { perror("ioctl"); return-1; } structsockaddr_in*ptr; ptr=(structsockaddr_in*)&ifr.ifr_ifru.ifru_addr; printf("IP: %s/n",inet_ntoa(ptr->sin_addr)); return0; } intget_mac(char*interface_name) { if(strcmp(interface_name,"lo")==0) { return0; } ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); if(ioctl(s,SIOCGIFHWADDR,&ifr)! =0) { perror("ioctl"); return-1; } u_char*ptr; ptr=(u_char*)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0]; printf("MAC: %02x-%02x-%02x-%02x-%02x-%02x/n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5)); return0; } intget_netmask(char*interface_name) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); if(ioctl(s,SIOCGIFNETMASK,&ifr)<0) { perror("ioctl"); return-1; } structsockaddr_in*ptr; ptr=(structsockaddr_in*)&ifr.ifr_ifru.ifru_netmask; printf("Netmask: %s/n",inet_ntoa(ptr->sin_addr)); return0; } intget_broadcast(char*interface_name) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); if(ioctl(s,SIOCGIFBRDADDR,&ifr)<0) { perror("ioctl"); return-1; } structsockaddr_in*ptr; ptr=(structsockaddr_in*)&ifr.ifr_ifru.ifru_broadaddr; printf("Broadcast: %s/n",inet_ntoa(ptr->sin_addr)); return0; } intget_interface_info(char*interface_name) { get_ipaddr(interface_name); get_mac(interface_name); get_netmask(interface_name); get_broadcast(interface_name); return0; } intset_interface(char*interface_name) { charip[18]; charmac_addr[20]; charnetmask[18]; charbroadcast[18]; printf("Change"); get_ipaddr(interface_name); printf("To: "); fgets(ip,17,stdin); if(strcmp(ip,"/n")==0) gotoset_mac; set_ipaddr(interface_name,ip); set_mac: printf("Change"); get_mac(interface_name); printf("To: "); fgets(mac_addr,19,stdin); if(strcmp(mac_addr,"/n")==0) gotoset_netmask; set_mac(interface_name,mac_addr); set_netmask: printf("Change"); get_netmask(interface_name); printf("To: "); fgets(netmask,17,stdin); if(strcmp(netmask,"/n")==0) gotoset_broadcast; set_netmask(interface_name,netmask); set_broadcast: printf("Change"); get_broadcast(interface_name); printf("To: "); fgets(broadcast,17,stdin); if(strcmp(broadcast,"/n")==0) return0; set_broadcast(interface_name,broadcast); return0; } intinterface_up(char*interface_name) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); shortflag; flag=IFF_UP; if(ioctl(s,SIOCGIFFLAGS,&ifr)<0) { perror("ioctl"); return-1; } ifr.ifr_ifru.ifru_flags|=flag; if(ioctl(s,SIOCSIFFLAGS,&ifr)<0) { perror("ioctl"); return-1; } return0; } intinterface_down(char*interface_name) { if(strcmp(interface_name,"lo")==0) { printf("Youcan'tpulldowninterfacelo/n"); return0; } ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); shortflag; flag=~IFF_UP; if(ioctl(s,SIOCGIFFLAGS,&ifr)<0) { perror("ioctl"); return-1; } ifr.ifr_ifru.ifru_flags&=flag; if(ioctl(s,SIOCSIFFLAGS,&ifr)<0) { perror("ioctl"); return-1; } return0; } intset_ipaddr(char*interface_name,char*ip) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); structsockaddr_inaddr; bzero(&addr,sizeof(structsockaddr_in)); addr.sin_family=PF_INET; inet_aton(ip,&addr.sin_addr); memcpy(&ifr.ifr_ifru.ifru_addr,&addr,sizeof(structsockaddr_in)); if(ioctl(s,SIOCSIFADDR,&ifr)<0) { perror("ioctl"); return-1; } return0; } intset_mac(char*interface_name,char*mac_addr) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); if(ioctl(s,SIOCGIFHWADDR,&ifr)<0) { perror("ioctl"); return-1; } sa_family_tget_family; get_family=ifr.ifr_ifru.ifru_hwaddr.sa_family; interface_down(interface_name); bzero(&ifr,sizeof(structifreq)); strcpy(ifr.ifr_name,interface_name); ifr.ifr_ifru.ifru_hwaddr.sa_family=get_family; shorttmp; inti,j; j=0; for(i=0;i<17;i+=3) { if(mac_addr[i]<58&&mac_addr[i]>47) tmp=mac_addr[i]-48; if(mac_addr[i]<71&&mac_addr[i]>64) tmp=mac_addr[i]-55; if(mac_addr[i]<103&&mac_addr[i]>96) tmp=mac_addr[i]-87; tmp=tmp<<4; if(mac_addr[i+1]<58&&mac_addr[i+1]>47) tmp|=(mac_addr[i+1]-48); if(mac_addr[i+1]<71&&mac_addr[i+1]>64) tmp|=(mac_addr[i+1]-55); if(mac_addr[i+1]<103&&mac_addr[i+1]>96) tmp|=(mac_addr[i+1]-87); memcpy(&ifr.ifr_ifru.ifru_hwaddr.sa_data[j++],&tmp,1); } if(ioctl(s,SIOCSIFHWADDR,&ifr)<0) { perror("ioctl"); return-1; } interface_up(interface_name); return0; } intset_netmask(char*interface_name,char*netmask) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); structsockaddr_innetmask_addr; bzero(&netmask_addr,sizeof(structsockaddr_in)); netmask_addr.sin_family=PF_INET; inet_aton(netmask,&netmask_addr.sin_addr); memcpy(&ifr.ifr_ifru.ifru_netmask,&netmask_addr,sizeof(structsockaddr_in)); if(ioctl(s,SIOCSIFNETMASK,&ifr)<0) { perror("ioctl"); return-1; } return0; } intset_broadcast(char*interface_name,char*broadcast) { ints; if((s=socket(PF_INET,SOCK_STREAM,0))<0) { perror("Socket"); return-1; } structifreqifr; strcpy(ifr.ifr_name,interface_name); structsockaddr_inbroadcast_addr; bzero(&broadcast_addr,sizeof(structsockaddr_in)); broadcast_addr.sin_family=PF_INET; inet_aton(broadcast,&broadcast_addr.sin_addr); memcpy(&ifr.ifr_ifru.ifru_broadaddr,&broadcast_addr,sizeof(structsockaddr_in)); if(ioctl(s,SIOCSIFBRDADDR,&ifr)<0) { perror("ioctl"); return-1; } return0; } intusage(void) { printf("Usage: myifconfig[all]|[interface[set|up|down]]/n"); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ifconfig
![提示](https://static.bdocx.com/images/bang_tan.gif)