Synchronization method and system for command line text
By using pre-formatted command-line text and XML parsing templates, the problem of confusing associations between sub-level and group-level information in the device network management system was solved, achieving efficient and accurate data synchronization.
Patent Information
- Application Number
- CN202511599680.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-04
- Publication Date
- 2026-02-27
AI Technical Summary
Existing technologies lack unified text grouping rules and structured parsing logic in device network management systems, resulting in chaotic associations between multi-level sub-information and group-level information, low data parsing efficiency, and poor accuracy. In particular, it is prone to errors when processing a large number of nested configuration data.
The system uses a pre-formatted command-line text, distinguishing levels and comment markers by the number of spaces. Combined with XML parsing templates and intermediate table mapping relationships, it achieves grouping, parsing, and transformation of command-line text, ensuring automated association between the main table and sub-tables and data integrity.
It improves the accuracy and efficiency of data grouping, avoids hierarchical association errors, ensures the integrity and consistency of data synchronization, simplifies the interface adaptation process, and improves synchronization efficiency and accuracy.
Smart Images

Figure CN121579588A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of network device management and data synchronization technology, specifically to a method and system for synchronizing command-line text. Background Technology
[0002] In device network management systems, to enable configuration queries when devices are offline, device resource configuration information needs to be synchronized to the network management database. Current technologies often use a request-by-request approach based on interfaces to synchronize data. When processing configuration data containing multi-level sub-information, the lack of unified text grouping rules and structured parsing logic frequently leads to confusion between sub-level and group-level information, resulting in low data parsing efficiency and poor accuracy. Especially when configuration data contains a large number of nested sub-tables, complex interface adaptation is required to establish the relationship between the main table and sub-tables, increasing the complexity of the synchronization process and easily causing incomplete data synchronization due to incorrect hierarchical associations. Summary of the Invention
[0003] To address the shortcomings of existing technologies, this invention provides a method and system for synchronizing command-line text, which solves the problems mentioned in the background section.
[0004] To achieve the above objectives, the present invention provides the following technical solution: a method for synchronizing command-line text, comprising the following steps: Obtain the command-line text in a preset format stored in the device, wherein the command-line text contains device resource configuration information; The command line text is grouped to obtain grouped data; Based on the parsing template corresponding to the device type, the grouped data is parsed to generate intermediate table data; Based on the device type, a preset mapping relationship between intermediate tables and database tables is matched, and the intermediate table data is converted into database table data. Load the data from the database table into the target database.
[0005] Preferably, the preset format command line text satisfies: The first line of a group-level command line has no leading space and uses a specific symbol as the group end marker or comment marker. After a child-level message is wrapped, it contains a preset number of leading spaces, and the hierarchy of child-level messages is distinguished by the number of leading spaces. Default configuration information is not displayed when it matches the baseline configuration.
[0006] Preferably, the step of grouping the command line text to obtain grouped data includes: Read the command line text line by line, and identify the line without a leading space as the start of a new group; The number of leading spaces determines the group and level of the child information. If the number of leading spaces in a certain line is N more than that in the previous line, the child information in that line is determined to be the next level of the corresponding group in the previous line, and a hierarchical relationship between the data within the group is established. Here, N is a preset positive integer used to distinguish the levels. Filter rows containing annotation markers to form structured grouped data.
[0007] Preferably, the parsing template is an XML format file, containing: Regular expressions used to match group-level command lines; The primary key identifier and field attributes of the intermediate table, wherein the field attributes include the regular expression for matching field values, value retrieval rules, and default values; If it includes sub-tables, it also includes the sub-table identifier, the sub-table primary key, and the association rules between the sub-table and the main table, where: The sub-table identifier is a name or number that uniquely identifies the sub-table; The primary key of a subtable is used to uniquely identify records in the subtable; The association rules between the child table and the main table are to establish the association through the same field value or to achieve the association through a specific mapping relationship.
[0008] Preferably, the step of parsing the grouped data to generate intermediate table data based on the parsing template corresponding to the device type includes: The XML parsing template corresponding to the device type is called, and the group-level command line of the group data is matched by regular expression. The first line of the group-level command line of the group data is compared with the regular expression used to match the group-level command line in the parsing template. If the match is successful, subsequent processing is performed. Parse information at all levels within the group based on field attributes, extract field values or assign default values, and generate main table data; The child information is parsed according to the child table association rules. When the child table and the main table are associated through the same field value, the data with the same field value as the main table is searched in the child information, the child table data is generated and associated with the main table data to form complete intermediate table data.
[0009] Preferably, the preset mapping relationship between the intermediate table and the database table is defined through an XML format file, including: Database table identifiers and loading order; The correspondence between database fields and intermediate table fields, including field names, data types, and optional numeric conversion rules.
[0010] Preferably, the step of matching the preset mapping relationship between the intermediate table and the database table according to the device type and converting the intermediate table data into database table data includes: Call the XML file corresponding to the device type to determine the mapping rules between intermediate table fields and database fields. These mapping rules include the correspondence between intermediate table field names and database field names, as well as data type conversion rules. The intermediate table data is transformed according to the mapping rules. When there are numerical transformation rules, the numerical transformation is performed according to the rules. The converted data is deduplicated, and records with the same primary key are merged. This is done by sorting the converted data according to the primary key field of the database table, merging adjacent records with the same primary key value, retaining one record, and processing the other field values according to the preset merging method.
[0011] Preferably, the preset format command line text stored in the acquisition device includes: Command-line text is downloaded from the device to the local server via a file transfer protocol that supports resuming interrupted downloads.
[0012] Preferably, loading the database table data into the target database includes: Generate a data insertion script based on the database table structure. The script includes field names and corresponding data values. The insertion script is executed to write the database table data into the target database.
[0013] A command-line text synchronization system, comprising: The text acquisition module is used to acquire command-line text in a preset format stored on the device; The grouping module is used to group the command line text to obtain grouped data. The parsing module is used to match the corresponding parsing template based on the device type and parse the grouped data to generate intermediate table data; The conversion module is used to match a preset mapping relationship between an intermediate table and a database table according to the device type, and convert the intermediate table data into database table data. The loading module is used to load the database table data into the target database.
[0014] This invention provides a method and system for synchronizing command-line text. It has the following beneficial effects: 1. This invention clarifies the distinction rules between group-level command lines and child-level information by pre-setting the command line text format. Combined with the hierarchical judgment logic based on the number of spaces in grouping processing, the relationship between child-level information and group-level information is clearly distinguishable. This solves the problem of confusing association between child-level and group-level information in the prior art and improves the accuracy and efficiency of data grouping.
[0015] 2. By leveraging the sub-table identifiers, primary keys, and association rules defined in the XML parsing template, the automatic association between the main table and sub-table data is achieved, avoiding the complex adaptation process under the traditional interface adaptation method and reducing the problem of incomplete data synchronization caused by hierarchical association errors.
[0016] 3. By using mapping rules between intermediate tables and database tables, as well as data deduplication logic, the integrity and consistency of configuration data synchronized to the database are further ensured, improving the reliability of device resource configuration information synchronization. Attached Figure Description
[0017] Figure 1 This is a schematic diagram of the method flow of the present invention; Figure 2 This is a schematic diagram of the system framework of the present invention. Detailed Implementation
[0018] The technical solution of the present invention will now be clearly and completely described with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] Example 1: Please see the appendix Figure 1 -Appendix Figure 2 This invention provides a method for synchronizing command-line text, comprising the following steps: Obtain the command-line text in a preset format stored in the device, wherein the command-line text contains device resource configuration information; The command line text is grouped to obtain grouped data; Based on the parsing template corresponding to the device type, the grouped data is parsed to generate intermediate table data; Based on the device type, a preset mapping relationship between intermediate tables and database tables is matched, and the intermediate table data is converted into database table data. Load the data from the database table into the target database.
[0020] Specifically, through a comprehensive design encompassing standardized command-line text formatting, group processing, template parsing, mapping conversion, and data loading, efficient synchronization of device configuration information to the network management database is achieved. First, device configuration information is stored as command-line text in a unified format. Then, data hierarchy is organized through group processing. Combined with parsing templates and mapping relationships matching device types, the conversion from text to database table data is completed and loaded. This process avoids the tediousness of traditional interface requests, significantly improving synchronization efficiency through text processing and template-based parsing, while ensuring data accuracy and integrity. This solves the problems of low efficiency and error-proneness caused by complex interface adaptation in traditional synchronization methods.
[0021] The preset format command line text satisfies: The first line of a group-level command line has no leading space and uses a specific symbol as the group end marker or comment marker. After a child-level message is wrapped, it contains a preset number of leading spaces, and the hierarchy of child-level messages is distinguished by the number of leading spaces. Default configuration information is not displayed when it matches the baseline configuration.
[0022] Specifically, the pre-formatted command-line text provides a unified "language standard" for subsequent grouping and parsing by clearly defining the formatting rules for group-level and child-level information (such as the number of spaces), end and comment markers, and default information display rules. This design makes the text data structure clear, the hierarchical relationship within groups intuitively identifiable, and allows for quick identification of data ownership during grouping, reducing parsing errors caused by formatting issues. At the same time, the rule of not displaying default information simplifies the text size, reduces transmission and storage costs, and improves the efficiency and accuracy of data processing compared to traditional text without a unified format.
[0023] The grouping process of the command line text to obtain grouped data includes: Read the command line text line by line, and identify the line without a leading space as the start of a new group; The number of leading spaces determines the group and level of the child information. If the number of leading spaces in a certain line is N more than that in the previous line, the child information in that line is determined to be the next level of the corresponding group in the previous line, and a hierarchical relationship between the data within the group is established. Here, N is a preset positive integer used to distinguish the levels. Filter rows containing annotation markers to form structured grouped data.
[0024] Specifically, the grouping process reads line by line and identifies the start of a new group based on the first line's space feature. It then uses the difference in the number of spaces (N) to determine the sub-level hierarchy, while simultaneously filtering comment lines, thus achieving structured grouping of the data. The principle is to transform the implicit hierarchical relationships in the text into explicit grouping logic through quantifiable space counts. This establishes clear hierarchical relationships between potentially confusing sub-level and group-level information, simplifying the identification of data correlations during subsequent parsing and avoiding interference from comment information on valid data. Compared to traditional methods without fixed grouping rules, this significantly improves the accuracy and efficiency of grouping, laying a solid foundation for subsequent data parsing.
[0025] The parsing template is an XML format file, containing: Regular expressions used to match group-level command lines; The primary key identifier and field attributes of the intermediate table, wherein the field attributes include the regular expression for matching field values, value retrieval rules, and default values; If it includes sub-tables, it also includes the sub-table identifier, the sub-table primary key, and the association rules between the sub-table and the main table, where: The sub-table identifier is a name or number that uniquely identifies the sub-table; The primary key of a subtable is used to uniquely identify records in the subtable; The association rules between the child table and the main table are to establish the association through the same field value or to achieve the association through a specific mapping relationship.
[0026] Specifically, leveraging the structured nature of XML, various rules required for parsing (such as group-level command-line matching, field value extraction, and sub-table association) are solidified into templates. When processing grouped data of different device types, parsing can be automatically completed simply by matching the corresponding template. This avoids the repetitive work of developing separate parsing logic for each device, and the explicit sub-table association rules also ensure the accuracy of data association between the main table and sub-tables. Compared to traditional hard-coded parsing methods, this improves development efficiency and parsing accuracy.
[0027] The step of parsing the grouped data to generate intermediate table data based on the parsing template corresponding to the device type includes: The XML parsing template corresponding to the device type is called, and the group-level command line of the group data is matched by regular expression. The first line of the group-level command line of the group data is compared with the regular expression used to match the group-level command line in the parsing template. If the match is successful, subsequent processing is performed. Parse information at all levels within the group based on field attributes, extract field values or assign default values, and generate main table data; The child information is parsed according to the child table association rules. When the child table and the main table are associated through the same field value, the data with the same field value as the main table is searched in the child information, the child table data is generated and associated with the main table data to form complete intermediate table data.
[0028] Specifically, by leveraging the mapping between device types and XML parsing templates, group-level command lines are located through regular expression matching. Then, intermediate table data is generated based on field attributes and sub-table association rules. Precise regular expression matching ensures correct identification of group-level command lines, while field value extraction and default value assignment mechanisms guarantee the integrity of the main table data. The association between sub-table data and main table data is automated through explicit rules, avoiding errors from manual association. Compared to the traditional method of retrieving and associating data one by one, this significantly improves the efficiency of data parsing and the integrity of intermediate table data.
[0029] The pre-defined mapping relationship between the intermediate table and the database table is defined through an XML file, which includes: Database table identifiers and loading order; The correspondence between database fields and intermediate table fields, including field names, data types, and optional numeric conversion rules.
[0030] Specifically, XML files are used to record the mapping logic between intermediate tables and database tables, making the conversion process systematic. A clear loading order ensures the rationality of data loading sequence, field correspondences and data type definitions guarantee the accuracy of data conversion, and optional numerical conversion rules meet the data format adaptation needs of different scenarios. Compared to traditional manual writing of conversion logic, this improves the efficiency and accuracy of conversion.
[0031] The step of matching the preset mapping relationship between the intermediate table and the database table according to the device type and converting the intermediate table data into database table data includes: Call the XML file corresponding to the device type to determine the mapping rules between intermediate table fields and database fields. These mapping rules include the correspondence between intermediate table field names and database field names, as well as data type conversion rules. The intermediate table data is transformed according to the mapping rules. When there are numerical transformation rules, the numerical transformation is performed according to the rules. The converted data is deduplicated, and records with the same primary key are merged. This is done by sorting the converted data according to the primary key field of the database table, merging adjacent records with the same primary key value, retaining one record, and processing the other field values according to the preset merging method (such as taking the latest value, summing, etc.).
[0032] Specifically, based on the device type matching mapping XML file, the data in the intermediate table is converted to database table data through the mapping rules within the file, and deduplication is performed. The mapping rules ensure the accuracy of field correspondence and data type conversion, the numerical conversion rules meet the requirements of special data formats, and the deduplication process ensures the uniqueness of database table data through primary key sorting and merging mechanisms. Compared with the problems of field mismatch and data redundancy that are prone to occur in traditional conversions, this step improves the accuracy of data conversion and the cleanliness of database table data.
[0033] The preset format command line text stored in the device includes: Command-line text is downloaded from the device to the local server via a file transfer protocol that supports resuming interrupted downloads.
[0034] Specifically, by utilizing the protocol's breakpoint resumption function, if a file transfer is interrupted, it can resume from the point of interruption, avoiding the need to retransmit the entire file. Compared to traditional transfer methods that do not support breakpoint resumption, this reduces transfer failures and duplicate transfers caused by network fluctuations, improves the efficiency and success rate of command-line text retrieval, and ensures the smooth progress of subsequent synchronization processes.
[0035] The step of loading the database table data into the target database includes: Generate a data insertion script based on the database table structure. The script includes field names and corresponding data values. The insertion script is executed to write the database table data into the target database.
[0036] Specifically, an insertion script containing field names and corresponding data values is generated based on the database table structure, and then the data is written to the database by executing the script. The generation of the insertion script ensures that the data writing format matches the database table structure, avoiding write failures due to format errors. Automated script execution improves the efficiency of data loading, ensuring both accuracy and efficiency compared to manual entry or batch import methods with non-standard formats.
[0037] A command-line text synchronization system, comprising: The text acquisition module is used to acquire command-line text in a preset format stored on the device; The grouping module is used to group the command line text to obtain grouped data. The parsing module is used to match the corresponding parsing template based on the device type and parse the grouped data to generate intermediate table data; The conversion module is used to match a preset mapping relationship between an intermediate table and a database table according to the device type, and convert the intermediate table data into database table data. The loading module is used to load the database table data into the target database.
[0038] Specifically, through the collaborative work of various modules, the entire process of command-line text synchronization is automated. The text acquisition module ensures the successful acquisition of command-line text, the grouping and processing module organizes the data hierarchy, the parsing module generates intermediate table data using templates, the conversion module completes data format conversion, and the loading module writes the data into the database. The functions of each module are clear and closely connected. Through modular design, the complex synchronization process is broken down into independent yet collaborative steps, with each module focusing on a specific task, thus improving the maintainability and scalability of the system.
[0039] Example 2 (Illustrated explanation of each step in command-line text synchronization) Retrieve command line text in a preset format: Suppose we obtain a command-line text from a network device containing the device's interface configuration information. Group-level command lines, such as "Interface GigabitEthernet0 / 1", do not have a leading space on the first line and end with an exclamation mark (!"). Sub-level information, such as "Description 'Connecting to server room'", has two leading spaces after a newline, classifying it as a level 1 sub-level. "Full-duplex mode" has four leading spaces after a newline, two more than the previous line, classifying it as a level 2 sub-level. Furthermore, default information consistent with the baseline configuration, such as "Automatic speed", is not displayed in the text.
[0040] Grouping processes generate grouped data: When reading the command-line text line by line, the lines "Interface GigabitEthernet0 / 1" and "Interface GigabitEthernet0 / 2" without leading spaces are taken as the starting points of two new groups. The number of leading spaces determines the affiliation and hierarchy of sub-information: "Description 'Connecting to server room'" and "Speed 1000" with two leading spaces belong to the first-level sub-level of the "Interface GigabitEthernet0 / 1" group; "Full-duplex mode" with four leading spaces (two more spaces than the previous line) belongs to the second-level sub-level of the "Interface GigabitEthernet0 / 1" group. Simultaneously, lines containing comment markers are filtered out, ultimately forming structured grouped data that clearly presents the group-level commands and corresponding sub-level information for each group.
[0041] Parse the grouped data to generate intermediate table data: For this network device type, a corresponding XML parsing template is matched. This template contains regular expressions for matching group-level commands, as well as the primary key identifier and field attributes of the intermediate table. During parsing, the group-level commands in the grouped data are first matched using regular expressions. If "interface GigabitEthernet0 / 1" is successfully matched, the information at each level within the group is parsed according to the field attributes: "GigabitEthernet0 / 1" is extracted from the group-level commands as the primary key "interface name" of the main intermediate table; "connected server room" is extracted from the child-level information as the value of the "description" field, and "1000" is extracted as the value of the "rate" field in the "rate configuration" child table. Based on the rule that the child table and the main table are associated through "interface name", the child table data is associated with the main table data to form complete intermediate table data, including the interface name and description information of the main table and the interface name and rate information of the child table.
[0042] Convert to database table data: Based on the device type, a pre-defined mapping relationship between intermediate tables and database tables (defined in an XML file) is established. This relationship clarifies the database table identifiers, loading order, and the correspondence between intermediate table fields and database table fields (including field names, data types, and conversion rules). During conversion, according to the mapping rules, the "interface name" of the intermediate main table is converted to the "ifname" field (string data type) of the database table "tinterface," and the "description" is converted to the "ifdesc" field (string data type). Similarly, the "interface name" of the intermediate sub-table is converted to the "ifname" field of the database table "tinterfacespeed," and the "speed" is converted to the "speed" field according to the "lowercase to uppercase" rule. After conversion, the data is deduplicated. If records with the same "ifname" exist, they are sorted by primary key and merged, retaining only one record and processing the other field values according to a pre-defined method, ultimately yielding table data that meets the database requirements.
[0043] Load into the target database: Based on the structure of the database tables "tinterface" and "tinterfacespeed", a data insertion script is generated, containing field names and corresponding data values. For example, a record with `ifname` set to "GigabitEthernet0 / 1" and `ifdesc` set to "connected server room" is inserted into the `tinterface` table, and a record with `ifname` set to "GigabitEthernet0 / 1" and `speed` set to "1000" is inserted into the `tinterfacespeed` table. Executing this insertion script writes the data from the database tables to the target network management database, completing the entire synchronization process.
[0044] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A method for synchronizing command-line text, characterized in that, Includes the following steps: Obtain the command-line text in a preset format stored in the device, wherein the command-line text contains device resource configuration information; The command line text is grouped to obtain grouped data; Based on the parsing template corresponding to the device type, the grouped data is parsed to generate intermediate table data; Based on the device type, a preset mapping relationship between intermediate tables and database tables is matched, and the intermediate table data is converted into database table data. Load the data from the database table into the target database.
2. The method for synchronizing command-line text according to claim 1, characterized in that, The preset format command line text satisfies: The first line of a group-level command line has no leading space and uses a specific symbol as the group end marker or comment marker. After a child-level message is wrapped, it contains a preset number of leading spaces, and the hierarchy of child-level messages is distinguished by the number of leading spaces. Default configuration information is not displayed when it matches the baseline configuration.
3. The method for synchronizing command-line text according to claim 1, characterized in that, The grouping process of the command line text to obtain grouped data includes: Read the command line text line by line, and identify the line without a leading space as the start of a new group; The number of leading spaces determines the group and level of the child information. If the number of leading spaces in a certain line is N more than that in the previous line, the child information in that line is determined to be the next level of the corresponding group in the previous line, and a hierarchical relationship between the data within the group is established. Here, N is a preset positive integer used to distinguish the levels. Filter rows containing annotation markers to form structured grouped data.
4. The method for synchronizing command-line text according to claim 1, characterized in that, The parsing template is an XML format file, containing: Regular expressions used to match group-level command lines; The primary key identifier and field attributes of the intermediate table, wherein the field attributes include the regular expression for matching field values, value retrieval rules, and default values; If it includes sub-tables, it also includes the sub-table identifier, the sub-table primary key, and the association rules between the sub-table and the main table, where: The sub-table identifier is a name or number that uniquely identifies the sub-table; The primary key of a subtable is used to uniquely identify records in the subtable; The association rules between the child table and the main table are to establish the association through the same field value or to achieve the association through a specific mapping relationship.
5. The method for synchronizing command-line text according to claim 1, characterized in that, The step of parsing the grouped data to generate intermediate table data based on the parsing template corresponding to the device type includes: The XML parsing template corresponding to the device type is called, and the group-level command line of the group data is matched by regular expression. The first line of the group-level command line of the group data is compared with the regular expression used to match the group-level command line in the parsing template. If the match is successful, subsequent processing is performed. Parse information at all levels within the group based on field attributes, extract field values or assign default values, and generate main table data; The child information is parsed according to the child table association rules. When the child table and the main table are associated through the same field value, the data with the same field value as the main table is searched in the child information, the child table data is generated and associated with the main table data to form complete intermediate table data.
6. The method for synchronizing command-line text according to claim 1, characterized in that, The pre-defined mapping relationship between the intermediate table and the database table is defined through an XML file, which includes: Database table identifiers and loading order; The correspondence between database fields and intermediate table fields, including field names, data types, and optional numeric conversion rules.
7. The method for synchronizing command-line text according to claim 1, characterized in that, The step of matching the preset mapping relationship between the intermediate table and the database table according to the device type and converting the intermediate table data into database table data includes: Call the XML file corresponding to the device type to determine the mapping rules between intermediate table fields and database fields. These mapping rules include the correspondence between intermediate table field names and database field names, as well as data type conversion rules. The intermediate table data is transformed according to the mapping rules. When there are numerical transformation rules, the numerical transformation is performed according to the rules. The converted data is deduplicated, and records with the same primary key are merged. This is done by sorting the converted data according to the primary key field of the database table, merging adjacent records with the same primary key value, retaining one record, and processing the other field values according to the preset merging method.
8. The method for synchronizing command-line text according to claim 1, characterized in that, The preset format command line text stored in the device includes: Command-line text is downloaded from the device to the local server via a file transfer protocol that supports resuming interrupted downloads.
9. The method for synchronizing command-line text according to claim 1, characterized in that, The step of loading the database table data into the target database includes: Generate a data insertion script based on the database table structure. The script includes field names and corresponding data values. The insertion script is executed to write the database table data into the target database.
10. A command-line text synchronization system, characterized in that, A method for synchronizing command-line text as described in any one of claims 1-9, comprising: The text acquisition module is used to acquire command-line text in a preset format stored on the device; The grouping module is used to group the command line text to obtain grouped data. The parsing module is used to match the corresponding parsing template based on the device type and parse the grouped data to generate intermediate table data; The conversion module is used to match a preset mapping relationship between an intermediate table and a database table according to the device type, and convert the intermediate table data into database table data. The loading module is used to load the database table data into the target database.