Ejemplo n.º 1
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            clicommand=dict(required=True),
            outputfile=dict(required=True),
            host=dict(required=True),
            deviceType=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),),
        supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    cliCommand = module.params['clicommand']
    deviceType = module.params['deviceType']
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#", 2, remote_conn)

    # Send the CLi command
    output = output + cnos.waitForDeviceResponse(cliCommand + "\n", "(config)#", 2, remote_conn)

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if(errorMsg is None):
        module.exit_json(changed=True, msg="CLI command executed and results saved in file ")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            clicommand=dict(required=True),
            outputfile=dict(required=True),
            host=dict(required=True),
            deviceType=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),),
        supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    cliCommand = module.params['clicommand']
    deviceType = module.params['deviceType']
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#", 2, remote_conn)

    # Send the CLi command
    output = output + cnos.waitForDeviceResponse(cliCommand + "\n", "(config)#", 2, remote_conn)

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if(errorMsg is None):
        module.exit_json(changed=True, msg="CLI command executed and results saved in file ")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 3
0
def main():
    module = AnsibleModule(argument_spec=dict(
        outputfile=dict(required=True),
        host=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        enablePassword=dict(required=False, no_log=True),
        deviceType=dict(required=True),
        protocol=dict(required=True),
        serverip=dict(required=True),
        imgpath=dict(required=True),
        imgtype=dict(required=True),
        serverusername=dict(required=False),
        serverpassword=dict(required=False, no_log=True),
    ),
                           supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    outputfile = module.params['outputfile']
    host = module.params['host']
    deviceType = module.params['deviceType']
    protocol = module.params['protocol'].lower()
    imgserverip = module.params['serverip']
    imgpath = module.params['imgpath']
    imgtype = module.params['imgtype']
    imgserveruser = module.params['serverusername']
    imgserverpwd = module.params['serverpassword']
    output = ""
    timeout = 120
    tftptimeout = 600

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(host, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3,
                                                    remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2,
                                                 remote_conn)

    transfer_status = ""
    # Invoke method for image transfer from server
    if (protocol == "tftp" or protocol == "ftp"):
        transfer_status = cnos.doImageTransfer(protocol, tftptimeout,
                                               imgserverip, imgpath, imgtype,
                                               imgserveruser, imgserverpwd,
                                               remote_conn)
    elif (protocol == "sftp" or protocol == "scp"):
        transfer_status = cnos.doSecureImageTransfer(protocol, timeout,
                                                     imgserverip, imgpath,
                                                     imgtype, imgserveruser,
                                                     imgserverpwd, remote_conn)
    else:
        transfer_status = "Invalid Protocol option"

    output = output + "\n Image Transfer status \n" + transfer_status

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg is None):
        module.exit_json(changed=True, msg="Image file tranferred to device")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 4
0
def main():
    module = AnsibleModule(argument_spec=dict(
        commandfile=dict(required=True),
        outputfile=dict(required=True),
        host=dict(required=True),
        deviceType=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        enablePassword=dict(required=False, no_log=True),
    ),
                           supports_check_mode=False)
    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    commandfile = module.params['commandfile']
    outputfile = module.params['outputfile']
    deviceType = module.params['deviceType']
    hostIP = module.params['host']
    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3,
                                                    remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2,
                                                 remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#",
                                                 2, remote_conn)

    # Send commands one by one
    #with open(commandfile, "r") as f:
    f = open(commandfile, "r")
    for line in f:
        # Omit the comment lines in template file
        if not line.startswith("#"):
            command = line
            if not line.endswith("\n"):
                command = command + "\n"
            response = cnos.waitForDeviceResponse(command, "#", 2, remote_conn)
            errorMsg = cnos.checkOutputForError(response)
            output = output + response
            if (errorMsg is not None):
                break  # To cater to Mufti case
    # Write to memory
    output = output + cnos.waitForDeviceResponse("save\n", "#", 3, remote_conn)
    # Write output to file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg is None):
        module.exit_json(changed=True, msg="Template Applied")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 5
0
def main():
    #
    # Define parameters for vlan creation entry
    #
    module = AnsibleModule(argument_spec=dict(
        outputfile=dict(required=True),
        host=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        enablePassword=dict(required=False, no_log=True),
        deviceType=dict(required=True),
        vlanArg1=dict(required=True),
        vlanArg2=dict(required=False),
        vlanArg3=dict(required=False),
        vlanArg4=dict(required=False),
        vlanArg5=dict(required=False),
    ),
                           supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    vlanArg1 = module.params['vlanArg1']
    vlanArg2 = module.params['vlanArg2']
    vlanArg3 = module.params['vlanArg3']
    vlanArg4 = module.params['vlanArg4']
    vlanArg5 = module.params['vlanArg5']
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    deviceType = module.params['deviceType']

    output = ""
    if not HAS_PARAMIKO:
        module.fail_json(msg='paramiko is required for this module')

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in
    # your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + \
        cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + \
        cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    # Go to config mode
    output = output + \
        cnos.waitForDeviceResponse("conf d\n", "(config)#", 2, remote_conn)

    # Send the CLi command
    output = output + \
        cnos.vlanConfig(
            remote_conn, deviceType, "(config)#", 2, vlanArg1, vlanArg2,
            vlanArg3, vlanArg4, vlanArg5)

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # need to add logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg is None):
        module.exit_json(changed=True,
                         msg="VLAN configuration is accomplished")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 6
0
def main():
    module = AnsibleModule(argument_spec=dict(
        outputfile=dict(required=True),
        host=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        enablePassword=dict(required=False, no_log=True),
        deviceType=dict(required=True),
    ),
                           supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    cliCommand = "reload \n"
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    deviceType = module.params['deviceType']
    output = ""
    if not HAS_PARAMIKO:
        module.fail_json(msg='paramiko is required for this module')

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3,
                                                    remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2,
                                                 remote_conn)

    # Send the CLi command
    output = output + cnos.waitForDeviceResponse(cliCommand, "(y/n):", 2,
                                                 remote_conn)

    # Send the Confirmation y
    output = output + cnos.waitForDeviceResponse("y\n", "#", 2, remote_conn)

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg in "Device Response Timed out"):
        module.exit_json(changed=True,
                         msg="Device is Reloading. Please wait...")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 7
0
def main():
    module = AnsibleModule(argument_spec=dict(
        outputfile=dict(required=True),
        host=dict(required=True),
        username=dict(required=True),
        password=dict(required=True, no_log=True),
        enablePassword=dict(required=False, no_log=True),
        deviceType=dict(required=True),
        interfaceRange=dict(required=False),
        interfaceOption=dict(required=False),
        interfaceArg1=dict(required=True),
        interfaceArg2=dict(required=False),
        interfaceArg3=dict(required=False),
        interfaceArg4=dict(required=False),
        interfaceArg5=dict(required=False),
        interfaceArg6=dict(required=False),
        interfaceArg7=dict(required=False),
    ),
                           supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    interfaceRange = module.params['interfaceRange']
    interfaceOption = module.params['interfaceOption']
    interfaceArg1 = module.params['interfaceArg1']
    interfaceArg2 = module.params['interfaceArg2']
    interfaceArg3 = module.params['interfaceArg3']
    interfaceArg4 = module.params['interfaceArg4']
    interfaceArg5 = module.params['interfaceArg5']
    interfaceArg6 = module.params['interfaceArg6']
    interfaceArg7 = module.params['interfaceArg7']
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    deviceType = module.params['deviceType']

    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3,
                                                    remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2,
                                                 remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#",
                                                 2, remote_conn)

    # Send the CLi command
    if (interfaceOption is None or interfaceOption == ""):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, None, interfaceRange,
            interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4,
            interfaceArg5, interfaceArg6, interfaceArg7)
    elif (interfaceOption == "ethernet"):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, "ethernet",
            interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3,
            interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif (interfaceOption == "loopback"):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, "loopback",
            interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3,
            interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif (interfaceOption == "mgmt"):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, "mgmt", interfaceRange,
            interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4,
            interfaceArg5, interfaceArg6, interfaceArg7)
    elif (interfaceOption == "port-aggregation"):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, "port-aggregation",
            interfaceRange, interfaceArg1, interfaceArg2, interfaceArg3,
            interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif (interfaceOption == "vlan"):
        output = output + cnos.interfaceConfig(
            remote_conn, deviceType, "(config)#", 2, "vlan", interfaceRange,
            interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4,
            interfaceArg5, interfaceArg6, interfaceArg7)
    else:
        output = "Invalid interface option \n"
    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg is None):
        module.exit_json(changed=True, msg="Interface Configuration is done")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 8
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            outputfile=dict(required=True),
            host=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),
            deviceType=dict(required=True),
            protocol=dict(required=True),
            serverip=dict(required=True),
            imgpath=dict(required=True),
            imgtype=dict(required=True),
            serverusername=dict(required=False),
            serverpassword=dict(required=False, no_log=True),),
        supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    outputfile = module.params['outputfile']
    host = module.params['host']
    deviceType = module.params['deviceType']
    protocol = module.params['protocol'].lower()
    imgserverip = module.params['serverip']
    imgpath = module.params['imgpath']
    imgtype = module.params['imgtype']
    imgserveruser = module.params['serverusername']
    imgserverpwd = module.params['serverpassword']
    output = ""
    timeout = 120
    tftptimeout = 600

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(host, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    transfer_status = ""
    # Invoke method for image transfer from server
    if(protocol == "tftp" or protocol == "ftp"):
        transfer_status = cnos.doImageTransfer(protocol, tftptimeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn)
    elif(protocol == "sftp" or protocol == "scp"):
        transfer_status = cnos.doSecureImageTransfer(protocol, timeout, imgserverip, imgpath, imgtype, imgserveruser, imgserverpwd, remote_conn)
    else:
        transfer_status = "Invalid Protocol option"

    output = output + "\n Image Transfer status \n" + transfer_status

    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if(errorMsg is None):
        module.exit_json(changed=True, msg="Image file tranferred to device")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 9
0
def main():
    #
    # Define parameters for config save entry
    #
    module = AnsibleModule(
        argument_spec=dict(
            outputfile=dict(required=True),
            host=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),
            #deviceType=dict(required=True),),
            deviceType=dict(required=False),
        ),
        supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    cliCommand = "write memory \n"
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    #deviceType = module.params['deviceType']
    deviceType = "g8272_cnos"
    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP,
                            username=username,
                            password=password,
                            look_for_keys=False)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3,
                                                    remote_conn)

    #Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal-length 0\n", "#", 2,
                                                 remote_conn)

    #cnos.debugOutput(cliCommand)
    #Send the CLi command
    output = output + cnos.waitForDeviceResponse(cliCommand, "#", 2,
                                                 remote_conn)

    #Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    errorMsg = cnos.checkOutputForError(output)
    if (errorMsg == None):
        module.exit_json(
            changed=True,
            msg="Switch Running Config is Saved to Startup Config ")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 10
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            commandfile=dict(required=True),
            outputfile=dict(required=True),
            host=dict(required=True),
            deviceType=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),),
        supports_check_mode=False)
    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    commandfile = module.params['commandfile']
    outputfile = module.params['outputfile']
    deviceType = module.params['deviceType']
    hostIP = module.params['host']
    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#", 2, remote_conn)

    # Send commands one by one
    #with open(commandfile, "r") as f:
    f = open(commandfile, "r")
    for line in f:
        # Omit the comment lines in template file
        if not line.startswith("#"):
            command = line
            if not line.endswith("\n"):
                command = command+"\n"
            response = cnos.waitForDeviceResponse(command, "#", 2, remote_conn)
            errorMsg = cnos.checkOutputForError(response)
            output = output + response
            if(errorMsg is not None):
                break   # To cater to Mufti case
    # Write to memory
    output = output + cnos.waitForDeviceResponse("save\n", "#", 3, remote_conn)
    # Write output to file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if(errorMsg is None):
        module.exit_json(changed=True, msg="Template Applied")
    else:
        module.fail_json(msg=errorMsg)
Ejemplo n.º 11
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            outputfile=dict(required=True),
            host=dict(required=True),
            username=dict(required=True),
            password=dict(required=True, no_log=True),
            enablePassword=dict(required=False, no_log=True),
            deviceType=dict(required=True),
            interfaceRange=dict(required=False),
            interfaceOption=dict(required=False),
            interfaceArg1=dict(required=True),
            interfaceArg2=dict(required=False),
            interfaceArg3=dict(required=False),
            interfaceArg4=dict(required=False),
            interfaceArg5=dict(required=False),
            interfaceArg6=dict(required=False),
            interfaceArg7=dict(required=False),),
        supports_check_mode=False)

    username = module.params['username']
    password = module.params['password']
    enablePassword = module.params['enablePassword']
    interfaceRange = module.params['interfaceRange']
    interfaceOption = module.params['interfaceOption']
    interfaceArg1 = module.params['interfaceArg1']
    interfaceArg2 = module.params['interfaceArg2']
    interfaceArg3 = module.params['interfaceArg3']
    interfaceArg4 = module.params['interfaceArg4']
    interfaceArg5 = module.params['interfaceArg5']
    interfaceArg6 = module.params['interfaceArg6']
    interfaceArg7 = module.params['interfaceArg7']
    outputfile = module.params['outputfile']
    hostIP = module.params['host']
    deviceType = module.params['deviceType']

    output = ""

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # initiate SSH connection with the switch
    remote_conn_pre.connect(hostIP, username=username, password=password)
    time.sleep(2)

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    time.sleep(2)

    # Enable and enter configure terminal then send command
    output = output + cnos.waitForDeviceResponse("\n", ">", 2, remote_conn)

    output = output + cnos.enterEnableModeForDevice(enablePassword, 3, remote_conn)

    # Make terminal length = 0
    output = output + cnos.waitForDeviceResponse("terminal length 0\n", "#", 2, remote_conn)

    # Go to config mode
    output = output + cnos.waitForDeviceResponse("configure d\n", "(config)#", 2, remote_conn)

    # Send the CLi command
    if(interfaceOption is None or interfaceOption == ""):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, None, interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif(interfaceOption == "ethernet"):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "ethernet", interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif(interfaceOption == "loopback"):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "loopback", interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif(interfaceOption == "mgmt"):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "mgmt", interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif(interfaceOption == "port-aggregation"):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "port-aggregation", interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    elif(interfaceOption == "vlan"):
        output = output + cnos.interfaceConfig(remote_conn, deviceType, "(config)#", 2, "vlan", interfaceRange,
                                               interfaceArg1, interfaceArg2, interfaceArg3, interfaceArg4, interfaceArg5, interfaceArg6, interfaceArg7)
    else:
        output = "Invalid interface option \n"
    # Save it into the file
    file = open(outputfile, "a")
    file.write(output)
    file.close()

    # Logic to check when changes occur or not
    errorMsg = cnos.checkOutputForError(output)
    if(errorMsg is None):
        module.exit_json(changed=True, msg="Interface Configuration is done")
    else:
        module.fail_json(msg=errorMsg)