Recently, the company needs to add a feature, that is, automatic version update, using apt-Get. For more information about how to configure apt-Get software sources, see share your resources. The following code is used as part of the upgrade.
FILE *fp;char buffer[256];char source_ok[] = "Reading package lists...";if(0 > system("mv -f /etc/apt/sources.list /etc/apt/sources.list.upgrade"))printf("cldUp backup old source list error!\n");fp=fopen("/etc/apt/sources.list","w+");if(0 == fp)goto update_done;fprintf(fp,"deb http://%s/apt %s %s", source_addr, source_dir, source_ver);printf("\ndeb http://%s/apt %s %s\n", source_addr, source_dir, source_ver);fclose(fp);fp=popen("apt-get update 2>&1","r");while(!feof(fp)){if(0 == fgets(buffer,sizeof(buffer),fp))break;printf("%s",buffer); fflush(stdout);if(strstr(buffer,"Failed")){trace_log(buffer); printf("scan failed and exit\n"); goto update_done;}if(strstr(buffer,source_ok)){trace_log(buffer); printf("scan sourceok and exit\n");break;}}pclose(fp);fp=popen("apt-get -y --force-yes install ssh 2>&1","r");while(!feof(fp)){if(0 == fgets(buffer,sizeof(buffer),fp))break;printf("%s",buffer); if(strstr(buffer,"1 upgraded")){ //save the upgrade info b_upgrade_success = true;trace_log(buffer); } }pclose(fp); if(b_upgrade_success) { b_upgrade_success = 0; }update_done:if(0 > system("mv -f /etc/apt/sources.list.upgrade /etc/apt/sources.list"))printf("restore source list error!\n");
There is no problem with the preliminary test.