1、文件下载指令应用

支持断点续传

curl -C - -O "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"
curl -C - -O "https://curl.haxx.se/download/archeology/md5.txt"
直接覆盖
curl "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz" -o curl-7.58.0.tar.gz --progress
curl "https://curl.haxx.se/download/archeology/md5.txt" -o md5.txt --progress

系统自带的wget指令下载文件也类似

wget "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"
wget -O curl-7.58.0.tar.gz "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz"

2、产品固件更新应用demo

upgrade.sh

#!/bin/sh
url="https://curl.haxx.se/download/archeology/"
while true
do#1)download filemd5_url=${url}"md5.txt"#curl md5_url -o md5.txt --progresswget -O md5.txt "$md5_url"version=$(sed -n 1p md5.txt)filename=$(sed -n 2p md5.txt)md5=$(sed -n 3p md5.txt)version_bak=$(sed -n 1p md5_bak.txt)echo $version    echo $filenameecho $md5    echo $version_bak#2)md5sum firmware if [[ "$version" != "$version_bak" ]];then               cp md5.txt md5_bak.txt       download_url=${url}${filename}    download_url=${download_url%$'\r'}#curl "$download_url" -o $filename --progresswget -O $filename "$download_url"       md5_calc=$(md5sum $filename|cut -d ' ' -f1)#3)replace app file---> rebootif [ "$md5"=="$md5_calc" ] ; then     ./kill_app.sh#replace upgrade file./run_app.shrm $filenametime=$(date "+%Y-%m-%d %H:%M:%S")echo "${time} upgrade $md5_calc $filename " >> log.txtelse echo "upgrade file md5sum err !"fielse echo "local version is the latest version !"fisleep 10
done

3、 md5.txt

md5.txt文件内置3行关键信息

7.58.0
curl-7.58.0.tar.gz
7e9e9d5405c61148d53035426f162b0a

4、curl安装

https://curl.haxx.se/

./configure --prefix=/usr/local/curl
make
sudo make install

export PATH=$PATH:/usr/local/curl/bin
export LD_LIBRARY_PATH=/usr/local/curl/lib:$LD_LIBRARY_PATH

5、curl移植

./configure --prefix=/home/dong/curl/curl-7.58.0/_install --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
make
sudo make install

6、 wget编译,安装

https://ftp.gnu.org/gnu/wget/

# tar zxvf wget-1.9.1.tar.gz

# cd wget-1.9.1

# ./configure

# make

# make install

7、progress

wget -c https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz -o load.log

progress.sh

#wget -c https://ftp.gnu.org/gnu/wget/wget-1.19.tar.gz -o load.logwhile true
dosed -n '/..........\>/p' load.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4,$5,$6,$7}'
sed -n '/...\>/p' load.log | sed '$d' | sed -n '$p' | awk '{print $1,$2,$3,$4}'sleep 1done

8、progress.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//类似wget的进度条的实现,实际就是转移符\r的使用,\r的作用是返回至行首而不换行
int main(int argc, char *argv[])
{unsigned len = 60;char *bar = (char *)malloc(sizeof(char) * (len + 1));for (int i = 0; i < len + 1; ++i){bar[i] = '#';}for (int i = 0; i < len; ++i){printf("progress:[%s]%d%%\r", bar+len-i, i+1);fflush(stdout);//一定要fflush,否则不会会因为缓冲无法定时输出。usleep(100000);//sleep(1);
    }printf("\n");return 0;
}

9、checkurl.sh

#!/bin/bash
usage(){echo "Usage:$0 url"exit 1
}checkurl(){local num=`curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null $1 |egrep "(200|301|302)"|wc -l`if [ $num -eq 1 ] thenecho "ok"elseecho "$1"fi
}main(){if [ $# -ne 1 ]thenusageficheckurl $1}main $*

sh checkurl.sh www.vbill.cn

10、c语言文件下载,带进度条

https://blog.csdn.net/qq_31629063/article/details/80846673

https://www.cnblogs.com/wainiwann/p/3148884.html

url2file.c ,已经忘了在哪儿淘的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>#define HOST_NAME_LEN   256
#define URI_MAX_LEN     2048
#define RECV_BUF        8192
#define RCV_SND_TIMEOUT (10*1000)   //收发数据超时时间(ms)typedef struct {int sock;                       //与服务器通信的socketFILE *in;                       //sock描述符转为文件指针,方便读写char host_name[HOST_NAME_LEN];  //主机名int port;                       //主机端口号char uri[URI_MAX_LEN];          //资源路径char buffer[RECV_BUF];          //读写缓冲int status_code;                //http状态码int chunked_flag;               //chunked传输的标志位int len;                        //Content-length里的长度char location[URI_MAX_LEN];     //重定向地址char *save_path;                //保存内容的路径指针FILE *save_file;                //保存内容的文件指针int recv_data_len;              //收到数据的总长度time_t start_recv_time;         //开始接受数据的时间time_t end_recv_time;           //结束接受数据的时间
} http_t;/* 打印宏 */
#define MSG_DEBUG   0x01
#define MSG_INFO    0x02
#define MSG_ERROR   0x04static int print_level = /*MSG_DEBUG |*/ MSG_INFO | MSG_ERROR;#define lprintf(level, format, argv...) do{     \if(level & print_level)     \printf("[%s][%s(%d)]:"format, #level, __FUNCTION__, __LINE__, ##argv);  \
}while(0)#define MIN(x, y) ((x) > (y) ? (y) : (x))#define HTTP_OK         200
#define HTTP_REDIRECT   302
#define HTTP_NOT_FOUND  404/* 不区分大小写的strstr */
char *strncasestr(char *str, char *sub)
{if(!str || !sub)return NULL;int len = strlen(sub);if (len == 0){return NULL;}while (*str){if (strncasecmp(str, sub, len) == 0){return str;}++str;}return NULL;
}/* 解析URL, 成功返回0,失败返回-1 */
/* http://127.0.0.1:8080/testfile */
int parser_URL(char *url, http_t *info)
{char *tmp = url, *start = NULL, *end = NULL;int len = 0;/* 跳过http:// */if(strncasestr(tmp, "http://")){   tmp += strlen("http://");}start = tmp;if(!(tmp = strchr(start, '/'))){lprintf(MSG_ERROR, "url invaild\n");return -1;      }end = tmp;/*解析端口号和主机*/info->port = 80;   //先附默认值80
len = MIN(end - start, HOST_NAME_LEN - 1);strncpy(info->host_name, start, len);info->host_name[len] = '\0';if((tmp = strchr(start, ':')) && tmp < end){info->port = atoi(tmp + 1);if(info->port <= 0 || info->port >= 65535){lprintf(MSG_ERROR, "url port invaild\n");return -1;}/* 覆盖之前的赋值 */len = MIN(tmp - start, HOST_NAME_LEN - 1);strncpy(info->host_name, start, len);info->host_name[len] = '\0';}/* 复制uri */start = end;strncpy(info->uri, start, URI_MAX_LEN - 1);lprintf(MSG_INFO, "parse url ok\nhost:%s, port:%d, uri:%s\n", info->host_name, info->port, info->uri);return 0;
}/* dns解析,返回解析到的第一个地址,失败返回-1,成功则返回相应地址 */
unsigned long dns(char* host_name)
{struct hostent* host;struct in_addr addr;char **pp;host = gethostbyname(host_name);if (host == NULL){lprintf(MSG_ERROR, "gethostbyname %s failed\n", host_name);return -1;}pp = host->h_addr_list;if (*pp!=NULL){addr.s_addr = *((unsigned int *)*pp);lprintf(MSG_INFO, "%s address is %s\n", host_name, inet_ntoa(addr));pp++;return addr.s_addr;}return -1;
}/* 设置发送接收超时 */
int set_socket_option(int sock)
{struct timeval timeout;timeout.tv_sec = RCV_SND_TIMEOUT/1000;timeout.tv_usec = RCV_SND_TIMEOUT%1000*1000;lprintf(MSG_DEBUG, "%ds %dus\n", (int)timeout.tv_sec, (int)timeout.tv_usec);//设置socket为非阻塞// fcntl(sock ,F_SETFL, O_NONBLOCK); //以非阻塞的方式,connect需要重新处理// 设置发送超时if(-1 == setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval))){lprintf(MSG_ERROR, "setsockopt error: %m\n");return -1;}// 设置接送超时if(-1 == setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval))){lprintf(MSG_ERROR, "setsockopt error: %m\n");return -1;}return 0;
}/* 连接到服务器 */
int connect_server(http_t *info)
{int sockfd;struct sockaddr_in server;unsigned long addr = 0;unsigned short port = info->port;sockfd = socket(AF_INET, SOCK_STREAM, 0);if (-1 == sockfd){lprintf(MSG_ERROR, "socket create failed\n");goto failed;}if(-1 == set_socket_option(sockfd)){goto failed;}if ((addr = dns(info->host_name)) == -1){lprintf(MSG_ERROR, "Get Dns Failed\n");goto failed;}memset(&server, 0, sizeof(server));server.sin_family = AF_INET; server.sin_port = htons(port); server.sin_addr.s_addr = addr;if (-1 == connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr))){lprintf(MSG_ERROR, "connect failed: %m\n");goto failed;}info->sock = sockfd;return 0;failed:if(sockfd != -1)close(sockfd);return -1;
}/* 发送http请求 */
int send_request(http_t *info)
{int len;memset(info->buffer, 0x0, RECV_BUF);snprintf(info->buffer, RECV_BUF - 1, "GET %s HTTP/1.1\r\n""Accept: */*\r\n""User-Agent: Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n""Host: %s\r\n""Connection: Close\r\n\r\n", info->uri, info->host_name);lprintf(MSG_DEBUG, "request:\n%s\n", info->buffer);return send(info->sock, info->buffer, strlen(info->buffer), 0);
}/* 解析http头 */
int parse_http_header(http_t *info)
{char *p = NULL;// 解析第一行fgets(info->buffer, RECV_BUF, info->in);p = strchr(info->buffer, ' ');//简单检查http头第一行是否合法if(!p || !strcasestr(info->buffer, "HTTP")){lprintf(MSG_ERROR, "bad http head\n");return -1;}info->status_code = atoi(p + 1);   lprintf(MSG_DEBUG, "http status code: %d\n", info->status_code);// 循环读取解析http头while(fgets(info->buffer, RECV_BUF, info->in)){// 判断头部是否读完if(!strcmp(info->buffer, "\r\n")){return 0;   /* 头解析正常 */}lprintf(MSG_DEBUG, "%s", info->buffer);// 解析长度 Content-length: 554if(p = strncasestr(info->buffer, "Content-length")){p = strchr(p, ':');p += 2;     // 跳过冒号和后面的空格info->len = atoi(p);lprintf(MSG_INFO, "Content-length: %d\n", info->len);}else if(p = strncasestr(info->buffer, "Transfer-Encoding")){if(strncasestr(info->buffer, "chunked")){info->chunked_flag = 1;}else{/* 不支持其他编码的传送方式 */lprintf(MSG_ERROR, "Not support %s", info->buffer);return -1;}lprintf(MSG_INFO, "%s", info->buffer);}else if(p = strncasestr(info->buffer, "Location")){p = strchr(p, ':');p += 2;     // 跳过冒号和后面的空格strncpy(info->location, p, URI_MAX_LEN - 1);lprintf(MSG_INFO, "Location: %s\n", info->location);}}lprintf(MSG_ERROR, "bad http head\n");return -1;  /* 头解析出错 */
}/* 保存服务器响应的内容 */
int save_data(http_t *info, const char *buf, int len)
{int total_len = len;int write_len = 0;// 文件没有打开则先打开if(!info->save_file){info->save_file = fopen(info->save_path, "w");if(!info->save_file){lprintf(MSG_ERROR, "fopen %s error: %m\n", info->save_path);return -1;}}while(total_len){write_len = fwrite(buf, sizeof(char), len, info->save_file);if(write_len < len && errno != EINTR){lprintf(MSG_ERROR, "fwrite error: %m\n");return -1;}total_len -= write_len;}
}/* 读数据 */
int read_data(http_t *info, int len)
{int total_len = len;int read_len = 0;int rtn_len = 0;while(total_len){read_len = MIN(total_len, RECV_BUF);// lprintf(MSG_DEBUG, "need read len: %d\n", read_len);rtn_len = fread(info->buffer, sizeof(char), read_len, info->in);if(rtn_len < read_len){if(ferror(info->in)){if(errno == EINTR) /* 信号中断了读操作 */{;   /* 不做处理继续往下走 */}else if(errno == EAGAIN || errno == EWOULDBLOCK) /* 超时 */{lprintf(MSG_ERROR, "socket recvice timeout: %dms\n", RCV_SND_TIMEOUT);total_len -= rtn_len;lprintf(MSG_DEBUG, "read len: %d\n", rtn_len);break;}else    /* 其他错误 */{lprintf(MSG_ERROR, "fread error: %m\n");break;}}else    /* 读到文件尾 */{lprintf(MSG_ERROR, "socket closed by peer\n");total_len -= rtn_len;lprintf(MSG_DEBUG, "read len: %d\n", rtn_len);break;}}// lprintf(MSG_DEBUG, " %s\n", info->buffer);total_len -= rtn_len;lprintf(MSG_DEBUG, "read len: %d\n", rtn_len);if(-1 == save_data(info, info->buffer, rtn_len)){return -1;}info->recv_data_len += rtn_len;}if(total_len != 0){lprintf(MSG_ERROR, "we need to read %d bytes, but read %d bytes now\n", len, len - total_len);return -1;}
}/* 接收服务器发回的chunked数据 */
int recv_chunked_response(http_t *info)
{long part_len;//有chunked,content length就没有了do{// 获取这一个部分的长度fgets(info->buffer, RECV_BUF, info->in);part_len = strtol(info->buffer, NULL, 16);lprintf(MSG_DEBUG, "part len: %ld\n", part_len);if(-1 == read_data(info, part_len))return -1;//读走后面的\r\n两个字符if(2 != fread(info->buffer, sizeof(char), 2, info->in)){lprintf(MSG_ERROR, "fread \\r\\n error : %m\n");return -1;}}while(part_len);return 0;
}/* 计算平均下载速度,单位byte/s */
float calc_download_speed(http_t *info)
{int diff_time = 0; float speed = 0.0;diff_time = info->end_recv_time - info->start_recv_time;/* 最小间隔1s,避免计算浮点数结果为inf */if(0 == diff_time)diff_time = 1;speed = (float)info->recv_data_len / diff_time;return  speed;
}/* 接收服务器的响应数据 */
int recv_response(http_t *info)
{int len = 0, total_len = info->len;if(info->chunked_flag)return recv_chunked_response(info);if(-1 == read_data(info, total_len))return -1;return 0;
}/* 清理操作 */
void clean_up(http_t *info)
{if(info->in)fclose(info->in);if(-1 != info->sock)close(info->sock);if(info->save_file)fclose(info->save_file);if(info)free(info);
}/* 下载主函数 */
int http_download(char *url, char *save_path)
{http_t *info = NULL;char tmp[URI_MAX_LEN] = {0};if(!url || !save_path)return -1;//初始化结构体info = malloc(sizeof(http_t));if(!info){lprintf(MSG_ERROR, "malloc failed\n");return -1;}memset(info, 0x0, sizeof(http_t));info->sock = -1;info->save_path = save_path;// 解析urlif(-1 == parser_URL(url, info))goto failed;// 连接到serverif(-1 == connect_server(info))goto failed;// 发送http请求报文if(-1 == send_request(info))goto failed;// 接收响应的头信息info->in = fdopen(info->sock, "r");if(!info->in){lprintf(MSG_ERROR, "fdopen error\n");goto failed;}// 解析头部if(-1 == parse_http_header(info))goto failed;switch(info->status_code){case HTTP_OK:// 接收数据lprintf(MSG_DEBUG, "recv data now\n");info->start_recv_time = time(0);if(-1 == recv_response(info))goto failed;info->end_recv_time = time(0);lprintf(MSG_INFO, "recv %d bytes\n", info->recv_data_len);lprintf(MSG_INFO, "Average download speed: %.2fKB/s\n", calc_download_speed(info)/1000);break;case HTTP_REDIRECT:// 重启本函数lprintf(MSG_INFO, "redirect: %s\n", info->location);strncpy(tmp, info->location, URI_MAX_LEN - 1);clean_up(info);return http_download(tmp, save_path);case HTTP_NOT_FOUND:// 退出lprintf(MSG_ERROR, "Page not found\n");goto failed;break;default:lprintf(MSG_INFO, "Not supported http code %d\n", info->status_code);goto failed;}clean_up(info);return 0;
failed:clean_up(info);return -1;
}/****************************************************************************
测试用例:
(1)chunked接收测试
./a.out "http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx" test.aspx
(2)重定向测试
./a.out "192.168.10.1/main.html" test.txt
(3)错误输入测试
./a.out "32131233" test.txt
(4)根目录输入测试
./a.out "www.baidu.com/" test.txt
(5)端口号访问测试
./a.out "192.168.0.200:8000/FS_AC6V1.0BR_V15.03.4.12_multi_TD01.bin" test.txt
****************************************************************************/int main(int argc, char *argv[])
{if(argc < 3)return -1;http_download(argv[1], argv[2]);return 0;
}

转载于:https://www.cnblogs.com/dong1/p/9510350.html

产品固件(系统)升级——curl/wget相关推荐

  1. 苹果手机php怎么更新,Apple Watch固件系统怎么更新升级?

    苹果的 Apple Watch 手表,与我们平日里所使用的 iPhone 手机一样,它也有自己的一套独立的系统,名为"Watch OS".当然有了自己的独立系统以后,也就肯定会有后 ...

  2. 车载Linux固件升级,一种车载系统固件在线升级方法与流程

    本发明涉及汽车系统升级技术领域,尤其涉及一种车载系统固件在线升级方法. 背景技术: 随着汽车智能化.网联化的发展趋势,车联网的技术进步带来汽车软件系统的复杂度不断增加,升级迭代的速度越来越快,软件的不 ...

  3. 魅蓝s6 android系统版本,魅蓝S6官方公开稳定版固件系统rom升级更新包:Flyme 7.3.0.1A...

    魅蓝S6的公开稳定版包来分享一下了,这个也是官方原版的系统包了,这个原版的系统包是卡刷格式,这个系统包的版本是7.3.0.1A版本的,也是可以进行系统的升级更新用的,这个系统包大小是1.6G,更新内容 ...

  4. 你家路由器还在闲置吗?快拿过来刷不同固件系统,做wifi放大器不香么?让家里处处有网。

    一.家里有一个闲置3.4年的斐讯FIR303B路由器,号称300Mb,已经吃灰许久,由于卧室wifi信号没有那么好,就决定弄个桥接,放大wifi 二.没有想到的是 家里的斐讯FIR303B由于太过久远 ...

  5. FPGA远程固件在线升级

    FPGA远程固件在线升级 1.摘要 对最近做的FPGA远程更新/在线升级做一个总结.一般在代码开发阶段,我们使用JTAG烧写代码,但当产品投入到实际使用过程中,难以再用JTAG进行固件更新.所以需要开 ...

  6. CentOS7 升级 curl 支持 HTTP2 与 TLS 1.3

    目录 文章目录 目录 编译安装 YUM 升级 curl 常用选项 编译安装 安装编译环境: yum -y groupinstall "Development Tools" yum ...

  7. curl wget之间的区别 以及推荐SimpRead插件

    curl & wget之间的区别 以及推荐SimpRead插件 参考 技术|cURL 与 wget:你应该选用哪一个? 前言 在归纳这两者之间的区别之前,还是想推荐一下google chrom ...

  8. android aptx固件,新增aptX蓝牙发射协议,M5固件首次升级!

    M5新版固件版本号为FW1.1.1,在原来FW1.0.1基础上主要更改以下内容: 1.原来短按电源键锁屏功能现修改为双击电源键锁屏:短按电源键改为播放/暂停控制: 2.系统设置项增加"电源键 ...

  9. 镁光ssd管理工具 linux,镁光C400固态硬盘08TH固件及升级软件

    镁光C400固态硬盘08TH固件及升级软件内含两个版本,一个iso版,一个是exe一体版,均为镁光C400固态硬盘的固件,exe版带升级程序,iso版需要做启动盘,具体升级教程可参看网上的教程. - ...

最新文章

  1. js进阶 13-5 jquery队列动画如何实现
  2. php自定义控件,小程序自定义组件的实现方法(代码)
  3. Java中的冒泡排序(减少比较次数)
  4. 获取CPU序列号和MAC地址
  5. 小程序 textarea ios兼容解决
  6. 医学图像-颅内出血(转载+整理)
  7. 【IT笔试面试题整理】二叉树中和为某一值的路径--所有可能路径
  8. .NET Core下使用gRpc公开服务(SSL/TLS)
  9. git本地创建新分支并推送到远程仓库
  10. springMVC接收数据和响应返回
  11. 比对数组指定列php,指定列的数组
  12. 启动关闭HadoopSpark历史服务
  13. 安卓插件化开发!移动开发程序员怎么优雅迈过30K+这道坎?全网最新
  14. 常见Flash小游戏开发核心思想笔记——《拼图》
  15. 拒绝焦虑状态:TA爱我吗?
  16. 关于Gephi的安装的一些问题
  17. 常见程序设计及编程开发
  18. 分布式系统的知识点-架构设计
  19. 希腊字母,阿拉伯字母的读音表
  20. Word2Vec增量训练实现

热门文章

  1. JavaScript文件存储信息对象cookie编码生存期
  2. SQL Server 函数的使用(字符串函数)
  3. CSS百分比实现高度占位自适应
  4. 试题10 最大子阵(枚举)
  5. 报文交换(串行)和分组交换(并行)
  6. 九大背包问题专题--分组背包问题
  7. bzoj 3394: [Usaco2009 Jan]Best Spot 最佳牧场(floyd)
  8. python学习之文件处理
  9. opencv 创建调色板
  10. python 实现对象去重