linux下常用的基本设置与操作C语言实现
获取系统时间
int getlocaltime(struct tm *time)
{
struct timeval tv;
time_t second;
struct tm *temp;
gettimeofday(&tv, NULL);
second = tv.tv_sec;
temp = localtime(&second);
temp->tm_year += ;
temp->tm_mon += ;
memcpy(time, temp,sizeof(struct tm));
;
}
设置系统时间
int setlocaltime(struct tm *tm)
{
time_t timesec;
struct timeval tv;
struct tm temptm;
memcpy(&temptm,tm,sizeof(temptm));
temptm.tm_year = tm->tm_year - ;
temptm.tm_yday = ;
temptm.tm_isdst = -; /* Be sure to recheck dst. */
timesec = mktime(&temptm);
tv.tv_sec = timesec;
tv.tv_usec = ;
settimeofday(&tv,); //设置系统时间
;
}
获取IP地址
void getip(unsigned char* ip_buf,char * name)
{
struct ifreq temp;
struct sockaddr_in *myaddr;
;
;
strcpy(temp.ifr_name, name);
))<)
{
return NULL;
}
ret = ioctl(fd, SIOCGIFADDR, &temp);
close(fd);
)
return NULL;
myaddr = (struct sockaddr_in *)&(temp.ifr_addr);
strcpy(ip_buf, inet_ntoa(myaddr->sin_addr));
printf("getip:%s\n",ip_buf);
}
设置IP地址
int setip(char* ip)
{
struct ifreq temp;
struct sockaddr_in *addr;
;
;
strcpy(temp.ifr_name, "eth0");
))<)
{
;
}
addr = (struct sockaddr_in *)&(temp.ifr_addr);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(ip);
ret = ioctl(fd, SIOCSIFADDR, &temp);
close(fd);
)
;
;
}
检查网线是否连接
int check_nic(void)
{
struct ifreq ifr;
);
strcpy(ifr.ifr_name, "eth0");
)
{
close(skfd);
;
}
if(ifr.ifr_flags & IFF_RUNNING)
{
printf("link up\n");
close(skfd);
; // 网卡已插上网线
}
else
{
printf("link down\n");
close(skfd);
;
}
}
读取本机MAC地址
int read_mac(unsigned char *buf,char *name)
{
int sock;
int ret;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, );
)
{
perror("socket error");
;
}
memset(&ifr, , sizeof(ifr));
strcpy(ifr.ifr_name, name);
ifr.ifr_name[] = '\0';
ret = ioctl(sock,SIOCGIFHWADDR,&ifr);
)
{
memcpy(buf, ifr.ifr_hwaddr.sa_data, );
printf("HW address is: %02x:%02x:%02x:%02x:%02x:%02x \n",
(unsigned ], (unsigned ], (unsigned ],
(unsigned ], (unsigned ], (unsigned ]);
}
else
{
perror("ioctl error!\n");
}
close(sock);
return ret;
}
判断文件是否存在
int is_file_exist(const char *file_path)
{
if(file_path == NULL)
;
)
;
;
}
linux下常用的基本设置与操作C语言实现的更多相关文章
- Linux下常用的操作
Linux下常用的操作 文件定位 locate filename 有些版本的linux会出现 -bash: locate: command not found错误,不要慌,安装一下mlocate包就好 ...
- linux下MySQL安装登录及操作
linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...
- MongoDB在Linux下常用优化设置
MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...
- linux下常用的截图、录屏工具
录屏: 在linux下常用的录屏工具有5种,可以baidu或者google下喔,我选用的是recordMydesktop,使用非常方便,用时注意先把每秒桢数调高,否则效果必然很差. 在ubuntu下可 ...
- Linux下常用的ftp操作命令
Linux下常用的ftp操作命令 =========== 完美的分割线 ============= 1.登陆ftp服务器 ftp [IP] [PORT] # 登陆ftp服务器,本机登陆可以不写IP 实 ...
- linux下常用命令备忘
转自:Linux 命令集锦 linux下查看监听端口对应的进程 # lsof -i:9000 # lsof -Pnl +M -i4 如果退格键变成了:"^h". 终端连接unix删 ...
- zip,rar及linux下常用的压缩格式
日常操作中我们经常使用到文件压缩操作,其使用一些特定的算法来减小文件的大小,可以提高传输数据时的速率和减少数据在一些存储机制上占有的空间大小,实现空间利用最大化. 比如:如果你想通过邮箱发送一个文件夹 ...
- linux下常用关机命令
linux下常用的关机命令有:shutdown.halt.poweroff.init:重启命令有:reboot.下面本文就主要介绍一些常用的关机命令以及各种关机命令之间的区别和具体用法. 首先来看一下 ...
- Linux 下常用解压命令(转载)
Linux下常用文件解压(包括rpm.deb包) Linux下怎么解后缀名是gzip的文件?1.以.a为扩展名的文件:#tar xv file.a2.以.z为扩展名的文件:#uncompress fi ...
随机推荐
- 防止iframe被别的网站引用
try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.hr ...
- juniper ssg 常用命令
netscreen juniper ssg操作命令 2013年4月10日 命令行下取得配置信息 get config 命令行下取得相应时间设置 get clock set vrout ...
- oracle修改密码和设置密码有效期
一.修改密码1)修改密码 sql>alter user user01 identified by password; 2)修改密码并unlock sql>alter user user01 ...
- thinkphp 多表事务处理
try{ $this->user = D('User'); $this->user->startTrans(); //开始事务 $res = $this->user->S ...
- Struts2的Action中访问servletAPI方式
struts2的数据存放中心为ActionContext,其是每次请求来时都会创建一个ActionContext,访问结束销毁,其绑定在ThreadLocal上,由于每次访问web容器都会为每次请求创 ...
- js,java,ajax实现跨域访问及其原理
http://blog.csdn.net/saytime/article/details/51540876 这篇文章对跨域访问做了较为细致得分析,我这里做下简单总结 1.实现跨域访问原理: 浏览器由于 ...
- java arrays类学习
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...
- 深入剖析Redis主从复制
[http://sofar.blog.51cto.com/353572/1413024/] [Redis 主从复制的内部协议和机制] 一.主从概述 Redis 支持 Master-Slave( ...
- zoj2314 无源汇上下界可行流
题意:看是否有无源汇上下界可行流,如果有输出流量 题解:对于每一条边u->v,上界high,下界low,来说,我们可以建立每条边流量为high-low,那么这样得到的流量可能会不守恒(流入量!= ...
- 1 Python 环境搭建
Python可应用于多平台包括 Linux 和 Mac OS X. 你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Python的安装版本. U ...