//獲取本機ip位址函式
view plain
copy to clipboard
print?
qstring getlocalip()
memset(&ifr_ip, 0, sizeof
(ifr_ip));
strncpy(ifr_ip.ifr_name, "eth0"
, sizeof
(ifr_ip.ifr_name) - 1);
if( ioctl( sock_get_ip, siocgifaddr, &ifr_ip) < 0 )
sin = (struct
sockaddr_in *)&ifr_ip.ifr_addr;
strcpy(ipaddr,inet_ntoa(sin->sin_addr));
printf("local ip:%s /n"
,ipaddr);
close( sock_get_ip );
return
qstring( ipaddr );
}
//修改本機ip位址的函式
intsetlocalip(
const
char
*ipaddr )
memset( &sin_set_ip, 0, sizeof
(sin_set_ip));
strncpy(ifr_set_ip.ifr_name, "eth0"
, sizeof
(ifr_set_ip.ifr_name)-1);
sin_set_ip.sin_family = af_inet;
sin_set_ip.sin_addr.s_addr = inet_addr(ipaddr);
memcpy( &ifr_set_ip.ifr_addr, &sin_set_ip, sizeof
(sin_set_ip));
if( ioctl( sock_set_ip, siocsifaddr, &ifr_set_ip) < 0 )
//設定啟用標誌
ifr_set_ip.ifr_flags |= iff_up |iff_running;
//get the status of the device
if( ioctl( sock_set_ip, siocsifflags, &ifr_set_ip ) < 0 )
close( sock_set_ip );
return
0;
}
獲取本機mac位址函式
qstring getlocalmac()
memset(&ifr_mac,0,sizeof
(ifr_mac));
strncpy(ifr_mac.ifr_name, "eth0"
, sizeof
(ifr_mac.ifr_name)-1);
if( (ioctl( sock_mac, siocgifhwaddr, &ifr_mac)) < 0)
sprintf(mac_addr,"%02x%02x%02x%02x%02x%02x"
, (unsigned char
)ifr_mac.ifr_hwaddr.sa_data[0],
(unsigned char
)ifr_mac.ifr_hwaddr.sa_data[1],
(unsigned char
)ifr_mac.ifr_hwaddr.sa_data[2],
(unsigned char
)ifr_mac.ifr_hwaddr.sa_data[3],
(unsigned char
)ifr_mac.ifr_hwaddr.sa_data[4],
(unsigned char
)ifr_mac.ifr_hwaddr.sa_data[5]);
printf("local mac:%s /n"
,mac_addr);
close( sock_mac );
return
qstring( mac_addr );
}
//獲取子網掩碼的函式
qstring getlocalnetmask()
memset(&ifr_mask, 0, sizeof
(ifr_mask));
strncpy(ifr_mask.ifr_name, ifname, sizeof
(ifr_mask.ifr_name )-1);
if( (ioctl( sock_netmask, siocgifnetmask, &ifr_mask ) ) < 0 )
net_mask = ( struct
sockaddr_in * )&( ifr_mask.ifr_netmask );
strcpy( netmask_addr, inet_ntoa( net_mask -> sin_addr ) );
printf("local netmask:%s/n"
,netmask_addr);
close( sock_netmask );
return
qstring( netmask_addr );
}
//修改子netmask的函式
qstring setlocalnetmask(
const
char
*sznetmask)
memset(&ifr_mask, 0, sizeof
(ifr_mask));
strncpy(ifr_mask.ifr_name, "eth0"
, sizeof
(ifr_mask.ifr_name )-1);
sin_net_mask = (struct
sockaddr_in *)&ifr_mask.ifr_addr;
sin_net_mask -> sin_family = af_inet;
inet_pton(af_inet, sznetmask, &sin_net_mask ->sin_addr);
if(ioctl(sock_netmask, siocsifnetmask, &ifr_mask ) < 0)
}
//獲去gateway
qstring getgateway()
while
(fgets(buf,
sizeof
(buf), fp) != null)
sscanf(buf, "%*s%*s%s"
, gateway);
printf("default gateway:%s/n"
, gateway);
pclose(fp);
return
qstring(gateway);
}
//設定閘道器
intsetgateway(
const
char
*szgateway)
strcpy(cmd, "route add default gw "
);
strcat(cmd, szgateway);
ret = system(cmd);
if(ret < 0)
return
ret;
}
linux 下獲取本地IP位址
include include include include include include include include include include include include include include int main int argc,char argv int getloc...
Linux下 獲取本機ip位址
查ip資訊可以使用ifconfig,但有時候我們需要使用指令碼語句來進行自動獲取。方法一 ifconfig a grep inet grep v 127.0.0.1 grep v inet6 awk tr d addr 當前語句可以排查出除了127.0.0.1之外的所有ip位址,但有些時候我們的虛擬...
LINUX下獲取IP位址和MAC位址
include include include include include include include include ifdef solaris include endif define maxinte ces 16 main argc,argv register int argc reg...