linux C 獲取與修改IP位址

2021-08-26 14:11:18 字數 2189 閱讀 5843

#ifndef _include_setip_h_
#define_include_setip_h_
//設定ip位址
/*
* 函式名稱 : int setip(char *ip)
* 函式功能 : 設定系統ip位址
* 參 數 :
* char *ip :設定的ip位址,以點分十進位制的字串方式表示,如「192.168.0.5」
* 返 回 值 : 0 : 成功 ; -1 : 失敗
*/
intsetip(char*ip);
//獲取ip位址
/*
* 函式名稱 : char * getip(char *ip_buf)
* 函式功能 : 獲取系統ip位址
* 參 數 :
* char *ip_buf :用來存放ip位址的記憶體空間
* 返 回 值 : ip_buf : 存放ip位址的記憶體位址
*/
char* getip(char*ip_buf);
#endif

//_include_setip_h_

//end setip.h

#include
#include
#include
#include
#include
#include
#includein.h>
#includeif.h>
#include"setip.h"
//設定ip位址
/*
* 函式名稱 : int setip(char *ip)
* 函式功能 : 設定系統ip位址
* 參 數 :
* char *ip :設定的ip位址,以點分十進位制的字串方式表示,如「192.168.0.5」
* 返 回 值 : 0 : 成功 ; -1 : 失敗
*/
intsetip(char*ip)
addr = (structsockaddr_in *)&(temp.ifr_addr);
addr->sin_family = af_inet;
addr->sin_addr.s_addr = inet_addr(ip);
ret = ioctl(fd, siocsifaddr, &temp);
close(fd);
if(ret < 0)
return-1;
return0;
}
//獲取ip位址
/*
* 函式名稱 : char * getip(char *ip_buf)
* 函式功能 : 獲取系統ip位址
* 參 數 :
* char *ip_buf :用來存放ip位址的記憶體空間
* 返 回 值 : ip_buf : 存放ip位址的記憶體位址
*/
char* getip(char*ip_buf)
ret = ioctl(fd, siocgifaddr, &temp);
close(fd);
if(ret < 0)
returnnull;
myaddr = (structsockaddr_in *)&(temp.ifr_addr);
strcpy(ip_buf, inet_ntoa(myaddr->sin_addr));
returnip_buf;
}
/*測試程式*/
/*
int test_fun(int argc, char * argv)
*/
//end setip.c
**

linux c函式獲取系統IP位址

一,通過分析 etc hosts檔案裡對映關係獲取ip位址。include include int main gethostname hostname,sizeof hostname he gethostbyname hostname printf hostname s n hostname pri...

linux C語言獲取網絡卡對應IP位址

int get gw ip char eth,char ipaddr get ip address strncpy ifr.ifr name,eth,if namesize ifr.ifr name ifnamsiz 1 0 if ioctl sock fd,siocgifaddr,ifr 0 me...

修改Ip位址

臨時ip管理 ifconfig 網絡卡名 自己要設定的ip root fengjunhong fjh ifconfig ens33 192.168.1.120 重啟網路 centos7.x root fengjunhong fjh systemctl restart network centos6....