ifconfig enp3s0 | awk -F ':' '{print $2}'
FILE *fp = NULL;
char buff[128] = {0};
char *cmd = "ifconfig enp3s0 | awk -F ':' 'NR==2{print$2}'";
fp = popen(cmd,"r");
if(fp == NULL)
{
perror("the fp is NULL");
}
fscanf(fp,"%s",buff);
fclose(fp);
printf("the buff is %s\n",buff);
return 0
void show_ip(char **ip,const char *vpn) { FILE *fp = NULL; char *buff = NULL; char buff_ip[128] = {0}; int i = 0; if(!strncmp(vpn,"l2tp",4)) { char *cmd = "ifconfig l2tp-vpn | awk -F ':' 'NR==2{print$2}'"; }else if(!strncmp(vpn,"l2tp",4)){ char *cmd = "ifconfig ppp0-vpn | awk -F ':' 'NR==2{print$2}'"; } fp = popen(cmd,"r"); if(fp == NULL) { perror("the fp popen error"); return ; } fscanf(fp,"%s",buff_ip); pclose(fp); buff = *ip; while(buff_ip[i] != '\0') { *buff = buff_ip[i]; buff++; i++; } buff = NULL; printf("the buff is %s\n",buff); } /*测试*/ int main() { char *ip; char *vpn = "l2tp"; ip = (char *)malloc(18); show_ip(&ip,l2tp); printf("the ip is %s\n",ip); free(ip); ip = NULL; }
指针的赋值与释放需要注意,不用的指针给到NULL