Revert "black: re-format"
This reverts commit 5731818de0ce446ceaafc7e75ae39da1b69942ae.
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
Change-Id: Ie61cdc8c7f2825b0d9d66be87a6a3a058de2b372
diff --git a/ffdc/lib/ssh_utility.py b/ffdc/lib/ssh_utility.py
index fb44121..01b39dd 100644
--- a/ffdc/lib/ssh_utility.py
+++ b/ffdc/lib/ssh_utility.py
@@ -1,19 +1,16 @@
#!/usr/bin/env python3
-import logging
-import socket
-import time
-from socket import timeout as SocketTimeout
-
import paramiko
+from paramiko.ssh_exception import AuthenticationException
+from paramiko.ssh_exception import NoValidConnectionsError
+from paramiko.ssh_exception import SSHException
+from paramiko.ssh_exception import BadHostKeyException
from paramiko.buffered_pipe import PipeTimeout as PipeTimeout
-from paramiko.ssh_exception import (
- AuthenticationException,
- BadHostKeyException,
- NoValidConnectionsError,
- SSHException,
-)
from scp import SCPClient, SCPException
+import time
+import socket
+import logging
+from socket import timeout as SocketTimeout
class SSHRemoteclient:
@@ -23,6 +20,7 @@
"""
def __init__(self, hostname, username, password):
+
r"""
Description of argument(s):
@@ -40,6 +38,7 @@
self.password = password
def ssh_remoteclient_login(self):
+
r"""
Method to create a ssh connection to remote host.
"""
@@ -49,31 +48,23 @@
# SSHClient to make connections to the remote server
self.sshclient = paramiko.SSHClient()
# setting set_missing_host_key_policy() to allow any host
- self.sshclient.set_missing_host_key_policy(
- paramiko.AutoAddPolicy()
- )
+ self.sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
- self.sshclient.connect(
- hostname=self.hostname,
- username=self.username,
- password=self.password,
- banner_timeout=120,
- timeout=60,
- look_for_keys=False,
- )
+ self.sshclient.connect(hostname=self.hostname,
+ username=self.username,
+ password=self.password,
+ banner_timeout=120,
+ timeout=60,
+ look_for_keys=False)
- except (
- BadHostKeyException,
- AuthenticationException,
- SSHException,
- NoValidConnectionsError,
- socket.error,
- ) as e:
+ except (BadHostKeyException, AuthenticationException,
+ SSHException, NoValidConnectionsError, socket.error) as e:
is_ssh_login = False
return is_ssh_login
def ssh_remoteclient_disconnect(self):
+
r"""
Clean up.
"""
@@ -84,7 +75,8 @@
if self.scpclient:
self.scpclient.close()
- def execute_command(self, command, default_timeout=60):
+ def execute_command(self, command,
+ default_timeout=60):
"""
Execute command on the remote host.
@@ -93,26 +85,25 @@
"""
- empty = ""
+ empty = ''
cmd_start = time.time()
try:
- stdin, stdout, stderr = self.sshclient.exec_command(
- command, timeout=default_timeout
- )
+ stdin, stdout, stderr = \
+ self.sshclient.exec_command(command, timeout=default_timeout)
start = time.time()
while time.time() < start + default_timeout:
# Need to do read/write operation to trigger
# paramiko exec_command timeout mechanism.
xresults = stderr.readlines()
- results = "".join(xresults)
+ results = ''.join(xresults)
time.sleep(1)
if stdout.channel.exit_status_ready():
break
cmd_exit_code = stdout.channel.recv_exit_status()
# Convert list of string to one string
- err = ""
- out = ""
+ err = ''
+ out = ''
for item in results:
err += item
for item in stdout.readlines():
@@ -120,53 +111,30 @@
return cmd_exit_code, err, out
- except (
- paramiko.AuthenticationException,
- paramiko.SSHException,
- paramiko.ChannelException,
- SocketTimeout,
- ) as e:
+ except (paramiko.AuthenticationException, paramiko.SSHException,
+ paramiko.ChannelException, SocketTimeout) as e:
# Log command with error. Return to caller for next command, if any.
- logging.error(
- "\n\tERROR: Fail remote command %s %s" % (e.__class__, e)
- )
- logging.error(
- "\tCommand '%s' Elapsed Time %s"
- % (
- command,
- time.strftime(
- "%H:%M:%S", time.gmtime(time.time() - cmd_start)
- ),
- )
- )
+ logging.error("\n\tERROR: Fail remote command %s %s" % (e.__class__, e))
+ logging.error("\tCommand '%s' Elapsed Time %s" %
+ (command, time.strftime("%H:%M:%S", time.gmtime(time.time() - cmd_start))))
return 0, empty, empty
def scp_connection(self):
+
r"""
Create a scp connection for file transfer.
"""
try:
- self.scpclient = SCPClient(
- self.sshclient.get_transport(), sanitize=lambda x: x
- )
- logging.info(
- "\n\t[Check] %s SCP transport established.\t [OK]"
- % self.hostname
- )
+ self.scpclient = SCPClient(self.sshclient.get_transport(), sanitize=lambda x: x)
+ logging.info("\n\t[Check] %s SCP transport established.\t [OK]" % self.hostname)
except (SCPException, SocketTimeout, PipeTimeout) as e:
self.scpclient = None
- logging.error(
- "\n\tERROR: SCP get_transport has failed. %s %s"
- % (e.__class__, e)
- )
- logging.info(
- "\tScript continues generating FFDC on %s." % self.hostname
- )
- logging.info(
- "\tCollected data will need to be manually offloaded."
- )
+ logging.error("\n\tERROR: SCP get_transport has failed. %s %s" % (e.__class__, e))
+ logging.info("\tScript continues generating FFDC on %s." % self.hostname)
+ logging.info("\tCollected data will need to be manually offloaded.")
def scp_file_from_remote(self, remote_file, local_file):
+
r"""
scp file in remote system to local with date-prefixed filename.
@@ -183,9 +151,7 @@
except (SCPException, SocketTimeout, PipeTimeout, SSHException) as e:
# Log command with error. Return to caller for next file, if any.
logging.error(
- "\n\tERROR: Fail scp %s from remotehost %s %s\n\n"
- % (remote_file, e.__class__, e)
- )
+ "\n\tERROR: Fail scp %s from remotehost %s %s\n\n" % (remote_file, e.__class__, e))
# Pause for 2 seconds allowing Paramiko to finish error processing before next fetch.
# Without the delay after SCPException,
# next fetch will get 'paramiko.ssh_exception.SSHException'> Channel closed Error.
diff --git a/ffdc/lib/telnet_utility.py b/ffdc/lib/telnet_utility.py
index 03f7983..08e4071 100644
--- a/ffdc/lib/telnet_utility.py
+++ b/ffdc/lib/telnet_utility.py
@@ -1,21 +1,21 @@
#!/usr/bin/env python3
-import logging
-import socket
-import telnetlib
import time
+import socket
+import logging
+import telnetlib
from collections import deque
class TelnetRemoteclient:
+
r"""
Class to create telnet connection to remote host for command execution.
"""
- def __init__(
- self, hostname, username, password, port=23, read_timeout=None
- ):
+ def __init__(self, hostname, username, password, port=23, read_timeout=None):
+
r"""
Description of argument(s):
@@ -33,35 +33,23 @@
self.read_timeout = read_timeout
def tn_remoteclient_login(self):
+
is_telnet = True
try:
- self.tnclient = telnetlib.Telnet(
- self.hostname, self.port, timeout=15
- )
- if b"login:" in self.tnclient.read_until(
- b"login:", timeout=self.read_timeout
- ):
- self.tnclient.write(self.username.encode("utf-8") + b"\n")
+ self.tnclient = telnetlib.Telnet(self.hostname, self.port, timeout=15)
+ if b'login:' in self.tnclient.read_until(b'login:', timeout=self.read_timeout):
+ self.tnclient.write(self.username.encode('utf-8') + b"\n")
- if b"Password:" in self.tnclient.read_until(
- b"Password:", timeout=self.read_timeout
- ):
- self.tnclient.write(self.password.encode("utf-8") + b"\n")
+ if b'Password:' in self.tnclient.read_until(b'Password:', timeout=self.read_timeout):
+ self.tnclient.write(self.password.encode('utf-8') + b"\n")
- n, match, pre_match = self.tnclient.expect(
- [
- b"Login incorrect",
- b"invalid login name or password.",
- rb"\#",
- rb"\$",
- ],
- timeout=self.read_timeout,
- )
+ n, match, pre_match = \
+ self.tnclient.expect(
+ [b'Login incorrect', b'invalid login name or password.', br'\#', br'\$'],
+ timeout=self.read_timeout)
if n == 0 or n == 1:
logging.error(
- "\n\tERROR: Telnet Authentication Failed. Check"
- " userid and password.\n\n"
- )
+ "\n\tERROR: Telnet Authentication Failed. Check userid and password.\n\n")
is_telnet = False
else:
# login successful
@@ -88,15 +76,17 @@
# the telnet object might not exist yet, so ignore this one
pass
- def execute_command(self, cmd, i_timeout=120):
- r"""
- Executes commands on the remote host
+ def execute_command(self, cmd,
+ i_timeout=120):
- Description of argument(s):
- cmd Command to run on remote host
- i_timeout Timeout for command output
- default is 120 seconds
- """
+ r'''
+ Executes commands on the remote host
+
+ Description of argument(s):
+ cmd Command to run on remote host
+ i_timeout Timeout for command output
+ default is 120 seconds
+ '''
# Wait time for command execution before reading the output.
# Use user input wait time for command execution if one exists.
@@ -107,22 +97,23 @@
execution_time = 120
# Execute the command and read the command output.
- return_buffer = b""
+ return_buffer = b''
try:
+
# Do at least one non-blocking read.
# to flush whatever data is in the read buffer.
while self.tnclient.read_very_eager():
continue
# Execute the command
- self.tnclient.write(cmd.encode("utf-8") + b"\n")
+ self.tnclient.write(cmd.encode('utf-8') + b'\n')
time.sleep(execution_time)
- local_buffer = b""
+ local_buffer = b''
# Read the command output one block at a time.
return_buffer = self.tnclient.read_very_eager()
while return_buffer:
- local_buffer = b"".join([local_buffer, return_buffer])
+ local_buffer = b''.join([local_buffer, return_buffer])
time.sleep(3) # let the buffer fill up a bit
return_buffer = self.tnclient.read_very_eager()
except (socket.error, EOFError) as e:
@@ -138,4 +129,4 @@
logging.error("\t\t ERROR %s " % msg)
# Return ASCII string data with ending PROMPT stripped
- return local_buffer.decode("ascii", "ignore").replace("$ ", "\n")
+ return local_buffer.decode('ascii', 'ignore').replace('$ ', '\n')