def test_loading_key_files(self): for key_filename in [PKEY_FILENAME, DSA_KEY_FILENAME, ECDSA_KEY_FILENAME]: pkey = utils.load_private_key(key_filename) self.assertTrue(pkey, msg="Error loading key from file %s" % (key_filename,)) pkey = utils.load_private_key(open(key_filename)) self.assertTrue(pkey, msg="Error loading key from open file object for file %s" % (key_filename,)) fake_key = StringIO("blah blah fakey fakey key") self.assertFalse(utils.load_private_key(fake_key))
def test_loading_key_files(self): for key_filename in [PKEY_FILENAME, DSA_KEY_FILENAME, ECDSA_KEY_FILENAME]: pkey = utils.load_private_key(key_filename) self.assertTrue(pkey, msg="Error loading key from file %s" % (key_filename,)) pkey = utils.load_private_key(open(key_filename)) self.assertTrue(pkey, msg="Error loading key from open file object for file %s" % (key_filename,)) fake_key = BytesIO(b"blah blah fakey fakey key") self.assertFalse(utils.load_private_key(fake_key))
def test_loading_key_files(self): for key_filename in [PKEY_FILENAME, DSA_KEY_FILENAME, ECDSA_KEY_FILENAME]: pkey = utils.load_private_key(key_filename) self.assertTrue(pkey, msg="Error loading key from file %s" % (key_filename,)) pkey = utils.load_private_key(open(key_filename)) self.assertTrue(pkey, msg="Error loading key from open file object for file %s" % (key_filename,)) fake_key = StringIO("blah blah fakey fakey key\n") self.assertFalse(utils.load_private_key(fake_key)) fake_file = 'fake_key_file' with open(fake_file, 'wb') as fh: fh.write(b'fake key data\n') try: self.assertIsNone(utils.load_private_key(fake_file)) finally: os.unlink(fake_file)
def test_loading_key_files(self): for key_filename in [PKEY_FILENAME, DSA_KEY_FILENAME, ECDSA_KEY_FILENAME]: pkey = utils.load_private_key(key_filename) self.assertTrue(pkey, msg="Error loading key from file %s" % (key_filename,)) pkey = utils.load_private_key(open(key_filename)) self.assertTrue(pkey, msg="Error loading key from open file object for file %s" % (key_filename,)) fake_key = BytesIO(b"blah blah fakey fakey key\n") self.assertFalse(utils.load_private_key(fake_key)) fake_file = 'fake_key_file' with open(fake_file, 'wb') as fh: fh.write(b'fake key data\n') try: self.assertIsNone(utils.load_private_key(fake_file)) finally: os.unlink(fake_file)
def test_host_config(self): """Test per-host configuration functionality of ParallelSSHClient""" hosts = ['127.0.0.%01d' % n for n in xrange(1,3)] host_config = dict.fromkeys(hosts) servers = [] user = '******' password = '******' for host in hosts: _socket = make_socket(host) port = _socket.getsockname()[1] host_config[host] = {} host_config[host]['port'] = port host_config[host]['user'] = user host_config[host]['password'] = password server = start_server(_socket, fail_auth=hosts.index(host)) servers.append((server, port)) pkey_data = load_private_key(PKEY_FILENAME) host_config[hosts[0]]['private_key'] = pkey_data client = ParallelSSHClient(hosts, host_config=host_config) output = client.run_command(self.fake_cmd, stop_on_errors=False) client.join(output) for host in hosts: self.assertTrue(host in output) try: raise output[hosts[1]]['exception'] except AuthenticationException, ex: pass
def test_host_config(self): """Test per-host configuration functionality of ParallelSSHClient""" hosts = ['127.0.0.%01d' % n for n in xrange(1,3)] host_config = dict.fromkeys(hosts) servers = [] user = '******' password = '******' for host in hosts: server, port = start_server_from_ip(host, fail_auth=hosts.index(host)) host_config[host] = {} host_config[host]['port'] = port host_config[host]['user'] = user host_config[host]['password'] = password servers.append(server) pkey_data = load_private_key(PKEY_FILENAME) host_config[hosts[0]]['private_key'] = pkey_data client = ParallelSSHClient(hosts, host_config=host_config) output = client.run_command(self.fake_cmd, stop_on_errors=False) client.join(output) for host in hosts: self.assertTrue(host in output) try: raise output[hosts[1]]['exception'] except AuthenticationException as ex: pass else: raise AssertionError("Expected AutnenticationException on host %s", hosts[0]) self.assertFalse(output[hosts[1]]['exit_code'], msg="Execution failed on host %s" % (hosts[1],)) self.assertTrue(client.host_clients[hosts[0]].user == user, msg="Host config user override failed") self.assertTrue(client.host_clients[hosts[0]].password == password, msg="Host config password override failed") self.assertTrue(client.host_clients[hosts[0]].pkey == pkey_data, msg="Host config pkey override failed") for server in servers: server.kill()
import subprocess import ConfigParser import time from time import strftime, localtime from constants import IP_PATH, NODE_CONFIG HOME = os.environ['HOME'] # find all available hosts hosts = [] with open(IP_PATH, "r") as f: for line in f: hosts.append(line.rstrip()) # ssh commands pkey = load_private_key(HOME + '/.ssh/id_rsa_pis') client = ParallelSSHClient(hosts, user='******', pkey=pkey) # terminate the running program by soft terminate signal output = client.run_command('kill -15 "$(pgrep python)"') ssh_client_output(output) config = ConfigParser.ConfigParser() config.read(NODE_CONFIG) model, system = config.get('Node Config', 'model', 0), config.get('Node Config', 'system', 0) dir_name = '/' + model + '-' + system + '-' + strftime("%Y-%m-%d %H:%M:%S", localtime()) + '/' subprocess.Popen(['mkdir', HOME + '/stats' + dir_name])