Script to get a list of computers connected to a Avocent KVM switch


in his office, a customer have a few Avocent KVM switches to control some client computers in a remote room. He ask me about the possibility to get a list of all computers connected to these boxes, because he do not want to maintain any list by hand.

I research the documention but there is no (scripting) interface from which I could get such a list. SSH is only for connecting serial consoles, SNMP offers no OIDs for such a case.

Because of the costs, DSView isn’t a option. The only way seems to be to extracting the list by reading the Webfrontend HTML output. Let us do this 🙂

I wrote a script in powershell, at least version 2 is needed to handle selfsigned SSL certificates, which do the following:

  • Login to the Webfronted with https and SSL encryption by System.Net.HTTPWebRequest class to get the authentification cookie
  • Get the Device HTML  page by .NET class System.Net.Webclient and using Authentification cookie
  • Save HTML do a temporary file
  • Open the file with Internet Explorer
  • Get the URL to start a KVM session, computername and portnumber by  DOM

Script details

Define the User, Password, protocol and the devices:

# KVM User
# KVM Password
# Protocol
# Your Devices

Built HTTP POST text and disable SSL certificate warnings

[System.Net.CookieContainer]$oCookieContainer = New-Object System.Net.CookieContainer
# Ignore SSL Certificate Warning, requires Powershell 2.0
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Built the login POST request by using System.Net.HTTPWebRequest class

[string]$sLoginURL = $sProtocol+"://$sConsole/login.php"
[System.Net.HTTPWebRequest] $oWebRequest = [System.Net.WebRequest]::create($sLoginURL)
$oWebRequest.AllowAutoRedirect = $false
$oWebRequest.Method = "POST"
$oWebRequest.Accept="application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/, application/, application/msword, */*"
$oWebRequest.Headers.Add("Accept-Encoding: gzip, deflate")
$oWebRequest.Headers["Cache-Control"] = "no-cache";
$oWebRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MS-RTC LM 8; InfoPath.2)"
$oWebRequest.ContentType = "application/x-www-form-urlencoded"
$oWebRequest.Timeout = ([System.TimeSpan]::FromSeconds(50)).TotalMilliseconds
$oWebRequest.CookieContainer = $oCookieContainer
$oWebRequest.CookieContainer.Add((New-Object System.Net.Cookie("/home.php-t1s", "1","","$sConsole")));

Getting the authentification cookie and set an avocent specific cookie

# Try to login
$oReqStream = $oWebRequest.GetRequestStream()
$oReqStream.Write($aBody, 0, $aBody.Length)
[System.Net.HTTPWebResponse] $oResponse = $oWebRequest.getResponse()

Get the device list as html by using the authentification cookie

$oWebClient = new-object
$oWebClient.Headers.add("Cookie", $oResponse.Headers["Set-Cookie"])
$oWebClient.Headers.Add("Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/, application/, application/msword, */*")
$oWebClient.Headers.Add("Accept-Encoding: text/plain, text/html")
# Devicelist as html

Save the HTML Output in a file and open it with Internet Explorer

Set-Content -Path $sTempFile $sHTML
$oIE=new-object -Com internetexplorer.application
write-host "file:///$sTempFile"
    Start-Sleep 1
    write-host -nonewline "."

Get all devices by DOM (Document object model)

for($iLoop=1;$iLoop -le 64;$iLoop++){
        # Valid Entry
        write-host "DEVICE:  " $sDeviceName
        $oRegEx=new-object System.Text.RegularExpressions.Regex("targetid=(\d{1,2})$")
            if($iTargetID -gt 0){
                Write-Host "PORT:    " $iTargetID.ToString()
                write-host "KVM URL: " $sURL

Finally quit IE and remove temporary files

	Remove-Item -Force -Path $sTempFile

You can download the whole script by opening the post. It should be easy to generate a website with all your devices. Enjoy the script 🙂


Advertisment to support

List Targets which are connected to multiple Avocent KVM switches
4.4 KiB

One thought on “Script to get a list of computers connected to a Avocent KVM switch”

  1. I struggled a lot with powershell and fiddler to mimic the login behavior to these avocent devices and was not able to. I did not even thinked the this kind of script will be available on the internet. Thank you very much!!!

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.