Implementation of C code for the operation of directories and files on standby on the host computer in Linux

Source: Internet
Author: User

Requirements Description
Write a program to complete the operation of the directory and files on the host machine. For example, the host is a, the standby machine is B, the program required to be written to run on a machine, the program to create a file directory on the B machine and copy files operation.

Demand Analysis
We do not consider the implementation of the requirements of the program, if we are to manually implement the requirements described in the operation, what will we do? You might think of the way to use Telnet. That is, use Telnet on the a machine to remote to B machine, then perform the operation of creating directory and copying files on machine B.

We can also use Telnet in the program to complete the above requirements.

Program Writing
There are two ways to execute Telnet commands, one is to use system-provided systems functions directly, and the other is to use pipelines. Here are the programs written in either of these ways:

1. Using the system function

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: SYSTEM.C* File ID: None* Content Summary: Testing the usage of the system* Other instructions: remote operation with system* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20151225***********************************************************************/#include <stdio.h>#include <string.h>Redefine data type typedef unsigned char uint8;typedef signed int int32;//function declaration void dealremotefile (void);*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151225 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {dealremotefile ();return 0;}/*********************************************************************** Function Description: Processing remote files* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151225 V1.0 Zhou Zhaoxiong created***********************************************************************/void dealremotefile (void) {UINT8 szcmdbuf[1024] = {0};UINT8 szresultbuf[1024] = {0};snprintf (szcmdbuf, sizeof (SZCMDBUF)-1, "(Sleep 0.5; Echo Zhou; Sleep 1; Echo Zhou; Sleep 5; echo \ "Cd/home/zhou/zhouzx/test;mkdir-p testfile;cp TestFile.txt./testfile;\"; Sleep 2) | Telnet 10.10.10.10 ");printf ("Dealremotefile:cmd is%s\n", szcmdbuf);system (SZCMDBUF);}

2. How to use the pipe

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: pipeopenandclose.c* File ID: None* Content Summary: Test the use of Popen and Pclose* Other instructions: Use Popen and pclose to process remote files* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20151225***********************************************************************/#include <stdio.h>#include <string.h>Redefine data type typedef unsigned char uint8;typedef signed int int32;//function declaration void dealremotefile (void);*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151225 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {dealremotefile ();return 0;}/*********************************************************************** Function Description: Processing remote files* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151225 V1.0 Zhou Zhaoxiong created***********************************************************************/void dealremotefile (void) {UINT8 szcmdbuf[1024] = {0};UINT8 szresultbuf[1024] = {0};FILE *fppipe = NULL;snprintf (szcmdbuf, sizeof (SZCMDBUF)-1, "(Sleep 0.5; Echo Zhou; Sleep 1; Echo Zhou; Sleep 5; echo \ "Cd/home/zhou/zhouzx/test;mkdir-p testfile;cp TestFile.txt./testfile;\"; Sleep 2) | Telnet 10.10.10.10 ");printf ("Dealremotefile:cmd is%s\n", szcmdbuf);fppipe = Popen (Szcmdbuf, "R");if (NULL = = fppipe)    {printf ("Dealremotefile:fppipe is null!\n");return;    }While (fgets (szresultbuf, sizeof (SZRESULTBUF), fppipe)! = NULL)    {if (' \ n ' = = Szresultbuf[strlen (szresultbuf)-1])//Remove line break        {  Szresultbuf[strlen (SZRESULTBUF)-1] = ' + ';         }printf ("Dealremotefile:resultbuf is%s\n", szresultbuf);    }Pclose (fppipe); Close Pipefppipe = NULL;}

program compilation and Operation
Upload system.c and pipeopenandclose.c files to a machine using "Gcc-g-o System system.c" and "Gcc-g-o pipeopenandclose pipeopenandclose.c" respectively command to compile the two files and then run the "System" and "pipeopenandclose" commands separately, you can observe on the B machine that the relevant operation has been executed successfully.

Program Description
1. The commands in Szcmdbuf in the two above programs are at the heart of the program, and its pattern is "shell command | Telnet x.x.x.x ", where" x.x.x.x "is the B machine's Ip,shell command" Sleep "Let the program sleep, the first echo after the variable is the user name, the second echo after the variable is the password, The third Echo performs the specific operation of creating directories and copying files.

2.popen and Pclose mates to implement the relevant shell commands. The Popen function creates a sub-process by creating a pipeline called fork, which must be closed by the Pclose function.

Implementation of C code for the operation of directories and files on standby on the host computer in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.