|
1 | 1 | import json
|
| 2 | +import socket |
| 3 | +import threading |
| 4 | +from concurrent.futures import ThreadPoolExecutor |
2 | 5 |
|
3 | 6 | import SoftLayer
|
| 7 | +from rich import box |
| 8 | +from rich import print as rprint |
| 9 | +from rich.console import Console |
| 10 | +from rich.table import Table |
4 | 11 |
|
5 | 12 | client = SoftLayer.create_client_from_env()
|
6 | 13 |
|
7 |
| -vm_id = 137682113 |
| 14 | +def get_vms(client): |
| 15 | + vms = client['Account'].getVirtualGuests() |
| 16 | + return [vm for vm in vms if vm.get('primaryIpAddress')] |
8 | 17 |
|
9 |
| -def print_keys(d, parent_key=''): |
10 |
| - for k, v in d.items(): |
11 |
| - key = parent_key + '.' + k if parent_key else k |
12 |
| - if isinstance(v, dict): |
13 |
| - print_keys(v, key) |
14 |
| - else: |
15 |
| - print(key) |
| 18 | +def get_vm_details(vm): |
| 19 | + # Get VM details |
| 20 | + vm_id = vm['id'] |
| 21 | + mask = "mask[id,globalIdentifier,fullyQualifiedDomainName,hostname,domain,createDate,modifyDate,provisionDate,notes,dedicatedAccountHostOnlyFlag,privateNetworkOnlyFlag,primaryBackendIpAddress,primaryIpAddress,networkComponents[id,status,speed,maxSpeed,name,macAddress,primaryIpAddress,port,primarySubnet,securityGroupBindings[securityGroup[id, name]]],lastKnownPowerState.name,powerState,status,maxCpu,maxMemory,datacenter,activeTransaction[id, transactionStatus[friendlyName,name]],lastOperatingSystemReload.id,blockDevices,blockDeviceTemplateGroup[id, name, globalIdentifier],postInstallScriptUri,operatingSystem[passwords[username,password],softwareLicense.softwareDescription[manufacturer,name,version,referenceCode]],softwareComponents[passwords[username,password,notes],softwareLicense[softwareDescription[manufacturer,name,version,referenceCode]]],hourlyBillingFlag,userData,billingItem[id,package,nextInvoiceTotalRecurringAmount,nextInvoiceChildren[description,categoryCode,recurringFee,nextInvoiceTotalRecurringAmount],children[description,categoryCode,nextInvoiceTotalRecurringAmount],orderItem[id,order.userRecord[username],preset.keyName]],tagReferences[id,tag[name,id]],networkVlans[id,vlanNumber,networkSpace],dedicatedHost.id,transientGuestFlag,lastTransaction[transactionGroup]]" |
| 22 | + return client['Virtual_Guest'].getObject(id=vm_id, mask=mask) |
16 | 23 |
|
17 |
| -vm_details = client['Virtual_Guest'].getObject(id=vm_id) |
| 24 | +def check_port(ip, port): |
| 25 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 26 | + result = sock.connect_ex((ip, port)) |
| 27 | + sock.close() |
| 28 | + return port if result == 0 else None |
18 | 29 |
|
19 |
| -print_keys(vm_details) |
| 30 | +def scan_hosts(hosts, port): |
| 31 | + results = [] |
| 32 | + with ThreadPoolExecutor() as executor: |
| 33 | + futures = [executor.submit(check_port, vm['primaryIpAddress'], port) |
| 34 | + for vm in hosts] |
| 35 | + for future in futures: |
| 36 | + port, vm = future.result() |
| 37 | + if port: |
| 38 | + results.append((port, vm)) |
| 39 | + return results |
| 40 | + |
| 41 | +def main(): |
| 42 | + |
| 43 | + windows_table = Table(show_header=True, header_style="white", box=box.ROUNDED) |
| 44 | + windows_table.add_column("Server ID") |
| 45 | + windows_table.add_column("Public IP") |
| 46 | + windows_table.add_column("Open Port") |
| 47 | + windows_table.add_column("Provision Date") |
| 48 | + windows_table.add_column("Provisioned By") |
| 49 | + |
| 50 | + linux_table = Table(show_header=True, header_style="blue", box=box.ROUNDED) |
| 51 | + linux_table.add_column("Server ID") |
| 52 | + linux_table.add_column("Public IP") |
| 53 | + linux_table.add_column("Open Port") |
| 54 | + linux_table.add_column("Provision Date") |
| 55 | + linux_table.add_column("Provisioned By") |
| 56 | + |
| 57 | + try: |
| 58 | + client = SoftLayer.create_client_from_env() |
| 59 | + |
| 60 | + vms = get_vms(client) |
| 61 | + |
| 62 | + win_vms = [] |
| 63 | + nix_vms = [] |
| 64 | + for vm in vms: |
| 65 | + details = get_vm_details(vm) |
| 66 | + if details['operatingSystem']['softwareLicense']['softwareDescription']['referenceCode'].startswith('WIN_'): |
| 67 | + win_vms.append(details) |
| 68 | + else: |
| 69 | + nix_vms.append(details) |
| 70 | + |
| 71 | + rprint("Checking ports on Windows hosts") |
| 72 | + win_ports = scan_hosts(win_vms, 3389) |
| 73 | + for port, vm in win_ports: |
| 74 | + windows_table.add_row(str(vm['id']), str(vm['primaryIpAddress']), |
| 75 | + str(port), str(vm['provisionDate']), |
| 76 | + str(vm['billingItem']['orderItem']['order']['userRecord']['username'])) |
| 77 | + |
| 78 | + rprint("Checking ports on Linux hosts") |
| 79 | + nix_ports = scan_hosts(nix_vms, 22) |
| 80 | + for port in nix_ports: |
| 81 | + vm = port['vm'] |
| 82 | + linux_table.add_row(str(vm['id']), str(vm['primaryIpAddress']), |
| 83 | + str(port), str(vm['provisionDate']), |
| 84 | + str(vm['billingItem']['orderItem']['order']['userRecord']['username'])) |
| 85 | + |
| 86 | + console = Console() |
| 87 | + console.print(windows_table) |
| 88 | + console.print(linux_table) |
| 89 | + |
| 90 | + except Exception as e: |
| 91 | + rprint(f"Error: {e}") |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments