Esempio n. 1
0
def configure(update, context):
    new_server = context.user_data['new_server']
    ip = new_server['ip']
    rootpass = new_server['pass']

    #check if there's already API running on the server
    try:
        r = requests.get('http://{}'.format(ip)).json()
        if "Hi" in r['message']:
            update.message.reply_text(
                "Seems like setup is already done on this server. Now you should pick a server.",
                reply_markup=choose_server_markup)
            context.user_data['servers'].append(new_server)
            return CHOOSE_SERVER
    except RequestException:
        pass

    #check if auth is correct
    try:
        client = SSHClient(ip, user='******', password=rootpass)
        client.run_command('whoami', sudo=True)
        update.message.reply_text("Auth credentials ok.")
    except AuthenticationException:
        update.message.reply_text(
            "Auth credentials fail. Start-over with /start")
        return CONFIGURE

    update.message.reply_text(
        "Starting fresh server setup, it will take a few minutes...")
    command = "wget https://raw.githubusercontent.com/dathbezumniy/kmd-sync-api/master/sync_api_setup.sh " \
              "&& chmod u+x sync_api_setup.sh && ./sync_api_setup.sh"
    output = client.run_command(command, sudo=True)

    #wait until all dependencies downloaded/installed then check if API is up
    time.sleep(200)
    try:
        r = requests.get('http://{}'.format(ip)).json()
        if "Hi" in r['message']:
            update.message.reply_text(
                "Seems like setup is done and API is up. Now you should pick a server.",
                reply_markup=choose_server_markup)
            context.user_data['servers'].append(new_server)
            return CHOOSE_SERVER
    except RequestException:
        update.message.reply_text(
            "Something went wrong. API didn't start, you can try to start over the configuration with /start"
        )
        return CONFIGURE

    update.message.reply_text(
        "Something went wrong. API didn't start, you can try to start over the configuration with /start"
    )
    return CONFIGURE
Esempio n. 2
0
class SSHConnection:
    def __init__(self, host, port, username, password=None, pkey=None):
        from pssh.clients import SSHClient
        self.client = SSHClient(host=host, user=username, password=pass,
                                port=port, pkey=pkey)
        self.threads = []

    def exec(self, *args, **kwargs):
        print("Executing command: ", args)
        channel, host, stdout, stderr stdin = self.client.run_command(*args, **kwargs)
        from threading import Thread
        import sys
        stdout_thread = Thread(target=self._copy_output, args=(stdout, sys.stdout))
        stderr_thread = Thread(target=self._copy_output, args=(stderr, sys.stderr))
        stdout_thread.start()
        stderr_thread.start()
        self.threads.append(stdout_thread)
        self.threads.append(stderr_thread)

    def make_executable(self, path):
        ch, host, stdout, stderr, stdin = self.client.run_command(f"chmod +x {path}")
        self.client.close(ch)

    def exit(self):
        for t in self.threads:
            t.join()
        self.client.disconnect()

    def _copy_output(self, output, to):
        for line in output:
            print(line, file=to, flush=True, end="")


    def put(self, *args):
        self.client.scp_send(*args)

    def get(self, *args):
        self.client.scp_recv(*args)

    def _execute(self, command, *args, **kwargs):
        if not command.startswith("_"):
            targets = (self, self.client)
            for t in targets:
                if hasattr(t, command):
                    method = getattr(t, command)
                    if callable(method):
                        return method(*args, **kwargs)
        raise InvalidCommand(f"Invalid command - {command}")
          "&& chmod u+x prepare_dexp2p_node_ms.sh && ./prepare_dexp2p_node_ms.sh"

client = ParallelSSHClient(hosts, user="******")
output = client.run_command(command, sudo=True)

for node in output:
    for line in output[node]['stdout']:
        print(line)

# 2 - Preparing "started nodes" file on each server
i = 0
for host in hosts:
    print("Preparing file on node " + str(i + 1))
    non_parallel_client = SSHClient(host, user="******")
    if i == 0:
        non_parallel_client.run_command("touch ip_list")
    else:
        line_with_hosts = ""
        for host in hosts[:i]:
            line_with_hosts += host + "\n"
        non_parallel_client.run_command("echo -e " + line_with_hosts +
                                        " >> ip_list")
    i = i + 1
print("Test nodes software prepared. Starting network.")

# 3 - Starting network (need to do one by one)
i = 0
for host in hosts:
    print("Starting network on node " + str(i + 1))
    non_parallel_client = SSHClient(host, user="******")
    if i == 0:
Esempio n. 4
0
from pssh.clients import SSHClient

client = SSHClient(host="192.168.56.105", user="******", password="******")

ch, host, stdout, stderr, stdin = client.run_command("uptime")
for line in stdout:
    print(line)
Esempio n. 5
0
class SSH:
    """
    This class contains all methods to retieve information from the remote system
    """

    conn = False
    client = None

    def __init__(self, ip, user, password):
        self.HOST = ip
        self.USER = user
        self.PASSWD = password

        self.last_idle = 0.0
        self.last_total = 0.0

        print(utils.get_time() + "\tConnecting to " + ip)
        try:
            self.client = SSHClient(self.HOST,
                                    self.USER,
                                    self.PASSWD,
                                    timeout=1,
                                    num_retries=2)
            print(utils.get_time() + "\tSession started")
            self.conn = True
        except Exception:
            print(utils.get_time() + "\tSession not stablisehd. Closing")
            exit(-1)

    def finalize_conn(self):
        """
        Finalizes the SSH connection
        """
        self.client.session.disconnect()
        print(utils.get_time() + "\tClosing session")

    def get_GPU_temp(self):
        """
        Obtains the GPU temperature via the vcgencmd measure_temp command
        """
        host_out = self.client.run_command("vcgencmd measure_temp")
        for line in host_out.stdout:
            return line[5:9]

    def get_CPU_temp(self):
        """
        Obtains the CPU temperature
        """

        host_out = self.client.run_command(
            "cat /sys/class/thermal/thermal_zone0/temp")
        for line in host_out.stdout:
            return (int(line) / 1000).__round__(1)

    def get_CPU_percentage(self, line):
        """
        Calculates the usage of a CPU based on the /proc/stat content
        :param line: line from /proc/stat for the CORE
        :return: Usage percentage for the CORE
        """

        fields = [float(column) for column in line.strip().split()[1:]]
        idle, total = fields[3], sum(fields)
        idle_delta, total_delta = idle - self.last_idle, total - self.last_total
        self.last_idle = idle
        self.last_total = total
        usage = 100.0 * (1.0 - (idle_delta / total_delta))
        return usage.__round__(2)

    def get_CPU_use(self):
        """
        Obtains the CPU usage
        """
        output = []
        host_out = self.client.run_command("cat /proc/stat")

        contline = 0
        for line in host_out.stdout:
            if contline == 1:
                break

            output.append(self.get_CPU_percentage(line))

            contline += 1

        return output[0]

    def get_status(self):
        """
        Returns the status of the system, indicating if there are under voltage warnings or any other events
        The string is already reversed, so byte 0 in documentation is position 0 of the string (easier parsing)
        """
        host_out = self.client.run_command("vcgencmd get_throttled")

        for line in host_out.stdout:
            """
            Bit Hex value 	Meaning
            0 	1           Under-voltage detected
            1 	2 	        Arm frequency capped
            2 	4 	        Currently throttled
            3 	8 	        Soft temperature limit active
            16 	10000 	    Under-voltage has occurred
            17 	20000 	    Arm frequency capping has occurred
            18 	40000 	    Throttling has occurred
            19 	80000 	    Soft temperature limit has occurred
            """
            # Convert the hexadecimal value into binary (filling with 0 up to 20 positions if needed) and reverse it
            return bin(int(line[10:], 16))[2:].zfill(20)[::-1]

    def get_codecs(self):
        """
        Returns a list of the codecs and its status (enable for HW of disabled for SW processing)
        """
        output = []

        for codec in utils.CODECS:
            host_out = self.client.run_command("vcgencmd codec_enabled " +
                                               codec)

            for line in host_out.stdout:
                output.append(line)

        return output

    def get_kernel_info(self, arch=False):
        """
        Returns kernel name, release and processor architecture (optional)
        :param arch: flag to  get the architecture
        """
        if arch:
            command = "uname -smr"
        else:
            command = "uname -sr"

        host_out = self.client.run_command(command)

        for line in host_out.stdout:
            return line

    def get_hostname(self):
        """
        Returns hostname
        """
        host_out = self.client.run_command("hostname")

        for line in host_out.stdout:
            return line

    def get_RAM_usage(self):
        """
        Returns % of RAM usage
        """
        host_out = self.client.run_command("free -m")

        aux2 = []
        for line in host_out.stdout:
            if str.startswith(line, "Mem"):
                aux2 = line.split(" ")
                break

        aux = []
        for elem in aux2:
            if elem != "":
                aux.append(elem)
        # Used RAM is the sum of Used and Shared columns
        value = (int(aux[2]) + int(aux[4])) / int(aux[1])
        return str((value * 100).__round__(2)) + " %"

    def get_CPU_freq(self):
        """
        Returns current CPU frequency
        """
        host_out = self.client.run_command("vcgencmd measure_clock arm")

        for line in host_out.stdout:
            return str(int(int(line[14:]) / 1000000))

    def get_disk_usage(self):
        """
        Returns mounted disks usage (excluding tmpfs)
        """
        if utils.HUMAN_DISK_INFO:
            host_out = self.client.run_command("df -mh")
        else:
            host_out = self.client.run_command("df -m")

        aux2 = []
        cont = 0
        for line in host_out.stdout:
            if cont == 0:
                cont += 1
                continue
            if str.startswith(line, "tmpfs") or str.startswith(
                    line, "devtmpfs"):
                continue
            else:
                aux2.append(line)

        aux = []
        for elem in aux2:
            aux3 = elem.split(" ")
            for item in aux3:
                if item != "":
                    aux.append(item)
        output = "Mount\tTotal\tUsed\tFree\t% Used\n"
        for cont in range(0, len(aux) - 1, 6):
            output += str(aux[cont + 5]) + "\t" + str(aux[cont + 1]) + "\t" + str(aux[cont + 2]) + "\t" + \
                      str(aux[cont + 3]) + "\t" + str(aux[cont + 4]) + "\n"

        return output

    def get_avg_load(self):
        """
        Returns avg CPU load for the last 1, 5 and 15 minutes
        """
        host_out = self.client.run_command("cat /proc/loadavg")

        aux2 = []
        for line in host_out.stdout:
            aux2 = line.split(" ")

        aux = []
        for elem in aux2:
            if elem != "":
                aux.append(elem)

        return aux

    def get_uptime(self):
        """
        Returns the uptime in human readable format
        1 day, 5 hours, 5 minutes
        """
        host_out = self.client.run_command("uptime -p")
        for line in host_out.stdout:
            return line[3:]

    def get_governors(self):
        """
        Returns a list of the available governors as well as the current one
        """
        host_out = self.client.run_command(
            "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
        )
        out = []
        for line in host_out.stdout:
            out = line.split(" ")

        host_out = self.client.run_command(
            "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
        governor = ""
        for line in host_out.stdout:
            governor = line

        return out, governor

    def get_GPU_memory(self):
        """
        Returns memory reserved for the GPU
        """
        host_out = self.client.run_command("vcgencmd get_mem gpu")

        for line in host_out.stdout:
            return line.split("=")[1]
from pssh.utils import load_private_key
from pssh.clients import SSHClient
import yaml

with open("host_config.yml") as yaml_file:
    host_config = yaml.load(yaml_file, Loader=yaml.CLoader)

clients = []
for host, info in host_config.items():
    client = SSHClient(host=host,
                       port=info["port"],
                       user=info["user"],
                       password=info["password"])
    for command in info["commands"]:
        output = client.run_command(command)
        clients.append(output)

print(clients)
for channel, host, stdout, stderr, stdin in clients:
    for line in stdout:
        print(host, line)
Esempio n. 7
0
from pssh.clients import SSHClient

host = '192.168.56.101'
client = SSHClient(host, user="******", password="******")
channel, host, stdout, stderr, stdin = client.run_command('ls /usr/local/src')

for line in stdout:
    print(line)
from pssh.clients import SSHClient

client = SSHClient(host="192.168.56.105", user="******", password="******")

script = """
import sys
print(sys.version_info)
exit()
"""

ch, host, stdout, stderr, stdin = client.run_command("python", use_pty=True)

stdin.write(script)

for line in stderr:
    print(line)

for line in stdout:
    print(line)

stdin.close()