📄 console.c
字号:
/***********************************************************************///Serial console program for NetHawk 2+ Firewall.//Modified By Fenghua Lang 2005/07/28#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <sys/ioctl.h>#include <termio.h>#define FALSE 0#define TRUE 1#define WORKMODE_FILE "/nethawk/param/pub/workmode.info"#define ETH0_FILE "/nethawk/param/net/ifcfg-eth0"#define ETH2_FILE "/nethawk/param/net/ifcfg-eth2"#define COMM "/dev/ttyS0" //COM1volatile int STOP=FALSE;int get_workmode(){ FILE *fp; char modebuf[255]; if((fp=fopen(WORKMODE_FILE,"r"))!=NULL) { while(fgets(modebuf,255,fp)) { if(strstr(modebuf,"transparent")!=NULL) { return(0); } else if(strstr(modebuf,"gateway")!=NULL) { return(1); } } return(2); } else { printf("Open workmode.info file error!\n"); return(2); }}void set_if(int workmode,char *ip_addr,char *ip_mask){// char ip[30]; char buf[256]; FILE *fp_temp,*fp; if((fp_temp=fopen("/home/work/tmp_eth_file","w"))==NULL) { printf("open tmp_eth_file error!\n"); return; }// strcpy(ip,ip_addr); if(workmode==0) { if((fp=fopen(ETH2_FILE,"r"))==NULL) { printf("open tmp_eth_file error!\n"); exit; } } else { if((fp=fopen(ETH0_FILE,"r"))==NULL) { printf("open tmp_eth_file error!\n"); exit; } } while(fgets(buf,256,fp)) { if(strstr(buf,"IPADDR")!=NULL) { sprintf(buf,"IPADDR=%s\n",ip_addr); } else if(strstr(buf,"NETMASK=")) { sprintf(buf,"NETMASK=%s\n",ip_mask); } fputs(buf,fp_temp); } fclose(fp_temp); strcpy(buf,"cp -f"); strcat(buf," "); strcat(buf,"/home/work/tmp_eth_file"); strcat(buf," "); if(workmode==0) { strcat(buf,ETH2_FILE); } else { strcat(buf,ETH0_FILE); } system(buf); sync(); strcpy(buf,"/home/httpd/htdocs/bin/restart_network"); system(buf); return;}main(){ int fd,i,s,res,wes; int work_mode; char buf[255],optbuf[10],namebuf[50],pwdbuf[50]; struct termio tfd; FILE *ethfp; if ((fd = open(COMM,O_RDWR)) < 0 ) { printf("Open COM Port Error!\n"); exit(0); } s = ioctl(fd,TCGETA, &tfd) ; if (s == -1 ) { printf("ioctl error!\n") ; close(fd); exit(0); } //tfd.c_lflag &= ~(ICANON|ISIG|ECHO) ; tfd.c_lflag = ECHO|ICANON; tfd.c_iflag &= ~( INLCR|ICRNL|IUCLC|ISTRIP|IXON|BRKINT ) ; tfd.c_iflag |= IGNPAR|ICRNL ; tfd.c_oflag &= ~OPOST ; tfd.c_cc[ VMIN ] = 1 ; tfd.c_cc[ VTIME ] = 1 ; tfd.c_cflag &= ~(CBAUD|CSTOPB|PARENB|CSIZE) ; tfd.c_cflag |= B9600; tfd.c_cflag |= CS8 ; s = ioctl(fd,TCSETAW,&tfd) ; if (s == -1 ) { printf("ioctl error!\n"); close(fd); exit(0); } ioctl(fd,TCFLSH,2) ; // into main loop while (STOP==FALSE) { buf[0]='\n'; buf[1]='\r'; for(res=0;res<20;res++) wes=write(fd,buf,2); strcpy(buf,"************************************************************\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* *\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* 安全防护设备设置 *\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* *\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* 1)配置管理接口地址 *\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* 2)配置管理员密码 *\n\r"); wes=write(fd,buf,strlen(buf)); strcpy(buf,"* *\n\r"); strcpy(buf,"************************************************************\n\r"); wes=write(fd,buf,strlen(buf)); // strcpy(buf,"Enter the menu item number that you want[1/2/3]:"); strcpy(buf,"Enter the menu item number that you want[1/2]:"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); buf[res-1]='\0'; strcpy(optbuf,buf); if(strcmp(optbuf,"1")==0) { if(work_mode==0) { strcpy(buf,"\n\rThe DMZ Network Interface Setup is:\n\r"); wes=write(fd,buf,strlen(buf)); if(ethfp=fopen("/etc/sysconfig/network-scripts/ifcfg-eth2","r")) { while(fgets(buf,255,ethfp)) { if(strstr(buf,"IPADDR")!=NULL) { strcat(buf,"\r"); wes=write(fd,buf,strlen(buf)); // printf("%s",buf); } if(strstr(buf,"NETMASK")!=NULL) { strcat(buf,"\r"); wes=write(fd,buf,strlen(buf));// printf("%s",buf); } } fclose(ethfp); } } else { strcpy(buf,"\n\rThe Internal Network Interface Setup is:\n\r"); wes=write(fd,buf,strlen(buf)); if(ethfp=fopen("/etc/sysconfig/network-scripts/ifcfg-eth0","r")) { while(fgets(buf,255,ethfp)) { if(strstr(buf,"IPADDR")!=NULL) { strcat(buf,"\r"); wes=write(fd,buf,strlen(buf)); // printf("%s",buf); } if(strstr(buf,"NETMASK")!=NULL) { strcat(buf,"\r"); wes=write(fd,buf,strlen(buf));// printf("%s",buf); } } fclose(ethfp); } } strcpy(buf,"Do you really want to change this?[n]"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); buf[res-1]=0; if(strcmp(buf,"y")==0) { if(work_mode==0) { strcpy(buf,"\n\rInput DMZ Interface IP Address:"); } else { strcpy(buf,"\n\rInput Internal Interface IP Address:"); } wes=write(fd,buf,strlen(buf)); res=read(fd,namebuf,50); namebuf[res-1]='\0'; if(work_mode==0) { strcpy(buf,"\n\rInput DMZ Interface Netmask:"); } else { strcpy(buf,"\n\rInput Internal Interface Netmask:"); } wes=write(fd,buf,strlen(buf)); res=read(fd,pwdbuf,50); pwdbuf[res-1]='\0'; set_if(work_mode,namebuf,pwdbuf); } } if(strcmp(optbuf,"2")==0) { ret: strcpy(buf,"\n\rEnter admin password:"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); buf[res-1]='\0'; strcpy(pwdbuf,buf); strcpy(buf,"\n\rRetype password:"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); buf[res-1]='\0'; if(strcmp(buf,pwdbuf)!=0) { strcpy(buf,"\n\rPassword do not match,Press Enter key continue!"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); } else { strcpy(buf,"/home/httpd/htdocs/bin/modifypasswd"); strcat(buf," "); strcat(buf,"modify"); strcat(buf," "); strcat(buf,"admin"); strcat(buf," "); strcat(buf,pwdbuf);// printf("%s\n",buf); system(buf); sync(); } } /* if(strcmp(optbuf,"3")==0) { strcpy(buf,"\n\rAll of your setting will be lost!!!"); wes=write(fd,buf,strlen(buf)); buf[0]='\n'; buf[1]='\r'; wes=write(fd,buf,2); strcpy(buf,"\n\rDo you really want to do that?[n]"); wes=write(fd,buf,strlen(buf)); res=read(fd,buf,255); buf[res-1]='\0'; if(strcmp(buf,"y")==0) { system("cp -Rf /nethawk-bak/* /nethawk/"); } } */ buf[0]='\n'; buf[1]='\r'; for(res=0;res<20;res++) wes=write(fd,buf,2); } close(fd); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -