“Python 100 project #38: Windows Network Troubleshoot

It’s never been easy to troublshoot end users PC problems. For me, I’m working in SI, and our main target is SMB. Most of them are not tech, but rather than just a user. So it’s often very difficult to extract the basic information from them.

I created simple information gathering tool for windows just to collect few network related information.

 

Output Example:

On 233508=====================
route print===================================================================================================
Interface List
  3...12 50 04 b4 6a 10 ......AWS PV Network Device #0
  1...........................Software Loopback Interface 1
  6...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter
  7...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0       172.31.1.1     172.31.1.232     25
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  169.254.169.250  255.255.255.255       172.31.1.1     172.31.1.232     50
  169.254.169.251  255.255.255.255       172.31.1.1     172.31.1.232     50
  169.254.169.254  255.255.255.255       172.31.1.1     172.31.1.232     50
       172.31.1.0    255.255.255.0         On-link      172.31.1.232    281
     172.31.1.232  255.255.255.255         On-link      172.31.1.232    281
     172.31.1.255  255.255.255.255         On-link      172.31.1.232    281
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link      172.31.1.232    281
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link      172.31.1.232    281
===========================================================================
Persistent Routes:
  Network Address          Netmask  Gateway Address  Metric
  169.254.169.254  255.255.255.255       172.31.1.1      25
  169.254.169.250  255.255.255.255       172.31.1.1      25
  169.254.169.251  255.255.255.255       172.31.1.1      25
===========================================================================

IPv6 Route Table
===========================================================================
Active Routes:
 If Metric Network Destination      Gateway
  7    331 ::/0                     On-link
  1    331 ::1/128                  On-link
  7    331 2001::/32                On-link
  7    331 2001:0:4137:9e76:428:53b:53e0:fe17/128
                                    On-link
  3    281 fe80::/64                On-link
  7    331 fe80::/64                On-link
  7    331 fe80::428:53b:53e0:fe17/128
                                    On-link
  3    281 fe80::39be:13ca:2a9b:a07/128
                                    On-link
  1    331 ff00::/8                 On-link
  3    281 ff00::/8                 On-link
  7    331 ff00::/8                 On-link
===========================================================================
Persistent Routes:
  None
END===============================

netsh winhttp show proxy========================
Current WinHTTP proxy settings:

    Direct access (no proxy server).

END===============================

ping 8.8.8.8========================
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time<1ms TTL=51
Reply from 8.8.8.8: bytes=32 time<1ms TTL=51
Reply from 8.8.8.8: bytes=32 time<1ms TTL=51
Reply from 8.8.8.8: bytes=32 time<1ms TTL=51

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
END===============================

tracert -d 8.8.8.8========================
Tracing route to 8.8.8.8 over a maximum of 30 hops

  1     *        *        *     Request timed out.
  2     *        *        *     Request timed out.
  3     *        *        *     Request timed out.
  4     *        *        *     Request timed out.
  5     *        *        *     Request timed out.
  6    <1 ms    <1 ms    <1 ms  100.65.8.97 
  7    16 ms     2 ms     2 ms  72.21.220.46 
  8    <1 ms    <1 ms    <1 ms  52.93.27.226 
  9    13 ms    18 ms    13 ms  52.93.26.53 
 10    <1 ms    <1 ms    <1 ms  52.93.27.156 
 11    <1 ms    <1 ms    <1 ms  52.95.219.141 
 12    <1 ms    <1 ms    <1 ms  108.170.246.65 
 13     1 ms     1 ms    <1 ms  209.85.255.45 
 14    <1 ms    <1 ms    <1 ms  8.8.8.8 

Trace complete.
END===============================

External_IP========================
18.205.162.XXX
END===============================

 

Here is the code:

from datetime import datetime
from os import popen


def cmd_execute(command):
    print(f"Executing '{command}'...")

    return_vals = popen(command).read()

    print(return_vals)

    if return_vals:
        return return_vals
    else:
        return None


def get_external_ip():
    print(f"Executing '{command}'...")
    from requests import get

    resp = get('https://api.ipify.org')
    if resp.status_code == 200:
        return resp.text
    else:
        return None

if __name__ == "__main__":

    commands = ["route print",
                "netsh winhttp show proxy",
                "ping 8.8.8.8",
                "tracert -d 8.8.8.8",
                ]

    filename = "tspackage_" + datetime.today().strftime('%Y%m%d') + ".log"

    with open(filename, "a") as f:
        f.write(f"\nOn {datetime.today().strftime('%H%M%S')}=====================")

    for command in commands:
        ret = cmd_execute(command)
        if ret:
            with open(filename, "a") as f:

                f.write(f"\n{command}========================")
                f.write(ret)
                f.write("END===============================\n")

    external_ip = get_external_ip()
    if external_ip:
        with open(filename, "a") as f:
            f.write(f"\nExternal_IP========================")
            f.write(external_ip)
            f.write("END===============================\n")