HEX
Server: Apache
System: Linux pdx1-shared-a1-38 6.6.104-grsec-jammy+ #3 SMP Tue Sep 16 00:28:11 UTC 2025 x86_64
User: mmickelson (3396398)
PHP: 8.1.31
Disabled: NONE
Upload Files
File: //usr/local/bin/dhwp/dhwp/controllers/test.py
import os
import re
import sys
import socket
import requests
import time

from requests.packages.urllib3.exceptions import InsecureRequestWarning
from cement import App, Controller, ex
from cement import shell

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class Test(Controller):
    class Meta:
        label = 'test'
        description = 'test commands'
        stacked_on = 'base'
        stacked_type = 'nested'

    @ex(
        help='test a wordpress install',
        arguments=[
            (['-d', '--domain'],
             {'help': 'domain of wp install',
              'action': 'store',
              'dest': 'domain'}),
            (['-p', '--port'],
             {'help': 'port of wp install',
              'action': 'store',
              'dest': 'port'}),
            (['-i', '--ip'],
             {'help': 'ip address of wp install',
              'action': 'store',
              'dest': 'ip'}),
            (['-r', '--retries'],
             {'help': 'number of retries',
              'action': 'store',
              'default': 20,
              'dest': 'retries'}),
        ]
    )
    def site(self):
        domain = self.app.pargs.domain
        ip = self.app.pargs.ip
        retries = int(self.app.pargs.retries)
        if not domain:
            self.app.log.fatal("domain is required")
            sys.exit(1)

        self.app.log.info("Testing domain %s" % domain)
        uri = 'http://' + domain + '/wp-admin/'

        if ip:
            try:
                socket.inet_pton(socket.AF_INET, ip)
            except Exception:
                self.app.log.fatal("ip address is invalid")
                sys.exit(1)
            uri = 'http://' + ip + '/wp-admin/'
        else:
            try:
                socket.gethostbyname(domain)
            except Exception:
                self.app.log.fatal("Unable to resolve %s to an IP address.  Please provide an IP via --ip." % domain)
                sys.exit(1)

        for retry in range(retries):
            try:
                result = requests.get(uri, verify=False, allow_redirects=False, headers={'Host': domain}, timeout=5)
            except Exception:
                print("Connection refused")
                continue
            status = result.status_code

            if status == 200:
                self.app.log.info("HTTP code %s - Site test successful" % status)
                sys.exit(0)

            elif 300 <= status < 400:
                location = result.headers['Location']
                print("Redirected by HTTP code %s to " % status + location)
                if ip:
                    # If an ip is provided, we need to take the redirect and swap domain for ip again
                    newdomain = location.split("//")[-1].split("/")[0]
                    uri = location.replace(newdomain, ip, 1)
                    domain = newdomain
                else:
                    uri = location
                continue

            # On last attempt try /admin in case of plugin renaming /wp-admin
            if retry == (retries - 2):
                uri.replace('/wp-admin/', '/admin/', 1)

            print("HTTP code %s - sleeping 5 seconds" % status)
            time.sleep(5)

        # All retries failed - sad panda
        self.app.log.fatal("Site not in a functional state.")
        sys.exit(1)