Skip to content

Commit

Permalink
remote connect and close
Browse files Browse the repository at this point in the history
if no password, use pyscada user ssh key
close the connection on exceptions
  • Loading branch information
clavay committed Jul 4, 2023
1 parent b9fd6c4 commit 5d71bcf
Showing 1 changed file with 75 additions and 18 deletions.
93 changes: 75 additions & 18 deletions pyscada/systemstat/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,38 @@ def connect(self):
self.inst = paramiko.SSHClient()
self.inst.load_system_host_keys()
self.inst.set_missing_host_key_policy(paramiko.WarningPolicy())
self.inst.connect(self._device.systemstatdevice.host,
self._device.systemstatdevice.port,
self._device.systemstatdevice.username,
self._device.systemstatdevice.password,
timeout=self._device.systemstatdevice.timeout)
if self._device.systemstatdevice.password is "":
self.inst.connect(
self._device.systemstatdevice.host,
self._device.systemstatdevice.port,
self._device.systemstatdevice.username,
timeout=self._device.systemstatdevice.timeout,
)
else:
self.inst.connect(
self._device.systemstatdevice.host,
self._device.systemstatdevice.port,
self._device.systemstatdevice.username,
self._device.systemstatdevice.password,
timeout=self._device.systemstatdevice.timeout,
)
channel = self.inst.get_transport().open_session()
channel.get_pty()
self.shell = self.inst.invoke_shell()
sleep(1)
while self.shell.recv_ready():
self.shell.recv(1)
self.inst.close()
return True
except (socket.gaierror, paramiko.ssh_exception.SSHException, OSError,
socket.timeout, paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.NoValidConnectionsError) as e:
self._not_accessible_reason = e
if self.inst is not None and hasattr(slef.inst, "close"):
try:
self.inst.close()
except:
pass
self.inst = None
return False

Expand Down Expand Up @@ -372,10 +388,21 @@ def read_data_all(self, variables_dict):
inst.load_system_host_keys()
inst.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
inst.connect(hostname, port, username, password, timeout=timeout)
if password is "":
inst.connect(hostname, port, username, timeout=timeout)
else:
inst.connect(
hostname, port, username, password, timeout=timeout
)
value = "OK"
inst.close()
except Exception as e:
value = str(e)
if inst is not None and hasattr(inst, "close"):
try:
inst.close()
except:
pass
elif item.systemstatvariable.information == 40:
param = item.systemstatvariable.parameter
try:
Expand Down Expand Up @@ -633,9 +660,18 @@ def exec_python_cmd(self, cmd, ssh_cmd=None, ssh_prefix=""):
inst.load_system_host_keys()
inst.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
inst.connect(hostname, port, username, password, timeout=timeout)
i, o, e = inst.exec_command("""python3 -c '""" + str(ssh_prefix) + """print(""" + str(cmd) + """)'""",
timeout=timeout)
if password is "":
inst.connect(hostname, port, username, timeout=timeout)
else:
inst.connect(hostname, port, username, password, timeout=timeout)
i, o, e = inst.exec_command(
"""python3 -c '"""
+ str(ssh_prefix)
+ """print("""
+ str(cmd)
+ """)'""",
timeout=timeout,
)
err = e.read().decode()
err = err[:-1] if err.endswith('\n')else err
if err != '':
Expand All @@ -647,12 +683,21 @@ def exec_python_cmd(self, cmd, ssh_cmd=None, ssh_prefix=""):
value = value[:-1] if value.endswith('\n') else value
value = None if value == '' else value
inst.close()
except (socket.gaierror, paramiko.ssh_exception.SSHException, OSError,
socket.timeout, paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.NoValidConnectionsError) as e:
pass
if value == '' and err != '':
logger.warning(f'Error running remote command {cmd} on {hostname}, returns : {err}')
except (
socket.gaierror,
paramiko.ssh_exception.SSHException,
OSError,
socket.timeout,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.NoValidConnectionsError,
) as e:
if inst is not None and hasattr(inst, "close"):
try:
inst.close()
except:
pass
value = None
value = None if value == 'None' else value
return value
Expand All @@ -673,17 +718,29 @@ def exec_cmd(self, cmd, ssh_cmd=None):
inst.load_system_host_keys()
inst.set_missing_host_key_policy(paramiko.WarningPolicy())
try:
inst.connect(hostname, port, username, password, timeout=timeout)
if password is "":
inst.connect(hostname, port, username, timeout=timeout)
else:
inst.connect(hostname, port, username, password, timeout=timeout)
i, o, e = inst.exec_command(str(cmd), timeout=timeout)
err = e.read().decode()
value = o.read().decode()
inst.close()
except (socket.gaierror, paramiko.ssh_exception.SSHException, OSError,
socket.timeout, paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.NoValidConnectionsError) as e:
pass
if value == '' and err != '':
logger.warning(f'Error running remote command {cmd} on {hostname}, returns : {err}')
except (
socket.gaierror,
paramiko.ssh_exception.SSHException,
OSError,
socket.timeout,
paramiko.ssh_exception.AuthenticationException,
paramiko.ssh_exception.NoValidConnectionsError,
) as e:
if inst is not None and hasattr(inst, "close"):
try:
inst.close()
except:
pass
value = None
return value

Expand Down

0 comments on commit 5d71bcf

Please sign in to comment.