使用popen()执行shell命令并获取内容
2015-06-26 17:31
836 查看
1.popen通过管道获取shell的输出结果,跟重定向(ls > /test.txt)类似。
1.popen通过管道获取shell的输出结果,跟重定向(ls > /test.txt)类似。
root@Easy-link:~# cat /proc/uptime 67.23 19.08 char*get_uptime() { float hours; FILE *fp; static char buf[16]; fp = popen("cat /proc/uptime |cut -d \" \" -f1","r"); fgets(buf,sizeof(buf),fp); pclose(fp); hours = (atof(buf))/3600.00; sprintf(buf,"Uptime:%7.2f H",hours); return buf; }
char*get_date() { FILE *fp; static char buf[16]; //fp = popen("date '+%y/%m/%d %I:%M%p'","r"); fp = popen("date '+%m/%d %I:%M %a'","r"); fgets(buf,sizeof(buf),fp); pclose(fp); return buf; } char*get_ip() { FILE *fp; char tmp[16]; static char buf[16]; fp = popen("ifconfig ppp0|grep \"inet addr\"|cut -d \":\" -f2|cut -d \" \" -f1","r"); fgets(tmp,sizeof(buf),fp); pclose(fp); sprintf(buf,"%16s",tmp); return buf; } char*get_rx() { float f,s,rx; FILE *fp; static char buf[16]=" "; fp = popen("cat /proc/net/dev|grep \"ppp0\"|cut -d \" \" -f4","r"); fgets(buf,sizeof(buf),fp); pclose(fp); f=atof(buf); sleep(1); fp = popen("cat /proc/net/dev|grep \"ppp0\"|cut -d \" \" -f4","r"); fgets(buf,sizeof(buf),fp); pclose(fp); s=atof(buf); rx = ((s-f)/1024.00); sprintf(buf," RX:%7.2f KB/s",rx); return buf; } char*get_tx() { float f,s,tx; FILE *fp; static char buf[16]=" "; fp = popen("cat /proc/net/dev|grep \"ppp0\"|cut -d \" \" -f42","r"); fgets(buf,sizeof(buf),fp); pclose(fp); f=atof(buf); sleep(1); fp = popen("cat /proc/net/dev|grep \"ppp0\"|cut -d \" \" -f42","r"); fgets(buf,sizeof(buf),fp); pclose(fp); s=atof(buf); tx = ((s-f)/1024.00); sprintf(buf," TX:%7.2f KB/s",tx); return buf; }
相关文章推荐
- Xshell常用配置
- /etc/profile、~/.bash_profile等几个文件的执行过程
- 【每天一个linux命令】11. Linux配置环境变量/etc/profile .bashrc .bash_profile等
- profile与bashrc
- profile与bashrc 分类: Ubuntu学习笔记 2015-06-26 14:42 146人阅读 评论(0) 收藏
- shell expect
- 认识 BASH 这个 Shell
- 一个Linux中用于监控的简易shell脚本
- sudo:无法解析主机 解决方案
- bash: ./make_ext4fs: No such file or directory 错误解决方法
- shell学习二十八天----case语句
- shell学习二十七天----退出状态和if语句
- cgywin bat 设置 改用mitty
- shell 中如何判断前一个命令是否执行成功
- shell 中如何判断前一个命令是否执行成功
- 查看当前发行版可以使用的shell
- 查看当前发行版可以使用的shell
- [转]Bash Shell中命令行选项/参数处理
- shell中IFS用法
- shell中的条件语句