Пример #1
0
def execute_remote_command(command, hostname, username, password):
    client = None

    try:
        client = ssh_client.SshClient(host=hostname, port=22, username=username, password=password)

        ret = client.execute(command, sudo=True)

        print "  ".join(ret["out"]), "  E ".join(ret["err"]), ret["retval"]
    finally:
        if client:
            client.close()
Пример #2
0
def init_test_case():
    try:
        client = ssh_client.SshClient(hostname, username, password)
        cmd_ctrl = remote_cmd.CmdCtrl(cmd_sender=client.exec_cmd,
                                      multi_cmd_sender=client.exec_multi_cmd,
                                      file_uploader=client.upload,
                                      file_downloader=client.download)
        proto = protocol.Protocol()
        test_case = TestCase(cmd_ctrl, proto)
        return test_case, client

    except Exception as e:
        LOG.error(e)
        exit()
Пример #3
0
def reboot_device():
    return 0
    client = ssh_client.SshClient(hostname, username, password)
    client.exec_cmd("sudo reboot now")
    time.sleep(1)
    client.close()
Пример #4
0
import ssh_client

cl = ssh_client.SshClient('dmitrydev.adquant.net', '/root/test')
cl.connect()
Пример #5
0
def main():
    mail_notification = False

    c_wmi = wmi_class.WmiClass()
    ssh = ssh_client.SshClient()
    utils = util.Util()

    webservers_file_path = "webserver.txt"
    f_nam = "srv.txt"
    output_file_name = "another-name.html"
    hosts = []
    webserver_hosts = []

    config = load_config("config.json")

    # WMI user/pass
    wmi_user = utils.b64_decrypt(config.get("wmi").get("user"))
    wmi_pwd = utils.b64_decrypt(config.get("wmi").get("password"))

    ssh_user = utils.b64_decrypt(config.get("ssh").get("user"))
    ssh_pwd = utils.b64_decrypt(config.get("ssh").get("password"))
    ssh_port = config.get("ssh").get("port")
    ssh_payload = utils.b64_decrypt(config.get("ssh").get("payload"))
    print("[+] Payload length:: " + str(len(ssh_payload)))

    user = config.get("mail").get("smtp").get("user")

    serv = config.get("mail").get("smtp").get("server")
    port = config.get("mail").get("smtp").get("port")

    para = config.get("mail").get("to")
    subject = config.get("mail").get("subject")

    p = _parser.Parser()

    # with open("C:\\Users\\opensylar\\Desktop\\mem_parse.log", "r") as fp:
    #    l = fp.read()
    #    p.parse_mem(l)

    with open(webservers_file_path, "r") as f:
        ports = [80, 443]

        for c_addr in f:
            c_addr = c_addr.replace("\n", "")
            c_addr = c_addr.replace("\r", "")
            print("[+] Sending web tests to " + c_addr)
            status_web = check_web(c_addr, ports)
            webserver_hosts.append(status_web)

    with open(f_nam, "r") as f:
        for c_addr in f:
            c_addr = c_addr.replace("\n", "")
            c_addr = c_addr.replace("\r", "")

            if is_ip_range(c_addr) > 0x0:
                net1 = ip_network(c_addr, strict=False)

                for addr in net1:
                    addr = str(addr)
                    hosts.append({"hostname": addr})

            else:
                hosts.append({"hostname": c_addr})

    # Setea las variables del dict
    for h in hosts:
        print("\r\n[+] Checking " + h.get("hostname"))

        std_out, std_error = run_ping2(h.get("hostname"))
        ping_vals = parse_output_ping(std_out)

        h.update(ip_addr=h.get("hostname"))

        if ping_vals["ttl"] is not 0x0:
            html_path = "report-details/details-" + h.get("hostname") + ".html"
            h.update(status="up")

            # dns_nam = dns_resolver(h.get("hostname"))

            try:
                dns_nam = socket.gethostbyaddr(h.get("hostname"))
                dns_nam = dns_nam[0]

            except socket.herror:
                dns_nam = "Unknow"

            os_nam = os_detect(ping_vals["ttl"])
            # print("TTL:: " + str(ping_vals["ttl"]))

            h.update(dns_name=dns_nam)
            h.update(os=os_nam)
            h.update(html_path=html_path)

            s = socket_client.SocketClient()
            html_rpt = html_report.HtmlReport()
            html_rpt.set_path("report-details")

            if os_nam == "Windows":
                print("[+] Sending WMI query..")

                c_wmi.send_query(h.get("hostname"), wmi_user, wmi_pwd, 0x1)
                c_wmi.send_query(h.get("hostname"), wmi_user, wmi_pwd, 0x2)
                c_wmi.send_query(h.get("hostname"), wmi_user, wmi_pwd, 0x3)
                c_wmi.send_query(h.get("hostname"), wmi_user, wmi_pwd, 0x4)

                h.update(status_agent="1")
                html_rpt.build(h.get("hostname"), c_wmi, html_path, False)

            else:
                h.update(status_agent="1")
                print("[+] Sending SSH payload..")

                c_ssh = ssh.send_query(h.get("hostname"), ssh_port, ssh_user,
                                       ssh_pwd, ssh_payload)

                html_rpt.build(h.get("hostname"), c_ssh, html_path, True)

        # Offline
        else:
            print("[!] " + h.get("hostname") + " offline")
            h.update(status_agent="0")
            h.update(status="down")  # Dead

            try:
                dns_nam = socket.gethostbyaddr(h.get("hostname"))
                dns_nam = dns_nam[0]

            except socket.herror:
                dns_nam = "Unknow"

            except socket.gaierror:
                print("[!] " + h.get("hostname") + " is bad host")
                exit(-1)

            h.update(dns_name=dns_nam)
            h.update(os="Unknow")

    createhtml(output_file_name, hosts, webserver_hosts)

    c_path = os.getcwd()
    file_output = c_path + "\\reports.zip"
    build_zip(c_path, file_output, output_file_name)

    if mail_notification is True:
        print("[+] Sending mail to SMTP relay server")

        u = {"sender": user}
        s = {"serv": serv, "port": port}
        m = {
            "to": para,
            "subject": subject,
            "attach": file_output,
            "body": "Report"
        }
        send_mail(m, u, s)

    print("[+] Finish!")