📄 test_pvc.c
字号:
#include <stdio.h>#include <stdlib.h>#include<math.h>#include <string.h>#include <errno.h>#include <time.h>#include <unistd.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/types.h> #include<signal.h>#include<sys/wait.h>#include <linux/serial.h>
#include <asm/ioctls.h>
#include <sys/io.h>#include <fcntl.h> #include <pthread.h>
#include <termios.h> #include <assert.h>#include <iconv.h>#include <gtk/gtk.h>#define TEST_FIRM_FILE "./test_log/firm.cfg"GtkWidget *window_login,*fixed1,*button_fuwei,*button_jihuo,*button_exit,*entry_username,*entry_pwd;int i_fuwei=0,i_vlan_start=0,i_vlan_end=0;GtkWidget *button_state[32];GtkWidget *label_state[32];static GtkWidget *label_state2;int jihuo_pvc(int argc, char *argv[]);int fuwei_pvc(int argc, char *argv[]);int gtk_login_cancel(GtkWidget * widget, gpointer data){ system("killall test_pvc"); gtk_widget_destroy(window_login); return 1;}GtkWidget *create_button(void){ //显示动画gif文件 GtkWidget *icon; GtkWidget *button; GdkPixbufAnimation* anim_pixbuf = gdk_pixbuf_animation_new_from_file("./Image/exit.gif", NULL); icon = gtk_image_new_from_animation(anim_pixbuf); gtk_widget_show(icon); button = gtk_button_new(); gtk_container_add(GTK_CONTAINER(button), icon); return button;}int main(int argc, char *argv[]){ GdkColor color; int i,j,icount=0,row=40,line=100,button_id=0,label_id=0,ilabel_count=0; gtk_init(&argc, &argv); window_login = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_icon_from_file(GTK_WINDOW(window_login),"./Image/2.ICO",NULL); gtk_window_set_title(GTK_WINDOW(window_login), " Mass Tools V1.0 by CZ"); gtk_window_set_default_size(GTK_WINDOW(window_login), 800,570); fixed1 = gtk_fixed_new (); gtk_container_add (GTK_CONTAINER (window_login), fixed1); gtk_widget_show (fixed1); //1个label label_state2 = gtk_label_new((gchar *)("")); gtk_label_set_use_markup (GTK_LABEL(label_state2), TRUE); gtk_fixed_put(GTK_FIXED (fixed1), label_state2,0,0); gtk_widget_show(label_state2); ////////////////////////////////////////////////// char label[256]; line=70; for(i=0;i<8;i++) { row=50; label_id=icount; for(j=0;j<4;j++) { label_id+=i+j; sprintf(label,"<span size=\"20000\"color=\"DarkCyan\">VLAN %d</span>",ilabel_count+1); label_state[label_id]= gtk_label_new(label); gtk_label_set_use_markup (GTK_LABEL(label_state[label_id]), TRUE); gtk_fixed_put(GTK_FIXED (fixed1), label_state[label_id], row, line); gtk_widget_show(label_state[label_id]); row+=200; icount++; ilabel_count++; } line+=60; icount--; } icount=0; char buf[128]; sprintf(buf,"待激活"); line=100; for(i=0;i<8;i++) { row=40; button_id=icount; for(j=0;j<4;j++) { button_id+=i+j; button_state[button_id] = gtk_button_new_with_mnemonic (buf); gtk_widget_set_size_request (button_state[button_id], 120, 32); PangoFontDescription *font_desc = pango_font_description_from_string("Sans"); pango_font_description_set_size (font_desc, 12 * PANGO_SCALE); gdk_color_parse ("blue", &color); gtk_widget_modify_fg(GTK_BIN(button_state[button_id])->child, GTK_STATE_NORMAL,&color); gtk_widget_modify_font (GTK_BIN(button_state[button_id])->child, font_desc); gtk_fixed_put (GTK_FIXED (fixed1), button_state[button_id],row, line); gtk_widget_show (button_state[button_id]); row+=200; icount++; } line+=60; icount--; }//两个button。 button_jihuo = gtk_button_new_with_label ("激 活"); gtk_widget_set_size_request (button_jihuo, 110,40); PangoFontDescription *font_desc = pango_font_description_from_string("Sans"); pango_font_description_set_size (font_desc, 15 * PANGO_SCALE); gdk_color_parse ("mediumblue", &color); gtk_widget_modify_fg(GTK_BIN(button_jihuo)->child, GTK_STATE_NORMAL,&color); gtk_widget_modify_font (GTK_BIN(button_jihuo)->child, font_desc); GdkColor color2; gdk_color_parse ("lavender", &color2); gtk_widget_modify_bg (button_jihuo, GTK_STATE_NORMAL, &color2); gtk_fixed_put (GTK_FIXED (fixed1), button_jihuo,180, 10); gtk_widget_show (button_jihuo); button_fuwei = gtk_button_new_with_label ("复 位"); gtk_widget_set_size_request (button_fuwei, 110,40); font_desc = pango_font_description_from_string("Sans"); pango_font_description_set_size (font_desc, 15 * PANGO_SCALE); gdk_color_parse ("mediumblue", &color); gtk_widget_modify_fg(GTK_BIN(button_fuwei)->child, GTK_STATE_NORMAL,&color); gtk_widget_modify_font (GTK_BIN(button_fuwei)->child, font_desc); gtk_widget_modify_bg (button_fuwei, GTK_STATE_NORMAL, &color2); gtk_fixed_put (GTK_FIXED (fixed1), button_fuwei,380, 10); gtk_widget_show (button_fuwei); button_exit = create_button(); gtk_widget_set_size_request (button_exit, 120,45); gtk_fixed_put (GTK_FIXED (fixed1), button_exit,580, 10); gtk_widget_show (button_exit); g_signal_connect(G_OBJECT(button_jihuo), "clicked", G_CALLBACK(jihuo_pvc),NULL); g_signal_connect(G_OBJECT(button_fuwei), "clicked", G_CALLBACK(fuwei_pvc),NULL); g_signal_connect(G_OBJECT(button_exit), "clicked", G_CALLBACK(gtk_login_cancel),NULL); gdk_color_parse("gray",&color); gtk_widget_modify_bg(window_login,GTK_STATE_NORMAL,&color); gtk_widget_show_all(window_login); gtk_main(); return 0 ;}int get_vlan_start_end_from_file(char *filename,char vlan_start[128],char vlan_end[128]){ FILE *fp; char buf[1024]; int len = 0,i; char *pName,*pValue,*temp; fp = fopen(filename,"r"); if (fp == NULL) { printf("Open file %s error\n",filename); return 0; } while(1) { fgets(buf,1024,fp); if (feof(fp)) break; pName=pValue=buf; if ((pValue = strstr(buf,"=")) != NULL) { *pValue = '\0'; pValue++; if(( temp = strstr(pValue,"\n")) != NULL) { *temp = '\0'; } if(strcmp(pName,"vlan_start") == 0) { strcpy(vlan_start,pValue); } else if(strcmp(pName,"vlan_end") == 0) { strcpy(vlan_end,pValue); } } } fclose(fp); return 1;}void Set_Button_State_Info(int index_button ,char *buttonlabel,char * fontcolor,int fontsize){ gtk_button_set_label((GtkButton *)(button_state[index_button]),buttonlabel); PangoFontDescription *font_desc = pango_font_description_from_string("Sans"); pango_font_description_set_size (font_desc,fontsize * PANGO_SCALE); gtk_widget_modify_font (GTK_BIN(button_state[index_button])->child, font_desc); GdkColor color; gdk_color_parse (fontcolor, &color); gtk_widget_modify_fg(GTK_BIN(button_state[index_button])->child, GTK_STATE_NORMAL,&color); //gtk_widget_modify_bg (button[index_button], GTK_STATE_NORMAL, &color);}void Set_Label_State_Info(char *color,char * labelInfo){ char label[256]; label[0]='\0'; sprintf(label,"<span size=\"50000\"color=\"%s\"background=\"black\">%s</span>",color,labelInfo); label_state2= gtk_label_new(label); gtk_label_set_use_markup (GTK_LABEL(label_state2), TRUE); gtk_fixed_put(GTK_FIXED (fixed1), label_state2, 0, 0); gtk_widget_show(label_state2);}int jihuo_pvc(int argc, char *argv[]){ char ftp_interface[128]; char vlan_start[128],vlan_end[128],eth_name[128]; int i; vlan_start[0]=vlan_end[0]='\0'; get_vlan_start_end_from_file(TEST_FIRM_FILE,vlan_start,vlan_end); //printf("%s %s\n",vlan_start,vlan_end); i_vlan_start=atoi(vlan_start); i_vlan_end=atoi(vlan_end); char cmd[1024]; sprintf(eth_name,"%s","eth0"); //增加VLAN口 for(i=i_vlan_start;i<=i_vlan_end;i++) { sprintf(cmd,"vconfig add %s %d 2>/dev/null",eth_name,i); printf("%s\n",cmd); system(cmd); sprintf(cmd,"ifconfig %s.%d %s%d",eth_name,i,"192.168.1.",200); printf("%s\n",cmd); system(cmd); } Set_Label_State_Info("green","开始"); while (gtk_events_pending ()) gtk_main_iteration ();/////////////////////////////////////////////////////////////////// char *values_url=NULL,*values_url2=NULL,*target=NULL,buf[65535]; int loop,pvcret,pvcret2,pvcbasic,pvcbasic2; buf[0]='\0'; values_url="wan_VC=PVC2&wanVCFlag=1&wan_VCStatus=1&Alwan_VPI=8&Alwan_VCI=35&Alwan_QoS=UBR&wan_PCR=0&wanConTypeFlag=0&wanTypeRadio=Two&wan_PPPServicename=&wan_PPPUsername=orange&wan_PPPPassword=orange&wan_PPPEncap=PPPoE+LLC&PPPoEBIStatus=0&wan_ConnectSelect=Connect_Keep_Alive&wan_TCPMSS=0&wan_PPPGetIP=0&wan_NAT=Enable&WAN_DefaultRoute=1&wan_TCPMTU=0&wan_RIPVersion=RIP2-B&wan_RIPDirection=Both&wan_IGMP=IGMP+v2&wan_EnabledSpoof=0&BridgeFlag=0&wanIGMPQitFlag=0"; values_url2="wan_VC=PVC2&wanVCFlag=0&wan_VCStatus=1&Alwan_VPI=0&Alwan_VCI=35&Alwan_QoS=UBR&wan_PCR=0&wanConTypeFlag=0&wanTypeRadio=Three&wan_BridgeSelect=1483+Bridged+IP+LLC&BridgeFlag=0&wanIGMPQitFlag=0"; target="http://192.168.1.1/Forms/home_wan_1"; while(1) { if(i_fuwei ==i_vlan_end-i_vlan_start+1) break; for(i=i_vlan_start;i<=i_vlan_end;i++) { char buttonlabel[256]; buttonlabel[0]='\0'; strcpy(buttonlabel, gtk_button_get_label((GtkButton *)(button_state[i-i_vlan_start]))); if(strcmp(buttonlabel,"激活成功") != 0) { sprintf(ftp_interface,"eth0.%d",i_vlan_start); for(loop=0;loop<=20;loop++) { pvcret=http_pvc_post(target,values_url,ftp_interface); sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); pvcbasic=http_get_webpage("http://192.168.1.1/basic/home_wan.htm",ftp_interface, buf); sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); pvcret2=http_pvc_post(target,values_url2,ftp_interface); sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); pvcbasic2=http_get_webpage("http://192.168.1.1/basic/home_wan.htm",ftp_interface, buf); if(pvcret==0&&pvcbasic==0&&pvcret2==0&&pvcbasic2==0)break; sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); } if(loop==20) { printf("修改PVC错误1\n"); Set_Button_State_Info(i-i_vlan_start ,"激活失败","red",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); return 0; } //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); //判断是否修改成功 char *pvc2=NULL; char *pvc_end=NULL; int pvclist=1; char buf_pvcn[65535]; for(loop=0;loop<=30;loop++) { pvclist=http_get_webpage("http://192.168.1.1/basic/home_pvclist.htm",ftp_interface, buf_pvcn); sleep(1); pvc2=strstr(buf_pvcn,"PVC2"); //pvc2为空 if(pvc2 == NULL) continue; if(pvclist==0 && pvc2 != NULL) break; sleep(1); } while (gtk_events_pending ()) gtk_main_iteration (); pvc_end=pvc2; pvc_end+=150; *pvc_end='\0'; printf("%s\n",pvc2); if(loop==30||(strstr(pvc2,"Yes")==NULL)) { printf("修改PVC错误2\n"); Set_Button_State_Info(i-i_vlan_start ,"激活失败","red",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); return 0; } else { Set_Button_State_Info(i-i_vlan_start ,"激活成功","green",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); i_fuwei++; } } } } Set_Label_State_Info("green","结束"); while (gtk_events_pending ()) gtk_main_iteration (); printf("####激活结束####\n"); return 1;}int fuwei_pvc(int argc, char *argv[]){ char ftp_interface[128]; char *target=NULL,*value=NULL; int loop,pvcret,i,j; Set_Label_State_Info("green","开始"); while (gtk_events_pending ()) gtk_main_iteration (); sleep(1); for(j=0;j<=i_vlan_end-i_vlan_start;j++) { Set_Button_State_Info(j ,"复位中...","blue",12); while (gtk_events_pending ()) gtk_main_iteration (); } target="http://192.168.1.1/Forms/tools_system_1"; value="restoreFlag=1&Restart=RESTART"; i_fuwei=0; while(1) { if(i_fuwei ==i_vlan_end-i_vlan_start+1) break; for(i=i_vlan_start;i<=i_vlan_end;i++) { char buttonlabel[256]; buttonlabel[0]='\0'; strcpy(buttonlabel, gtk_button_get_label((GtkButton *)(button_state[i-i_vlan_start]))); while (gtk_events_pending ()) gtk_main_iteration (); if(strcmp(buttonlabel,"复位成功") != 0) { sprintf(ftp_interface,"eth0.%d",i_vlan_start); for(loop=0;loop<=20;loop++) { pvcret=http_pvc_post(target,value,ftp_interface); if(pvcret==0) break; sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); } if(loop==20) { printf("复位PVC错误1\n"); Set_Button_State_Info(i-i_vlan_start ,"复位失败","red",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); return 0; } for(j=0;j<8;j++) { sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); printf("######复位中...\n"); } /////////////等待启动 //判断是否修改成功 char *pvc2=NULL,*pvc_end=NULL,buf_pvcn[65535]; int pvclist=1; for(loop=0;loop<=30;loop++) { pvclist=http_get_webpage("http://192.168.1.1/basic/home_pvclist.htm",ftp_interface, buf_pvcn); sleep(1); pvc2=strstr(buf_pvcn,"PVC2"); //pvc2为空 if(pvc2 == NULL) continue; if(pvclist==0 && pvc2 != NULL) break; sleep(1); while (gtk_events_pending ()) gtk_main_iteration (); } pvc_end=pvc2; pvc_end+=150; *pvc_end='\0'; printf("%s\n",pvc2); if(loop==30||(strstr(pvc2,"No")==NULL)) { printf("复位PVC错误2\n"); Set_Button_State_Info(i-i_vlan_start ,"复位失败","red",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); return 0; } else { Set_Button_State_Info(i-i_vlan_start ,"复位成功","green",12); //刷新页面 while (gtk_events_pending ()) gtk_main_iteration (); i_fuwei++; } while (gtk_events_pending ()) gtk_main_iteration (); } } } Set_Label_State_Info("green","结束"); while (gtk_events_pending ()) gtk_main_iteration (); printf("####复位结束####\n"); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -