class VM: """ Represents a VM. Keeps the credentials and tests data. Attributes: name(string): name of the VM (e.g. "alice") port(int): port on localhost which ssh is set up to username(string): username used for ssh password(string): password used for ssh installation_tests(list of VMTest): tests that check if proper packages are installed on the VM online_tests(list of VMTest): tests that emulate one of the exercises of the book manual_tests(list of strings): tests that are not performed, but just used as a reminder for the user """ def __init__(self, name, port, username, password): self.client = SSHClient(port, username, password) self.name = name def run_tests(self): print "Running installation tests for VM: {0}".format(self.name) for t in self.installation_tests: res = self.client.exec_command(t.cmd, t.timeout) print t.format_result(res) print "Running online tests for VM: {0}".format(self.name) for t in self.online_tests: res = self.client.exec_command(t.cmd, t.timeout) print t.format_result(res) print "-" * 80
def makeDirs (path): baseDir = BASE_UPLOAD_TO_DIR dirPaths = [] for dirPath in path.split("/"): actualDirPath = os.path.join(baseDir, dirPath) dirPaths.append(actualDirPath) baseDir = actualDirPath if dirPaths is not None and len(dirPaths) > 1: del dirPaths[len(dirPaths)-1] # Remove the file name client = SSHClient() client.load_system_host_keys() client.connect("bdholmes.com", username="******") for dirPath in dirPaths: stdin, stdout, stderr = client.exec_command("mkdir " + dirPath) error = False for std in [stdout, stderr]: if len(std.readlines()) > 0: error = True for line in std.readlines(): print line if not error: print "Created dir " + dirPath + "..."
def restartDjangoServer (): client = SSHClient() client.load_system_host_keys() client.connect("bdholmes.com", username="******") stdin, stdout, stderr = client.exec_command("touch " + UWSGI_LOCATION) if len(stdout.readlines()) > 0: for line in stdout.readlines(): print line else: print "Django server restarted!"
#!/usr/bin/env python import os import ssh from ssh import SSHClient host="10.107.105.201" user="******" client = SSHClient() client.load_system_host_keys() client.connect(host, username=user) stdin, stdout, stderr = client.exec_command('(cd /home/user1/simulations/decoder_optimised/decoder_node_1; python python_GNUparallel_ngspice.py --num=10 --ssh=/home/user1/simulations/decoder_optimised/decoder_node_1/sshmachines.txt --dir=/home/user1/simulations/decoder_optimised/decoder_node_1/decoder_node_1_line1 --spc=decoder_node_1_line1_sim)') print "stderr: ", stderr.readlines() print "pwd: ", stdout.readlines()