Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions nettacker/core/lib/ssh.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
import logging

from paramiko import SSHClient, AutoAddPolicy
import socket
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you "probably" don't need this here... look at the other comment below

from paramiko import SSHClient, AutoAddPolicy, AuthenticationException, SSHException
from paramiko.auth_strategy import NoneAuth, Password

from nettacker.core.lib.base import BaseEngine, BaseLibrary

logging.getLogger("paramiko.transport").disabled = True


class SshLibrary(BaseLibrary):
client = SSHClient

def brute_force(self, *args, **kwargs):
host = kwargs["host"]
port = kwargs["port"]
username = kwargs["username"]
password = kwargs["password"]
password = kwargs.get("password")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change what's not broken

timeout = kwargs.get("timeout", 3)

connection = self.client()
connection.set_missing_host_key_policy(AutoAddPolicy())
connection.connect(
**{
"hostname": host,

try:
connection.connect(
hostname=host,
port=port,
auth_strategy=Password(username=username, password_getter=lambda: password) if password else NoneAuth(username=username),
timeout=timeout,
look_for_keys=False,
allow_agent=False,
banner_timeout=timeout,
)
transport = connection.get_transport()
if not transport or not transport.is_active():
return {}
return {
"host": host,
"port": port,
"auth_strategy": Password(username=username, password_getter=lambda: password)
if password
else NoneAuth(username=username),
"username": username,
"password": password if password else "",
}
)

return {
"host": host,
"port": port,
"username": username,
"password": password,
}

except AuthenticationException:
return {}
except SSHException:
return {}
except (socket.timeout, socket.error, ConnectionRefusedError, OSError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use both OSError and socket.error. Python3+ they are basically aliases. Also, you really aren't doing anything special by handling them separately.

return {}
except Exception:
return {}
finally:
try:
connection.close()
except Exception:
pass

class SshEngine(BaseEngine):
library = SshLibrary
Loading