Linux下获取和设置IP
在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作。
那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体。
/* Interface request structure used for socket ioctl's. All interface
ioctl's must have parameter definitions which begin with ifr_name.
The remainder may be interface specific. */
struct ifreq
{
# define IFHWADDRLEN
# define IFNAMSIZ IF_NAMESIZE
union
{
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
} ifr_ifrn;
union
{
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short int ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ]; /* Just fits the size */
char ifru_newname[IFNAMSIZ];
__caddr_t ifru_data;
} ifr_ifru;
};
# define ifr_name ifr_ifrn.ifrn_name /* interface name */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
# define ifr_addr ifr_ifru.ifru_addr /* address */
# define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
# define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
# define ifr_flags ifr_ifru.ifru_flags /* flags */
# define ifr_metric ifr_ifru.ifru_ivalue /* metric */
# define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
# define ifr_map ifr_ifru.ifru_map /* device map */
# define ifr_slave ifr_ifru.ifru_slave /* slave device */
# define ifr_data ifr_ifru.ifru_data /* for use by interface */
# define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
# define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
# define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
# define ifr_newname ifr_ifru.ifru_newname /* New name */
# define _IOT_ifreq _IOT(_IOTS(,,)
# define _IOT_ifreq_short _IOT(_IOTS(,,)
# define _IOT_ifreq_int _IOT(_IOTS(,,)
看完这个,结构体的内容一目了然
1.获取ip
int get_local_ip(char * dev_name, char * addr)
{
int ret;
int sockfd;
struct sockaddr_in sin;
struct ifreq req;
if(dev_name == NULL)
{
error_exit("dev_name");
}
)) < )
{
error_exit("ip socket");
}
strcpy(req.ifr_name, dev_name);
ret = ioctl(sockfd,SIOCGIFADDR,&req);
)
{
memcpy(&sin, &req.ifr_addr,sizeof(sin));
printf("local IP:%s\n",inet_ntoa(sin.sin_addr));
}
close(sockfd);
;
}
2.设置ip
#define error_exit(_errmsg_) \
error(EXIT_FAILURE, errno, _errmsg_)
#define MAC_ADDR_LEN 6
int set_local_ip(char * dev_name, const char * ip_addr)
{
int ret;
int sockfd;
struct sockaddr_in set_ip;
struct ifreq req;
if(ip_addr == NULL)
{
error_exit("ip_addr invalid");
}
)) < )
{
error_exit("ip socket");
}
bzero(&req, sizeof(req));
strncpy(req.ifr_name, dev_name, strlen(dev_name) );
bzero(&set_ip, sizeof(set_ip));
set_ip.sin_family = AF_INET;
set_ip.sin_addr.s_addr = inet_addr(ip_addr);
memcpy(&req.ifr_addr, &set_ip, sizeof(set_ip));
ret = ioctl(sockfd,SIOCSIFADDR,&req);
)
{
error_exit("set IP...");
}
close(sockfd);
;
}
整个demo为:
int main()
{
char * dev_name = "eth1";
] = "192.168.123.100";
set_local_ip(dev_name, ip_addr);
;
}
Linux下获取和设置IP的更多相关文章
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- linux下获取本机IP
转载:http://blog.chinaunix.net/uid-20593763-id-1620213.html 源代码级Unix/Linux 通用网卡IP地址获取方法 在Unix和Linux系统下 ...
- Linux 下获取本机IP
http://blog.csdn.net/K346K346/article/details/48231933 int main () { /* struct ifaddrs *ifap, *ifa; ...
- linux下获取外网IP
使用阿里云或者有多个网卡IP的机器需要取外网IP时,可以用下面这种 wget -qO - ifconfig.co 更多方法参考:https://yq.aliyun.com/ziliao/105999
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
// all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...
- Windows下获取本机IP地址方法介绍
Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...
- Linux下获取硬盘使用情况
Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...
- MongoDB在Linux下常用优化设置
MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...
- .net core在Linux下获取AD域信息
.net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...
随机推荐
- 【BZOJ4872】【Shoi2017】分手是祝愿
Time Limit: 20 Sec Memory Limit: 512 MB Description Zeit und Raum trennen dich und mich. 时空将你我分开 ...
- C++11新特性——大括号初始化
C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...
- spring管理hibernate,mybatis,一级缓存失效原因
mybatis缓存:一级缓存和二级缓存 hibernate缓存:一级缓存和二级缓存 关于缓存: 缓存是介于物理数据源与应用程序之间,是对数据库中的数据复制一份临时放在内存中的容器, 其作用是为了减少应 ...
- [POI2012]STU-Well
题意翻译 给定一个非负整数序列A,每次操作可以选择一个数然后减掉1,要求进行不超过m次操作使得存在一个Ak=0且max(∣xi−xi−1∣)最小,输出这个最小值以及此时最小的k (1≤n≤1 000 ...
- 【agc003D】Anticube
Portal --> agc003D Description 给你\(n\)个数,要从里面选出最多的数满足这些选出来的数中任意两个数的乘积都不是立方数 Solution (为什么感觉最近这种解法 ...
- golang管道
golang中的channel channel用于goroutine之间的通信 如果不用channel,使用共享全局变量的方式,需要加锁 // synchornized 同步 // golang中的 ...
- Qt error ------ no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))
connect(ui->spinBox_luminosity,&QSpinBox::valueChanged, ui->horizontalSlider_luminosity, & ...
- 使用MyEclipse 2014创建项目
1. 打开MyEclipse 2014,如果是第一次运行,会提示设置workspace路径,如图: WorkSpace路径是指日后你自己利用MyEclipse创建项目时,项目文件的存放路径.通常不要放 ...
- 关于Linux用户名
1.创建/删除/修改用户名 useradd 选项 用户名其中各选项含义如下: 代码:-c comment 指定一段注释性描述.-d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建 ...
- MySQL完整复制表到另一个新表
1. 复制表结构 CREATE TABLE newuser LIKE user; 2. 导入数据 INSERT INTO newauser SELECT * FROM user;