# Buggy code
def connect(self, **kwargs):
    host = self.module.params["host"]
    port = self.module.params["port"] or 22

    username = self.module.params["username"]
    password = self.module.params["password"]
    key_filename = self.module.params["ssh_keyfile"]
    timeout = self.module.params["timeout"]

    try:
        self.shell = Shell(
            kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE
        )
        self.shell.open(
            host,
            port=port,
            username=username,
            password=password,
            key_filename=key_filename,
            timeout=timeout,
        )
    except ShellError:
        e = get_exception()
        msg = "failed to connect to %s:%s - %s" % (host, port, str(e))
        self.module.fail_json(msg=msg)


# Traceback (paramiko.ssh_exception.SSHException)
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
2351276/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_bEsrK8/ansible_module_ios_command.py", line 169, in <module>
main()
File "/tmp/ansible_bEsrK8/ansible_module_ios_command.py", line 144, in main
response = module.execute(commands)
File "/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py", line 146, in execute
File "/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py", line 129, in connect
File "/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py", line 82, in connect
File "/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/shell.py", line 107, in open
File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 380, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 598, in _auth
raise SSHException('No authentication methods available')
paramiko.ssh_exception.SSHException: No authentication methods available

fatal: [freqm]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "ios_command"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_bEsrK8/ansible_module_ios_command.py\", line 169, in <module>\n    main()\n  File \"/tmp/ansible_bEsrK8/ansible_module_ios_command.py\", line 144, in main\n    response = module.execute(commands)\n  File \"/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py\", line 146, in execute\n  File \"/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py\", line 129, in connect\n  File \"/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/ios.py\", line 82, in connect\n  File \"/tmp/ansible_bEsrK8/ansible_modlib.zip/ansible/module_utils/shell.py\", line 107, in open\n  File \"/usr/local/lib/python2.7/site-packages/paramiko/client.py\", line 380, in connect\n    look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)\n  File \"/usr/local/lib/python2.7/site-packages/paramiko/client.py\", line 598, in _auth\n    raise SSHException('No authentication methods available')\nparamiko.ssh_exception.SSHException: No authentication methods available\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}

# Fixed code
def connect(self, **kwargs):
    host = self.module.params["host"]
    port = self.module.params["port"] or 22

    username = self.module.params["username"]
    password = self.module.params["password"]
    key_filename = self.module.params["ssh_keyfile"]
    timeout = self.module.params["timeout"]

    allow_agent = (key_filename is not None) or (
        key_filename is None and password is None
    )

    try:
        self.shell = Shell(
            kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE
        )
        self.shell.open(
            host,
            port=port,
            username=username,
            password=password,
            key_filename=key_filename,
            allow_agent=allow_agent,
            timeout=timeout,
        )
    except ShellError:
        e = get_exception()
        msg = "failed to connect to %s:%s - %s" % (host, port, str(e))
        self.module.fail_json(msg=msg)