Пример #1
0
def premount_device(dev):
    """ dev example: /dev/sda1 """
    dev_info_dict = get_device_info_data(dev)
    premount_path = "/media/pre_" + dev_info_dict['label']
    if not os.path.exists(premount_path):
        module_printer("mount point not exists - create: " +
                       str(premount_path))
        cmd = "sudo mkdir -p " + str(premount_path)
        exitcode, stdout, stderr = LocalMachine.run_command(cmd)
        check_exitcode(cmd, exitcode, stderr)
    if not device_is_mounted(premount_path):
        module_printer("premount device " + str(dev_info_dict['path']))
        cmd = "sudo mount -t {} {} {}".format(dev_info_dict['filesystem'],
                                              dev_info_dict['path'],
                                              premount_path)
        exitcode, stdout, stderr = LocalMachine.run_command(cmd)
        if exitcode != 0:
            # retry without explicit filesystem parameter
            cmd = "sudo mount {} {}".format(dev_info_dict['path'],
                                            premount_path)
            exitcode, stdout, stderr = LocalMachine.run_command(cmd)
        check_exitcode(cmd, exitcode, stderr)
    else:
        module_printer("already mounted " + str(dev_info_dict['path']))
    return premount_path
Пример #2
0
def get_rpitools_services(color=Colors.CYAN):
    global rpitools_services, linux_services
    services = rpitools_services
    data = color + " RPITOOLS SERVICES:\n" + Colors.NC
    for service in services:
        is_active = LocalMachine.run_command("systemctl is-active " +
                                             str(service))[1]
        is_enabled = LocalMachine.run_command("systemctl is-enabled " +
                                              str(service))[1]
        is_active, is_enabled = service_state_coloring(isactive=is_active,
                                                       isenabled=is_enabled,
                                                       service_name=service)
        data += "\t" + color + str(
            service) + Colors.NC + " active status: " + str(is_active) + "\n"

        data += "\t" + str(service) + " enabled status: " + str(
            is_enabled) + "\n"
    services = linux_services
    data += color + " LINUX SERVICES:\n" + Colors.NC
    for service in services:
        is_active = LocalMachine.run_command("systemctl is-active " +
                                             str(service))[1]
        is_enabled = LocalMachine.run_command("systemctl is-enabled " +
                                              str(service))[1]
        is_active, is_enabled = service_state_coloring(isactive=is_active,
                                                       isenabled=is_enabled,
                                                       service_name=service)
        data += "\t" + color + str(
            service) + Colors.NC + " active status: " + str(is_active) + "\n"
        data += "\t" + str(service) + " enabled status: " + str(
            is_enabled) + "\n"
    return data
Пример #3
0
def hum_readable_list_devices(full_info=False):
    if full_info:
        cmd="lsblk"
        exit_code, stdout, stderr = LocalMachine.run_command(cmd)
        if exit_code == 0:
            print("[CMD] " + str(cmd))
            print(stdout)
            print("\n")
        cmd="blkid"
        exit_code, stdout, stderr = LocalMachine.run_command(cmd)
        if exit_code == 0:
            print("[CMD] " + str(cmd))
            print(stdout)
            print("\n")
        cmd="sudo fdisk -l | grep -v grep | grep /dev/sd"
        exit_code, stdout, stderr = LocalMachine.run_command(cmd)
        if exit_code == 0:
            print("[CMD] " + str(cmd))
            print(stdout)

    devices_list = BlockDeviceHandler.list_connected_devices()
    for device in devices_list:
        device_info = BlockDeviceHandler.get_device_info_data(device)
        for key, value in device_info.items():
            sep_len = 12 - len(key)
            sep = " "*sep_len
            print("\t{}:{}{}".format(key, sep, value))
Пример #4
0
def restore_diskconf_file(path, confname="diskconf.json"):
    json_path = str(path) + "/" + str(confname)
    save_path = "/tmp/" + str(confname)
    cmd = "sudo cp {} {}".format(save_path, json_path)
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr)
    cmd = "sudo rm -f {}".format(save_path)
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    BlockDeviceHandler.check_exitcode(cmd, exitcode, stderr)
Пример #5
0
def get_disk_usage():
    device_is_connected = LocalMachine.run_command_safe("echo /dev/sd*")
    if "/dev/sd*" not in device_is_connected:
        data_tmp = LocalMachine.run_command_safe("df -h / /dev/sd* | grep -v devtmpfs")
    else:
        data_tmp = LocalMachine.run_command_safe("df -h /")
    data_list = data_tmp.split("\n")
    data = ""
    for index, line in enumerate(data_list):
        data += " " + line + "\n"
    return data
Пример #6
0
def get_internal_ip():
    data = LocalMachine.run_command_safe("hostname -I")
    data_list = data.split(" ")
    ip_printout = ""
    for index, ip in enumerate(data_list):
        ip_printout += "IP[{}]: {} ".format(index, ip)
    return ip_printout
Пример #7
0
def get_logged_in_users():
    data_tmp = LocalMachine.run_command_safe("ps -ef | grep [s]shd | grep -v ^root")
    data_list = data_tmp.split("\n")
    data = ""
    for index, line in enumerate(data_list):
        data += " " + line + "\n"
    return data
Пример #8
0
def get_mem_usage():
    mem_total = LocalMachine.run_command_safe(
        "sudo cat /proc/meminfo | grep 'MemTotal' | tr -dc '0-9'")
    mem_available = LocalMachine.run_command_safe(
        "sudo cat /proc/meminfo | grep 'MemAvailable' | tr -dc '0-9'")
    mem_free = LocalMachine.run_command_safe(
        "sudo cat /proc/meminfo | grep 'MemFree' | tr -dc '0-9'")

    available_percent = (float(int(mem_total) - int(mem_available)) /
                         float(mem_total)) * 100
    available_percent = '%.1f' % available_percent

    free_percent = (float(int(mem_total) - int(mem_free)) /
                    float(mem_total)) * 100
    free_percent = '%.1f' % free_percent
    return float(available_percent), float(
        free_percent), mem_total, mem_available, mem_free
Пример #9
0
def device_is_mounted(device):
    """ device example: /dev/sda1 or mount point example: /media/device """
    cmd = "grep -qs '{} ' /proc/mounts".format(device)
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    if exitcode == 0:
        return True
    else:
        return False
Пример #10
0
def unmount_device(dev):
    """ device example: /dev/sda1 or mount point example: /media/device """
    cmd = "sudo umount " + str(dev)
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    check_exitcode(cmd, exitcode, stderr)
    time.sleep(1)
    if device_is_mounted(dev):
        time.sleep(1)
        unmount_device(dev)
Пример #11
0
def get_mac_addresses():
    output = ""
    devices = LocalMachine.run_command_safe("ls -1 /sys/class/net")
    devices_list = devices.split("\n")
    for index, device in enumerate(devices_list):
        if device != "lo":
            cmd = "ifconfig " + device + " | grep 'HWaddr'"
            mac_line = LocalMachine.run_command_safe(cmd, check_exitcode=False)
            substring_index = -17
            if mac_line == "":
                cmd = "ifconfig " + device + " | grep 'ether'"
                mac_line = LocalMachine.run_command_safe(cmd, check_exitcode=False)
                substring_index = 13
            if str(mac_line) != "" and mac_line is not None:
                output += " \t" + device + "\t\t" + str(mac_line)[substring_index:]
                if index < len(devices_list) - 1:
                    output += "\n"
    return output
Пример #12
0
def get_top_processes(char_width, topx=3):
    output = ""
    cmd = "ps aux | head -n 1; ps aux --sort -rss | sort -nrk 3,3 | head -n " + str(
        topx)
    data = LocalMachine.run_command_safe(cmd)
    data_list = data.split("\n")
    for line in data_list:
        output += " " + line[0:char_width] + "\n"
    return output
Пример #13
0
def mount_all_devices():
    media_dirs = []
    for dirname, dirnames, filenames in os.walk('/media/'):
        media_dirs = dirnames
        break

    for actualdir in media_dirs:
        is_mounted_cmd = "grep -qs '/media/{}' /proc/mounts".format(actualdir)
        exitcode, stdout, stderr = LocalMachine.run_command(is_mounted_cmd)
        if exitcode == 0:
            print("/media/{} is already mounted".format(actualdir))
        elif exitcode == 1:
            exitcode, stdout, stderr = LocalMachine.run_command(
                "cat /etc/fstab")
            if exitcode == 0 and actualdir not in stdout:
                print(actualdir + " not in /etc/fstab")
                print("\tSkipping mount...")
                return
            print("/media/{} mount".format(actualdir))
            mount_cmd = "mount /media/" + str(actualdir)
            exitcode, stdout, stderr = LocalMachine.run_command(mount_cmd)
            if exitcode == 0:
                is_mounted_cmd = "grep -qs '/media/{}' /proc/mounts".format(
                    actualdir)
                exitcode, stdout, stderr = LocalMachine.run_command(
                    is_mounted_cmd)
                if exitcode == 0:
                    print("Successfully mounted")
                elif exitcode == 1:
                    print("Fail to mount - may device is not found")
                else:
                    module_printer(
                        "EXITCODE: {}\nSTDOUT: {}\nSTDERR:{}".format(
                            exitcode, stdout, stderr))
            else:
                module_printer("Mount failed!")
                module_printer("Command: {} return with error code: {}".format(
                    mount_cmd, exitcode))
                module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr))
        else:
            module_printer("Unexpected error!")
            module_printer("Command: {} return with error code: {}".format(
                is_mounted_cmd, exitcode))
            module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr))
Пример #14
0
def get_rpienv_status(color=Colors.CYAN):
    rpienv_log = LocalMachine.run_command(
        "if [ -f /var/log/rpienv ]; then cat /var/log/rpienv; else echo ''; fi"
    )[1]
    print(rpienv_log)
    if "[ ENV ERROR ]" in rpienv_log:
        rpienv_status = 'warning'
    else:
        rpienv_status = 'ok'
    return process_state_coloring(rpienv_status, "rpienv_log")
Пример #15
0
def reset_dumped_database_and_restart_service():
    global dumped_dotdictbackup_json_path
    cmd = "rm -f " + str(dumped_dotdictbackup_json_path)
    exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True)
    if exit_code == 0:
        cmd = "sudo systemctl restart memDictCore"
        exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True)
        if exit_code == 0:
            cmd = "sudo systemctl is-active memDictCore"
            exit_code, stdout, stderr = LocalMachine.run_command(cmd, wait_for_done=True)
            if stdout == "active":
                print("Reset memDictCore databse was SUCCESSFUL")
            else:
                print("Reset memDictCore databse FAILED: " + str(stdout))
        else:
            print("Reset memDictCore databse FAILED: " + str(stderr))
    else:
        print("Reset memDictCore databse FAILED: " + str(stderr))
    return exit_code
Пример #16
0
def unmount_all_premounted_devices():
    """ serach under /media/ with pre_ prefix and unmount """
    premounted_devices_list = []
    cmd = "ls -1 /media/"
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    check_exitcode(cmd, exitcode, stderr)
    devices_list = stdout.split('\n')
    for dev in devices_list:
        dev = "/media/" + str(dev)
        if "pre_" in dev:
            premounted_devices_list.append(dev)
    module_printer("premounted devices: " + str(premounted_devices_list))
    for dev in premounted_devices_list:
        if device_is_mounted(dev):
            unmount_device(dev)
        else:
            module_printer("{} not mounted".format(dev))
        cmd = "rm -rf " + str(dev)
        exitcode, stdout, stderr = LocalMachine.run_command(cmd)
        check_exitcode(cmd, exitcode, stderr)
Пример #17
0
def format_ex4(device, label):
    """ format disk to ext4 linux file format """
    cmd = "sudo mkfs.ext4 {} -L {}".format(device, label)
    exit_code, stdout, stderr = LocalMachine.run_command(cmd)
    if exit_code == 0:
        module_printer("[CMD] " + str(cmd))
        module_printer(stdout)
        module_printer("\n")
    else:
        module_printer("EXITCODE:{}\nSTDOUT:{}\nSTDERR:{}\n".format(
            exit_code, stdout, stderr))
Пример #18
0
def set_get_device_name(device, name=None):
    if os.path.exists(device):
        if name is None:
            exitcode, stdout, stderr = LocalMachine.run_command(
                "sudo e2label " + str(device))  #device=/dev/sda1
            if exitcode == 0 and stderr == "" and stdout != "":
                print("Device {} name: {}".format(device, stdout))
                return stdout
            else:
                print("exitcode:{}\nstdout:{}\nstderr:{}".format(
                    exitcode, stdout, stderr))
        else:
            exitcode, stdout, stderr = LocalMachine.run_command(
                "sudo e2label " + str(device) + " " + str(name))
            if exitcode == 0 and stderr == "":
                set_get_device_name(device)
            else:
                print("exitcode:{}\nstdout:{}\nstderr:{}".format(
                    exitcode, stdout, stderr))
    else:
        error_msg("Device is not found " + str(device))
Пример #19
0
def unmount_all_devices(del_mount_point=False):
    """ unmount all mounted devices """
    mounted_devices_list = []
    cmd = "ls -1 /media/"
    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    check_exitcode(cmd, exitcode, stderr)
    devices_list = stdout.split('\n')
    for dev in devices_list:
        if device_is_mounted(dev):
            dev = "/media/" + str(dev)
            mounted_devices_list.append(dev)
    module_printer("mounted devices: " + str(mounted_devices_list))
    for dev in mounted_devices_list:
        if device_is_mounted(dev):
            unmount_device(dev)
        else:
            module_printer("{} not mounted".format(dev))
        if del_mount_point:
            cmd = "rm -rf " + str(dev)
            exitcode, stdout, stderr = LocalMachine.run_command(cmd)
            check_exitcode(cmd, exitcode, stderr)
Пример #20
0
def is_any_device_avaible():
    """ search under /dev with sd prefix for any available devices """
    cmd = "ls /dev/sd*"
    textmatrix = ""
    devices = []

    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    msg = "STDOUT: {} STDERR: {}".format(stdout, stderr)
    if exitcode == 0:
        return True, msg
    else:
        return False, msg
Пример #21
0
def edit_fstab_create_mount_point(device_info_dict, fstab_path="/etc/fstab"):
    global USER

    uuid = device_info_dict["uuid"]
    label = device_info_dict["label"]
    cmd = "UUID={} /media/{} auto defaults,auto,noatime,nofail,users,rw, 0 1".format(
        uuid, label)  # ext4 mount - done

    # check device is already added to the system, and make override protection
    enable = False
    uuid_is_exists = "cat {} | grep -v grep | grep '{}'".format(
        fstab_path, uuid + " ")
    print(uuid_is_exists)
    label_is_exists = "cat {} | grep -v grep | grep '{}'".format(
        fstab_path, label + " ")
    exitcode, stdout, stderr = LocalMachine.run_command(uuid_is_exists)
    if exitcode != 0 and stderr == "" and stdout == "":
        print("\tnew device unique id {}".format(uuid))
        exitcode, stdout, stderr = LocalMachine.run_command(label_is_exists)
        if exitcode != 0 and stderr == "" and stdout == "":
            print("\tnew device name {}".format(label))
            enable = True
        else:
            print("\tdevice name is already exists in fstab {} !!!".format(
                label))
    else:
        print("\tdevice unique id is already exists in fstab {} !!!".format(
            uuid))

    if enable:
        print("+++> ADDING NEW DEVICE IS ENABLE")
        check_cmd = "cat {} | grep -v grep | grep '{}'".format(fstab_path, cmd)
        exitcode, stdout, stderr = LocalMachine.run_command(check_cmd)
        if exitcode != 0 and stdout == "":
            print("line not exists: {}".format(cmd))
            print("ADD TO " + str(fstab_path))
            # edit fstab file
            add_cmd = "sudo echo '{}' >> {}".format(cmd, fstab_path)
            exitcode, stdout, stderr = LocalMachine.run_command(add_cmd)
            if exitcode == 0:
                print("{} added succesfully!".format(cmd))
            else:
                error_msg("Command: {} return with error code: {}".format(
                    cmd, exitcode))
                error_msg("STDOUT: {}\nSTDERR:{}".format(stdout, stderr))
        else:
            print("line exists: {}".format(cmd))

    # create mount point
    if not os.path.isdir("/media/" + str(label)):
        print("Create /media/" + str(label))
        os.makedirs("/media/" + str(label))
        print("chmod 0765 /media/" + str(label))
        os.chmod("/media/" + str(label), 0765)
        print("chmod " + str(USER))
        LocalMachine.run_command("chown " + USER + " /media/" + str(label))
    else:
        print("/media/" + str(label) + " already exists")
Пример #22
0
def map_wlan_devices():
    devices = []
    host_address_list = SearchDevices.online_device_scanner()
    for device in host_address_list:
        devip = device
        macaddr = "n/a"
        try:
            exitcode, stdout, stderr = LocalMachine.run_command('arp -n {}'.format(device), shell=True)
            if exitcode == 0:
                macaddr = stdout.split(' ')[3]
        except Exception:
            pass
        devices.append([devip, macaddr])
    return devices
Пример #23
0
def list_connected_devices():
    """ get block devices from /dev """
    cmd = "ls /dev/sd*"
    textmatrix = ""
    devices = []

    exitcode, stdout, stderr = LocalMachine.run_command(cmd)
    if exitcode != 0 and "No such file or directory" in stderr:
        module_printer("\tNO DEVICES WAS FOUND.")
        return []
    if exitcode == 0:
        textmatrix = LocalMachine.text_to_matrix_converter(stdout)
        for line in textmatrix:
            digit = line[0][
                -1]  # check if didgit is the last character ex. /dev/sda1
            if digit.isdigit():
                devices.append(line[0])
    else:
        module_printer("Command: {} return with error code: {}".format(
            cmd, exitcode))
        module_printer("STDOUT: {}\nSTDERR:{}".format(stdout, stderr))
        sys.exit(1)
    return devices
Пример #24
0
def get_other_monitored_processes(color=Colors.CYAN):
    process_name_list = ["Xorg", "vncserver", "kodi"]
    data = color + " MONITORED PROCESSES:\n" + Colors.NC
    for process in process_name_list:
        exitcode, process_state, stderr = LocalMachine.run_command(
            "ps aux | grep -v grep | grep '" + str(process) + "'")
        if process_state == "":
            process_state = "inactive"
        else:
            process_state = "active"
        process_state, is_enabled = service_state_coloring(
            isactive=process_state, service_name=process)
        data += "\t" + color + str(process) + Colors.NC + " state: " + str(
            process_state) + "\n"
    return data
Пример #25
0
def create_base_strucure(storage_root_path, path_list):
    cmd = "mkdir -p "
    for path in path_list:
        full_cmd = cmd + path
        if not os.path.exists(path) and not os.path.islink(path):
            console_out("CMD: " + str(full_cmd))
            exit_code, stdout, stderr = LocalMachine.run_command(full_cmd)
            if exit_code == 0:
                console_out("\tCREATE DIR DONE")
            else:
                console_out("\tCREATE DIR FAIL" + str(stderr))
        else:
            console_out("Already exists: " + str(path))

    for index, path in enumerate(path_list):
        if get_user_and_group(path)[1] != str(storage_folders_groups[index]):
            cmd_grp = "sudo chgrp " + str(
                storage_folders_groups[index]) + " " + str(path)
            console_out("CMD: " + str(cmd_grp))
            exit_code, stdout, stderr = LocalMachine.run_command(cmd_grp)
            if exit_code == 0:
                console_out("\tSET GROUP DONE")
            else:
                console_out("\tSET GROUP FAIL" + str(stderr))
Пример #26
0
def migrate_from_internal_to_external_storage(default_storage_root,
                                              external_storage_root,
                                              default_storage_folders):
    if not os.path.exists(default_storage_root):
        return False
    default_storage_folder_content = os.listdir(default_storage_root)
    linking_is_required = False
    for content in default_storage_folder_content:
        abs_path = default_storage_root + os.sep + content
        if os.path.isdir(abs_path) and not os.path.islink(abs_path):
            cmd = "mv " + str(abs_path) + " " + str(external_storage_root)
            console_out("Migrate storage from {} to {}".format(
                default_storage_root, external_storage_root))
            exit_code, stdout, stderr = LocalMachine.run_command(cmd)
            if exit_code != 0: console_out("\tFAIL")
            linking_is_required = True
    if linking_is_required:
        console_out("Create symlinks for backward compatibility")
        for folder in default_storage_folders:
            from_ = str(external_storage_root) + os.sep + str(folder)
            to_ = str(default_storage_root) + os.sep + str(folder)
            cmd = "ln -sf {} {}".format(from_, to_)
            exit_code, stdout, stderr = LocalMachine.run_command(cmd)
    return True
Пример #27
0
    def network_mapper_raw(self, debug_print=False):
        print("[ClusterMemberDiscover] [arp-scan] get raw local network data")
        exitcode, stdout, stderr = LocalMachine.run_command("sudo arp-scan -l")
        local_machines_info_list = stdout.split("\n")
        for local_machines_info in local_machines_info_list:
            values = local_machines_info.split("\t")
            if self.validate_ip(values[0]):
                try:
                    self.devices_on_network_raw[str(values[0]).strip()] = [str(values[1]).strip(),\
                                       str(values[2]).strip(), "hostname"]
                except:
                    pass

        if debug_print:
            for key, value in self.devices_on_network_raw.items():
                print("IP: {}\n\tDATA: {}".format(key, value))
Пример #28
0
def set_value_MemDict(key, value, namespace="system", field=None):
    global clientMemDict_path
    if field is not None:
        cmd = "{} -md -n {} -f {} -k {} -v {} -s True".format(
            clientMemDict_path, namespace, field, key, value)
    else:
        cmd = "{} -md -n {} -k {} -v {} -s True".format(
            clientMemDict_path, namespace, key, value)
    __debug_print(cmd)
    exitcode, stdout, stderr = LocalMachine.run_command(cmd,
                                                        wait_for_done=True)
    if exitcode == 0:
        state = stdout
    else:
        print("Get data from memdict failed: {}\n{}".format(cmd, stderr))
        state = None
    return state
Пример #29
0
def map_wlan_devices(service_port=9008):
    devices = []
    print("[NWSCN] Get online host list on port {}".format(service_port))
    host_address_list = SearchDevices.online_device_scanner(
        service_port=service_port)
    print("[NWSCN] Get macaddress by ip ...")
    for device in host_address_list:
        devip = device
        macaddr = "n/a"
        try:
            exitcode, stdout, stderr = LocalMachine.run_command(
                'arp -n {}'.format(device), shell=True, debug=False)
            if exitcode == 0:
                macaddr = stdout.split(' ')[3]
        except Exception:
            pass
        devices.append([devip, macaddr])
    return devices
Пример #30
0
    def device_mapper_inject_hostname_data(self, debug_print=True):
        print("[ClusterMemberDiscover] [nbtscan] get hostname by ip, create compact network data")
        for key, value in self.devices_on_network_raw.items():
            exitcode, stdout, stderr = LocalMachine.run_command("nbtscan " +  str(key))
            stdout_lines = stdout.split("\n")
            cnt = 0
            for index, line in enumerate(stdout_lines):
                line_worlds = re.split('\s+', line)
                if key in line_worlds[0]:
                    hostname = line_worlds[1]
                    self.devices_on_network[hostname] = {"ip": str(key), "mac": str(value[0]), "device": str(value[1])}
                else:
                    hostname = "UNKNOWN-" + str(cnt)
                    cnt+=1
                    self.devices_on_network[hostname] = {"ip": str(key), "mac": str(value[0]), "device": str(value[1])}

        if debug_print:
            for key, value in self.devices_on_network.items():
                print("{}\n\t{}".format(key, value))