- 论坛徽章:
- 0
|
- /*
- 将一文件中的某个ip替换掉 by wzt
- */
- #include <stdio.h>
- #include <string.h>
- #define MAXLINE 1024
- #define MAXIP 20
- #define DEFAULT_FILE "d:\\c\\6.txt"
- char old_ip[MAXIP];
- char new_ip[MAXIP];
- /* 将字符串"MYSERVER_IP = "保存到临时变量temp中 */
- void extract_data(char *line,char *temp)
- {
- int i = 0,j = 0,k = 0;
- for ( ; line[i] != '=' ; i++ );
- for ( ; line[i] != ' '; i++ );
- for ( ; j <= i ; j++ )
- temp[j] = line[j];
- temp[j] = '\0';
- printf("[+] %s\n",temp);
- }
- /* 检查输入ip合法性 */
- void check_ip(char *str)
- {
- int i=0,count = 0;;
- for ( ; i < strlen(str) ; i++ ) {
- if ( !(str[i] >= '0' && str[i] <= '9' )) {
- if ( str[i] == '.' ) {
- count++;
- continue;
- }
- printf("[-] Wrong IP.\n");
- exit(0);
- }
- }
- if (count == 3 )
- printf("[+] IP check OK.\n");
- else
- printf("[-] Wrong IP.\n");
- }
- /* 替换ip */
- void load_file(FILE *s_fp)
- {
- char line[MAXLINE],new_line[MAXLINE],temp[MAXIP];
- int pos,len1,len2;
- int i = 0;
- while ( fgets(line,MAXLINE,s_fp) != NULL ) {
- if ( strstr(line,"MYSERVER_IP") != NULL ) {
- extract_data(line,temp);
- sprintf(new_line,"%s%s",temp,new_ip);
- len1 = strlen(line);
- len2 = strlen(new_line);
- printf("[+] %s\n",line);
- len1 = len1 - len2;
- for ( ; i < len1 ; i++ )
- new_line[len2++] = ' ';
- new_line[len2] = '\0';
- printf("[+] %s\n",new_line);
- pos = strlen(line);
- fseek(s_fp,-pos,SEEK_CUR);
- fputs(new_line,s_fp);
- break;
- }
- }
- }
- int main(int argc,char **argv)
- {
- FILE *s_fp;
- printf("[+] input your ip : ");
- scanf("%s",new_ip);
- printf("[+] new_ip : %s\n",new_ip);
- check_ip(new_ip);
- if ( (s_fp = fopen(DEFAULT_FILE,"r+")) == NULL ) {
- printf("[-] open %s failed.\n",argv[1]);
- exit(0);
- }
- load_file(s_fp);
- fclose(s_fp);
- printf("[+] done.\n");
- return 0;
- }
复制代码
[ 本帖最后由 W.Z.T 于 2007-5-6 18:15 编辑 ] |
|