class SSHTest(unittest.TestCase): port = 1050 hostname = 'localhost' @classmethod def setUpClass(cls): users = {'testadmin': 'x'} MockSSH.startThreadedServer( commands, prompt="[root@hostname:Active] testadmin # ", interface=SSHTest.hostname, port=SSHTest.port, **users) time.sleep(0.5) @classmethod def tearDownClass(cls): print("tearDownClass") MockSSH.stopThreadedServer() def setUp(self): self.that = SSH(SSHTest.hostname, None, None, SSHTest.port) self.that.set_missing_host_key_policy(WarningPolicy()) def tearDown(self): self.that.close_connection() def testNoError(self): ''' Tests if out exists after running a command. ''' (_, out, _) = self.that.run('ls') self.assertIsNotNone(out) def testLsReturnValue(self): ''' Checks the return value of ls ''' (_, out, _) = self.that.run('ls') self.assertEqual(0, out.channel.recv_exit_status()) def testLsStringReturned(self): ''' Checks the existance of a string returned by ls. ''' (_, out, _) = self.that.run('ls') output = out.read().decode() self.assertIsNotNone(output) print("\nls returned %s" % output) def testEchoStringReturned(self): ''' Tests the reply of the echo command. ''' string = 'The reply' (_, out, _) = self.that.run('echo -n ' + string) output = out.read().decode() print("\necho returned %s" % output) self.assertEqual(output, string)
class SSHTest(unittest.TestCase): ''' Does an Integration test of the SSH class. To succesfully run this test fill in the parameters above with values corresponding to an SSH server. This can be any SSH server that you can log into. It should even work when the SSH server is local. After running please reset all the parameters to None. ''' def setUp(self): self.that = SSH(hostname, user, pwd, port) def tearDown(self): self.that.close_connection() def testNoError(self): ''' Tests if out exists after running a command. ''' (_, out, _) = self.that.run('ls') self.assertIsNotNone(out) def testLsReturnValue(self): ''' Checks the return value of ls ''' (_, out, _) = self.that.run('ls') self.assertEqual(0, out.channel.recv_exit_status()) def testLsStringReturned(self): ''' Checks the existance of a string returned by ls. ''' (_, out, _) = self.that.run('ls') output = out.read().decode() self.assertIsNotNone(output) #print("\nls returned %s" % output) def testEchoStringReturned(self): ''' Tests the reply of the echo command. ''' string = 'The reply' (_, out, _) = self.that.run('echo -n ' + string) output = out.read().decode() #print("\necho returned %s" % output) self.assertEqual(output, string)
def main(): # Input files and variables start_time = datetime.now() # Devices file and commands source devices_file = "/app/src/devices_commands.csv" # Format commands and devices on dictionary for outputs new_collector = Collector(devices_file) new_collector.get_env() devices_commands = new_collector.format_devices() new_collector.output_json() # Send SSH sessions for outputs # Number of threads number_threads = 10 # Number of retry number_retry = 3 new_ssh = SSH(devices_commands,number_threads,number_retry) new_ssh.run() print("Process Collect Files Done: " + str(datetime.now() - start_time))
def get_resource_usages(self): #query each slash2 compenent for resource usage rusages = {} for host, type, ctl_path, pid in self.runtime["daemons"]: print type user = os.getenv("USER") ssh = SSH(user, host, ''); kernel = "".join(ssh.run("uname -s")["out"]).lower() if "linux" in kernel: output = self.check_status(ssh, type, ctl_path, pid) elif "bsd" in kernel: output = self.query_ctl_rusage(ssh, type, ctl_path) else: # do something pass rusages[host] = output return rusages sys.exit(1)