No relevant resource is found in the selected language.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read our privacy policy>Search

Reminder

To have a better experience, please upgrade your IE browser.

upgrade

CX11x, CX31x, CX710 (Earlier Than V6.03), and CX91x Series Switch Modules V100R001C10 Configuration Guide 12

The documents describe the configuration of various services supported by the CX11x&CX31x&CX91x series switch modules The description covers configuration examples and function configurations.
Rate and give feedback:
Huawei uses machine translation combined with human proofreading to translate this document to different languages in order to help you better understand the content of this document. Note: Even the most advanced machine translation cannot match the quality of professional translators. Huawei shall not bear any responsibility for translation accuracy and it is recommended that you refer to the English document (a link for which has been provided).
Configuration Examples

Configuration Examples

This section provides an OPS configuration example, including the networking requirements, configuration roadmap, and configuration procedure.

Example for Using a Python Script to Automatically Back Up the Configuration File

Networking Requirements

As shown in Figure 3-35, an administrator manages SwitchA remotely over a network. The administrator hopes that SwitchA can automatically back up the configuration file every time the configuration is modified and saved, reducing maintenance workload.

Figure 3-35 Networking for automatic configuration file backup using a Python script
Configuration Roadmap
The configuration roadmap is as follows:
  1. Make a Python script that can obtain and copy the current configuration file.

  2. Upload the Python script to SwitchA and install the script.

  3. Bind the Python script to an assistant and set the trigger condition of the assistant to the configuration change event. Then the assistant can run the Python script automatically when the configuration file changes.

Procedure

  1. Make a Python script.

    # Create a Python script named backupconfig.py. For details about the script format, see Example of backupconfig.py script. This Python script implements the following functions:
    1. Obtains the current configuration using the APIs described in the OPS API Reference.

    2. Backs up the current configuration file. The backup configuration file name is current configuration file name_backup.

  2. Upload and install the Python script.

    # Enable the FTP service on SwitchA. Set the user name to ftpuser and password to pWD@123456.

    <HUAWEI> system-view
    [~HUAWEI] sysname SwitchA
    [*HUAWEI] commit
    [~SwitchA] ftp server enable
    [*SwitchA] commit
    [~SwitchA] aaa
    [~SwitchA-aaa] local-user ftpuser password irreversible-cipher pWD@123456
    [*SwitchA-aaa] local-user ftpuser service-type ftp
    [*SwitchA-aaa] local-user ftpuser level 15
    [*SwitchA-aaa] local-user ftpuser ftp-directory flash:/
    [*SwitchA-aaa] commit
    [~SwitchA-aaa] return
    

    # On the PC, set up an FTP connection to SwitchA, and enter the user name and password. Set the file transfer format to binary and the working directory of the FTP client to c:\temp (where the Python is saved).

    C:\Documents and Settings\Administrator> ftp 10.1.1.1
    Connected to 10.1.1.1.
    220 FTP service ready.
    User (10.1.1.1:(none)): ftpuser
    331 Password required for ftpuser.
    Password:
    230 User logged in.
    ftp> binary
    200 Type set to I.
    ftp> lcd c:\temp
    Local directory now C:\temp.
    

    # On the PC, upload the Python script from c:\temp to SwitchA.

    ftp> put backupconfig.py
    200 Port command okay.
    150 Opening BINARY mode data connection for backupconfig.py.
    226 Transfer complete
    

    # Install the Python script on SwitchA.

    <SwitchA> ops install file backupconfig.py
    

    # After the script is installed, manually run the script and check whether there is a backup configuration file in flash:/. If the backup configuration file exists, the script can implement the configuration file backup function.

    <SwitchA> ops run python backupconfig.py
    

  3. Configure an assistant.

    # Create an assistant and set the trigger condition of the assistant to the configuration file change event (cfg_file_change). Bind the Python script backupconfig.py to the assistant.

    <SwitchA> system-view
    [~SwitchA] ops
    [~SwitchA-ops] assistant backup_config
    [*SwitchA-ops-assistant-backup_config] condition event feature configuration name cfg_file_change
    [*SwitchA-ops-assistant-backup_config] execute 1 python backupconfig.py
    [*SwitchA-ops-assistant-backup_config] commit
    [~SwitchA-ops-assistant-backup_config] return
    

  4. Verify the configuration.

    # Check the configuration of the assistant.

    <SwitchA> display ops assistant verbose name backup_config
    Assistant         : backup_config 
    Task abstract     : backupconfig.py  
    Trigger time      : 0000-00-00 00:00:00+08:00  
    State             : ready                        
    Running times     : 0                          
    Trigger type      : event                       
    Trigger event     : --
    

    # After the configuration is complete, run the save command to save the configuration, and then check whether a backup configuration file is saved in flash:/.

Example of backupconfig.py script
#!/usr/bin/env python

import traceback
import httplib
import string

class OPSConnection(object):
    """Make an OPS connection instance."""

    def __init__(self, host, port = 80):
        self.host = host
        self.port = port
        self.headers = {
            "Content-type": "text/xml",
            "Accept":       "text/xml"
            }
        self.conn = None
		
    def close(self):
        """Close the connection"""
        self.conn.close()

    def create(self, uri, req_data):
        """Create operation"""
        ret = self.rest_call("POST", uri, req_data)
        return ret

    def delete(self, uri, req_data):
        """Delete operation"""
        ret = self.rest_call("DELETE", uri, req_data)
        return ret

    def get(self, uri, req_data = None):
        """Get operation"""
        ret = self.rest_call("GET", uri, req_data)
        return ret

    def set(self, uri, req_data):
        """Set operation"""
        ret = self.rest_call("PUT", uri, req_data)
        return ret

    def rest_call(self, method, uri, req_data):
        """REST call"""
        print('|---------------------------------- request: ----------------------------------|')
        print('%s %s HTTP/1.1\n' % (method, uri))
        if req_data == None:
            body = ""
        else:
            body = req_data
            print(body)
        if self.conn:
            self.conn.close()
        self.conn = httplib.HTTPConnection(self.host, self.port)

        self.conn.request(method, uri, body, self.headers)
        response = self.conn.getresponse()
        response.status = httplib.OK    # stub code
        ret = (response.status, response.reason, response.read())
        print('|---------------------------------- response: ---------------------------------|')
        print('HTTP/1.1 %s %s\n\n%s' % ret)
        print('|------------------------------------------------------------------------------|')
        return ret

def get_startup_info(ops_conn):
    """Get startup info. """

    uri = "/cfg/startupInfos/startupInfo"
    req_data = \
'''<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>
'''
    ret, _, rsp_data = ops_conn.get(uri, req_data)
    if ret != httplib.OK:
        return None

    return rsp_data

def copy_file(ops_conn,cfgFileName):
	"""Copy configuration."""
	
	uri = "/vfm/copyFile"
	str_temp = string.Template(
'''<?xml version="1.0" encoding="UTF-8"?>
<copyFile>
    <srcFileName>$srcFileName</srcFileName>
    <desFileName>$desFileName</desFileName>
</copyFile>
''')
	copyFileName = cfgFileName.rsplit(".",1)[0] + "_backup." + cfgFileName.rsplit(".",1)[1]
	req_data = str_temp.substitute(srcFileName = cfgFileName,desFileName = copyFileName)
	ret, _, rsp_data = ops_conn.create(uri, req_data)
	if ret != httplib.OK:
		return None
	return rsp_data
	
def main():
    """The main function."""
    
    host = "localhost"
    try:
        ops_conn = OPSConnection(host)
        print('+-------------------------- Open a OPS connection. ----------------------------+')        
        rsp_data = get_startup_info(ops_conn)
        if rsp_data is not None:            
            cfgFileName = rsp_data[rsp_data.find("curStartupFile")+15 : rsp_data.rfind("curStartupFile")-2]
            copy_file(ops_conn,cfgFileName)
        ops_conn.close()
        print('+-------------------------- Close a OPS connection. ---------------------------+')
        return
    except:
        errinfo = traceback.format_exc()
        print(errinfo)
        return

if __name__ == "__main__":
    main()
Configuration Files
  • # Configuration file of SwitchA

    #                                                                                                                                   
    sysname SwitchA                                                                                                                
    #                                                                                                                                   
    aaa                                                                                                                                 
     local-user ftpuser password irreversible-cipher $1a$[y5oM&cc10$Y{"7R,0@&6L+a-H+"ux/6v+0BjJsQSz$0##(,[bH$                           
     local-user ftpuser service-type ftp                                                                                                
     local-user ftpuser level 15                                                                                                        
     local-user ftpuser ftp-directory flash:/                                                                                           
    #                                                                                                                                   
    ops                                                                                                                                 
     assistant backup_config                                                                                                            
      execute 1 python backupconfig.py                                                                        
      condition event feature configuration name cfg_file_change
    # 
    return
Translation
Download
Updated: 2019-08-09

Document ID: EDOC1000041694

Views: 57468

Downloads: 3619

Average rating:
This Document Applies to these Products
Related Version
Related Documents
Share
Previous Next