/************************************************************************************
*Description: sdk_net_public
*Created on: 2023-2-16
*Author: yanwei
************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "stdint.h"
#include "sdk_net_public.h"
//获取指定网卡的IP地址
int32_t sdk_net_ip_get ( net_device_type_e nic, uint8_t * p_ip )
{
FILE *fp = NULL;
char buffer[32]={0};
char buffer2[32]={0};
int8_t cmd[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $2}'", "wlan0");
break;
case WLAN1:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $2}'", "wlan1");
break;
case ETH0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $2}'", "eth0");
break;
case ETH1:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $2}'", "eth1");
break;
case PPP0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $2}'", "usb0");
break;
default:
printf("input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
fgets(buffer, sizeof(buffer), fp);
if(memcmp(buffer, buffer2, sizeof(buffer))==0)
{
printf("SDK erro: this net device is no exit !!!\n");
return -1;
}
memcpy(p_ip ,buffer+5,sizeof(buffer)-5);
pclose(fp);
return 0;
}
//set指定网卡的IP地址
int32_t sdk_net_ip_set ( net_device_type_e nic, uint8_t * p_ip )
{
FILE *fp = NULL;
char buffer[32]={0};
int8_t cmd[128]={0};
int8_t cmd2[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "ifconfig %s %s", "wlan0",p_ip );
break;
case WLAN1:
sprintf(cmd, "ifconfig %s %s", "wlan1",p_ip );
break;
case ETH0:
sprintf(cmd, "ifconfig %s %s", "eth0",p_ip );
break;
case ETH1:
sprintf(cmd, "ifconfig %s %s", "eth1",p_ip );
break;
case PPP0:
sprintf(cmd, "ifconfig %s %s", "usb0",p_ip );
break;
default:
printf("SDK input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
pclose(fp);
sprintf(cmd2, "echo %s> /etc/init.d/Net_sdk_set/sdk_net_ip_set.sh", cmd );
fp=popen(cmd2, "r");
pclose(fp);
return 0;
}
//------------------------------fail
//获取指定网卡的子网掩码地址
int32_t sdk_net_subnetmask_get ( uint32_t nic, uint8_t * p_subnet)
{
FILE *fp = NULL;
char buffer[32]={0};
char buffer2[32]={0};
int8_t cmd[64]={0}; //减小内存
switch(nic)
{
case WLAN0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $4}'", "wlan0");
break;
case WLAN1:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $4}'", "wlan1");
break;
case ETH0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $4}'", "eth0");
break;
case ETH1:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $4}'", "eth1");
break;
case PPP0:
sprintf(cmd, "ifconfig %s | grep \"inet \" | awk '{print $4}'", "usb0");//最好是用宏定义
break;
default:
printf("input error\n");//最好是减少打印
return -2;
break;
}
fp=popen(cmd, "r");
fgets(buffer, sizeof(buffer), fp);
if(memcmp(buffer, buffer2, sizeof(buffer))==0)
{
printf("SDK erro: this net device is no exit !!!\n");
return -1;
}
memcpy(p_subnet,buffer+5,sizeof(buffer)-5);
pclose(fp);
return 0;
}
//--------------------------------------ok
//设置指定网卡的子网掩码地址
int32_t sdk_net_subnetmask_set ( net_device_type_e nic, uint8_t * p_subnet )
{
FILE *fp = NULL;
char buffer[32]={0};
int8_t cmd[64]={0};
int8_t cmd2[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "ifconfig %s netmask %s", "wlan0",p_subnet );
break;
case WLAN1:
sprintf(cmd, "ifconfig %s netmask %s", "wlan1",p_subnet );
break;
case ETH0:
sprintf(cmd, "ifconfig %s netmask %s", "eth0",p_subnet );
break;
case ETH1:
sprintf(cmd, "ifconfig %s netmask %s", "eth1",p_subnet );
break;
case PPP0:
sprintf(cmd, "ifconfig %s netmask %s", "usb0",p_subnet );
break;
default:
printf("SDK input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
pclose(fp);
sprintf(cmd2, "echo %s> /etc/init.d/Net_sdk_set/sdk_net_subnetmask_set.sh", cmd );
fp=popen(cmd2, "r");
pclose(fp);
return 0;
}
//------------------------------ok
//get 指定网卡的网关地址
int32_t sdk_net_gateway_get ( net_device_type_e nic, uint8_t *p_gateway)
{
FILE *fp = NULL;
char buffer[32]={0};
char buffer2[32]={0};
int8_t cmd[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "ip route show|grep %s|grep \"default via\" | awk '{print $3}'", "wlan0");
break;
case WLAN1:
sprintf(cmd, "ip route show|grep %s|grep \"default via\" | awk '{print $3}'", "wlan1");
break;
case ETH0:
sprintf(cmd, "ip route show|grep %s|grep \"default via\" | awk '{print $3}'", "eth0");
break;
case ETH1:
sprintf(cmd, "ip route show|grep %s|grep \"default via\" | awk '{print $3}'", "eth1");
break;
case PPP0:
sprintf(cmd, "ip route show|grep %s|grep \"default via\" | awk '{print $3}'", "usb0");
break;
default:
printf("SDK input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
fgets(buffer, sizeof(buffer), fp);
if(memcmp(buffer, buffer2, sizeof(buffer))==0)
{
printf("SDK erro: this net device is no exit !!!\n");
return -1;
}
memcpy(p_gateway,buffer,sizeof(buffer));
pclose(fp);
return 0;
}
//--------------------------------ok
//set 指定网卡的网关地址
int32_t sdk_net_gateway_set ( net_device_type_e nic, uint8_t *p_gateway)
{
FILE *fp = NULL;
char buffer[32]={0};
int8_t cmd[64]={0};
int8_t cmd2[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "route add default gw %s dev %s",p_gateway,"wlan0");
break;
case WLAN1:
sprintf(cmd, "route add default gw %s dev %s",p_gateway,"wlan1");
break;
case ETH0:
sprintf(cmd, "route add default gw %s dev %s",p_gateway,"eth0");
break;
case ETH1:
sprintf(cmd, "route add default gw %s dev %s",p_gateway,"eth1");
break;
case PPP0:
sprintf(cmd, "route add default gw %s dev %s",p_gateway,"usb0");
break;
default:
printf("SDK input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
pclose(fp);
sprintf(cmd2, "echo %s> /etc/init.d/Net_sdk_set/sdk_net_gateway_set.sh", cmd );
fp=popen(cmd2, "r");
pclose(fp);
return 0;
}
//---------------------------------ok
//set dns
int32_t sdk_net_dns_set ( net_device_type_e nic, uint8_t *p_dns)
{
FILE *fp = NULL;
char buffer[32]={0};
int8_t cmd[64]={0};
int8_t cmd2[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "echo nameserver %s > /etc/resolv.conf",p_dns);
break;
case WLAN1:
sprintf(cmd, "echo nameserver %s > /etc/resolv.conf",p_dns);
break;
case ETH0:
sprintf(cmd, "echo nameserver %s > /etc/resolv.conf",p_dns);
break;
case ETH1:
sprintf(cmd, "echo nameserver %s > /etc/resolv.conf",p_dns);
break;
case PPP0:
sprintf(cmd, "echo nameserver %s > /etc/resolv.conf",p_dns);
break;
default:
printf("SDK input error\n");
return -2;
break;
}
fp=popen(cmd, "r");
pclose(fp);
sprintf(cmd2, "echo \"%s\"> /etc/init.d/Net_sdk_set/sdk_net_dns_set.sh", cmd );
fp=popen(cmd2, "r");
pclose(fp);
return 0;
}
//----------------------------------------ok
//get nic dns
int32_t sdk_net_dns_get ( net_device_type_e nic, uint8_t *p_dns)
{
FILE *fp = NULL;
char buffer[32]={0};
char buffer2[32]={0};
int8_t cmd[64]={0};
switch(nic)
{
case WLAN0:
sprintf(cmd, "cat /etc/resolv.conf |grep nameserver|awk '{print $2}'");
break;
c
没有合适的资源?快使用搜索试试~ 我知道了~
linux 平台软件应用程序例程tar.gz
需积分: 8 0 下载量 22 浏览量
2023-03-20
10:43:10
上传
评论
收藏 52KB GZ 举报
温馨提示
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共38个文件
c:13个
h:6个
o:6个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
linux appcode
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 38 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/fb4c3aa90df649cba5304f7a8e54128b_weixin_38390808.jpg!1)
一点点伟大的技术
- 粉丝: 59
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)