{"id":335,"date":"2013-03-14T19:58:48","date_gmt":"2013-03-14T18:58:48","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=335"},"modified":"2013-06-27T08:39:50","modified_gmt":"2013-06-27T06:39:50","slug":"script-to-get-a-list-of-computers-connected-to-a-avocent-kvm-switch","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/script-to-get-a-list-of-computers-connected-to-a-avocent-kvm-switch\/","title":{"rendered":"Script to get a list of computers connected to a Avocent KVM switch"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_335 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_335')){$('.twoclick_social_bookmarks_post_335').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Script%20to%20get%20a%20list%20of%20computers%20connected%20to%20a%20Avocent%20KVM%20switch\",\"the_excerpt\":\"Hello%2C%0D%0A%0D%0Ain%20his%20office%2C%20a%20customer%20have%20a%20few%20Avocent%20KVM%20switches%20to%20control%20some%20client%20computers%20in%20a%20remote%20room.%20He%20ask%20me%20about%20the%20possibility%20to%20get%20a%20list%20of%20all%20computers%20connected%20to%20these%20boxes%2C%20because%20he%20do%20not%20want%20to%20maintain%20any%20list%20by%20hand.%0D%0A%0D%0AI%20research%20the%20documention%20but%20there%20is%20no%20%28scripting%29%20interface%20from%20which%20I%20could%20get%20such%20a%20list.%20SSH%20is%20only%20for%20connecting%20seri%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/script-to-get-a-list-of-computers-connected-to-a-avocent-kvm-switch\\\/\",\"post_id\":335,\"post_title_referrer_track\":\"Script+to+get+a+list+of+computers+connected+to+a+Avocent+KVM+switch\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hello,<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Because of the costs, DSView isn&#8217;t a option. The only way seems to be to extracting the list by reading the Webfrontend HTML output. Let us do this \ud83d\ude42<\/p>\n<p>I wrote a script in powershell, at least version 2 is needed to handle selfsigned SSL certificates, which do the following:<\/p>\n<ul>\n<li>Login to the Webfronted with https and SSL encryption by System.Net.HTTPWebRequest class to get the authentification cookie<\/li>\n<li>Get the Device HTML\u00a0 page by .NET class System.Net.Webclient and using Authentification cookie<\/li>\n<li>Save HTML do a temporary file<\/li>\n<li>Open the file with Internet Explorer<\/li>\n<li>Get the URL to start a KVM session, computername and portnumber by\u00a0 DOM<\/li>\n<\/ul>\n<h3>Script details<\/h3>\n<p>Define the User, Password, protocol and the devices:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# KVM User\r\n&#x5B;string]$sUser=&quot;Admin&quot;\r\n# KVM Password\r\n&#x5B;string]$sPassword=&quot;YourPassword&quot;\r\n# Protocol\r\n&#x5B;string]$sProtocol=&quot;https&quot;\r\n# Your Devices\r\n&#x5B;String&#x5B;]]$aAvocentDevices=@(&quot;Console1.domain.local&quot;,&quot;Console2.domain.local&quot;)\r\n<\/pre>\n<p>Built HTTP POST text and disable SSL certificate warnings<br \/>\n<!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;Byte&#x5B;]]$aBody=&#x5B;System.Text.Encoding]::Default.GetBytes(&quot;action=login&amp;loginUsername=&quot;+$sUser+&quot;&amp;loginPassword=&quot;+$sPassword+&quot;&amp;language=de&quot;)\r\n&#x5B;System.Net.CookieContainer]$oCookieContainer = New-Object System.Net.CookieContainer\r\n# Ignore SSL Certificate Warning, requires Powershell 2.0\r\n&#x5B;System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}\r\n<\/pre>\n<p>Built the login POST request by using System.Net.HTTPWebRequest class<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;string]$sLoginURL = $sProtocol+&quot;:\/\/$sConsole\/login.php&quot;\r\n&#x5B;System.Net.HTTPWebRequest] $oWebRequest = &#x5B;System.Net.WebRequest]::create($sLoginURL)\r\n$oWebRequest.AllowAutoRedirect = $false\r\n$oWebRequest.Method = &quot;POST&quot;\r\n$oWebRequest.Accept=&quot;application\/x-ms-application, image\/jpeg, application\/xaml+xml, image\/gif, image\/pjpeg, application\/x-ms-xbap, application\/vnd.ms-excel, application\/vnd.ms-powerpoint, application\/msword, *\/*&quot;\r\n$oWebRequest.Headers.Add(&quot;Accept-Encoding: gzip, deflate&quot;)\r\n$oWebRequest.Headers&#x5B;&quot;Cache-Control&quot;] = &quot;no-cache&quot;;\r\n$oWebRequest.UserAgent=&quot;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)&quot;\r\n$oWebRequest.ContentType = &quot;application\/x-www-form-urlencoded&quot;\r\n$oWebRequest.Timeout = (&#x5B;System.TimeSpan]::FromSeconds(50)).TotalMilliseconds\r\n$oWebRequest.CookieContainer = $oCookieContainer\r\n$oWebRequest.ContentLength=$aBody.Length\r\n$oWebRequest.Referer=$sProtocol+&quot;:\/\/$sConsole\/login.php&quot;\r\n$oWebRequest.CookieContainer.Add((New-Object System.Net.Cookie(&quot;\/home.php-t1s&quot;, &quot;1&quot;,&quot;&quot;,&quot;$sConsole&quot;)));\r\n<\/pre>\n<p>Getting the authentification cookie and set an avocent specific cookie<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Try to login\r\n$oReqStream = $oWebRequest.GetRequestStream()\r\n$oReqStream.Write($aBody, 0, $aBody.Length)\r\n&#x5B;System.Net.HTTPWebResponse] $oResponse = $oWebRequest.getResponse()\r\n$oResponse.Headers&#x5B;&quot;Set-Cookie&quot;]=&quot;\/home.php-t1s=1;&quot;+$oResponse.Headers&#x5B;&quot;Set-Cookie&quot;]\r\n<\/pre>\n<p>Get the device list as html by using the authentification cookie<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oWebClient = new-object system.net.webclient\r\n$oWebClient.Headers.add(&quot;Cookie&quot;, $oResponse.Headers&#x5B;&quot;Set-Cookie&quot;])\r\n$oWebClient.Headers.Add(&quot;Accept: application\/x-ms-application, image\/jpeg, application\/xaml+xml, image\/gif, image\/pjpeg, application\/x-ms-xbap, application\/vnd.ms-excel, application\/vnd.ms-powerpoint, application\/msword, *\/*&quot;)\r\n$oWebClient.Headers.Add(&quot;Accept-Encoding: text\/plain, text\/html&quot;)\r\n# Devicelist as html\r\n$sHTML=$oWebClient.DownloadString($sProtocol+&quot;:\/\/$sConsole\/target-table.php?id=1&amp;colcnt=4&quot;)\r\n$oReqStream.Flush()\r\n$oReqStream.Close()\r\n$oResponse.Close()\r\n\r\n<\/pre>\n<p>Save the HTML Output in a file and open it with Internet Explorer<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$sTempFile=&#x5B;System.IO.Path]::GetTempFileName()+&quot;.html&quot;\r\nSet-Content -Path $sTempFile $sHTML\r\n$oIE=new-object -Com internetexplorer.application\r\n$oIE.Visible=1\r\nwrite-host &quot;file:\/\/\/$sTempFile&quot;\r\n$oIE.Navigate2(&quot;file:\/\/\/$sTempFile&quot;)\r\nwhile($oIE.busy){\r\n    Start-Sleep 1\r\n    write-host -nonewline &quot;.&quot;\r\n}\r\n\r\n<\/pre>\n<p>Get all devices by DOM (Document object model)<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfor($iLoop=1;$iLoop -le 64;$iLoop++){\r\n    $oEntry=$oIE.Document.getElementByID(&quot;name$iLoop&quot;)\r\n    if($oEntry){\r\n        # Valid Entry\r\n        $sDeviceName=$oEntry.innerHTML\r\n        write-host &quot;DEVICE:  &quot; $sDeviceName\r\n        $oEntryLink=$oEntry.parentNode\r\n        $oRegEx=new-object System.Text.RegularExpressions.Regex(&quot;targetid=(\\d{1,2})$&quot;)\r\n        &#x5B;Int16]$iTargetID=0\r\n        if(&#x5B;System.Int16]::TryParse($oRegEx.Match($oEntryLink.href).Groups&#x5B;1].Value,&#x5B;ref]$iTargetID)){\r\n            if($iTargetID -gt 0){\r\n                $sURL=$sProtocol+&quot;:\/\/&quot;+$sConsole+&quot;\/session_launch.php?action=connect&amp;sessionType=kvmLaunch&amp;targetId=&quot;+$iTargetID.ToString()\r\n                Write-Host &quot;PORT:    &quot; $iTargetID.ToString()\r\n                write-host &quot;KVM URL: &quot; $sURL\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Finally quit IE and remove temporary files<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n\t$oIE.Quit()\r\n\tRemove-Item -Force -Path $sTempFile\r\n\r\n<\/pre>\n<p>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 \ud83d\ude42<\/p>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, 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 &hellip; <a href=\"https:\/\/michlstechblog.info\/blog\/script-to-get-a-list-of-computers-connected-to-a-avocent-kvm-switch\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Script to get a list of computers connected to a Avocent KVM switch<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[76,79,69,84,80,74,73,81,75,70,72,83,82,85,78,77],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-miscellaneous","tag-net","tag-authentification","tag-avocent","tag-certificate","tag-cookie","tag-devices","tag-get","tag-ignore","tag-kvm","tag-no","tag-script","tag-selfsigned","tag-ssl","tag-warning","tag-webclient","tag-webrequest"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":49,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":815,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions\/815"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}