{"id":51,"date":"2007-01-18T03:00:00","date_gmt":"2007-01-18T11:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/18\/automated-telnet\/"},"modified":"2007-01-18T03:00:00","modified_gmt":"2007-01-18T11:00:00","slug":"automated-telnet","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/18\/automated-telnet\/","title":{"rendered":"Automated Telnet"},"content":{"rendered":"<p>There was a recent post in the ScriptingAnswers.com forums about a Telnet COM object. I&#8217;m assuming the poster was looking for a way to automate a telnet session. Well there isnt&#8217; a free and easy way. What you need is some way to pass a string of commands to the Telnet session.&nbsp; Well, when all else fails, the SendKeys method is a last resort.<\/p>\n<p>I won&#8217;t belabor the point about Telnet security or the benefits of using SSH or something else. You do what you have to do. Even though my demo script uses the XP Telnet client, you could probably adapt it for any remote console client.<\/p>\n<p>Here&#8217;s the demo script in it&#8217;s entirety.&nbsp; I&#8217;ll go through a few key sections below:&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;TelnetDemo.vbs<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">Dim objShell<br \/>Dim objNetwork <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">Set objNetwork=CreateObject(&#8220;WScript.Network&#8221;) <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">strTitle=&#8221;Telnet Demo&#8221;<br \/>strDefaultServer=&#8221;Server01&#8243;<br \/>strDefaultUser=objNetwork.UserDomain &amp; &#8220;\\&#8221; &amp; objNetwork.UserName<br \/>strDefaultPassword=&#8221;P@ssw0rd&#8221; <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">strComputer=InputBox(&#8220;What server or device do you want to connect to?&#8221;,_<br \/>strTitle,strDefaultServer)<br \/>If Len(strComputer)=0 Then WScript.quit <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">strUsername=InputBox(&#8220;What credential do you want to use&#8221;,strTitle,_<br \/>strDefaultUser)<br \/>If Len (strUsername)=0 Then WScript.Quit <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">strPassword=InputBox(&#8220;What password do you want to use?&#8221;,strTitle,_<br \/>strDefaultPassword)<br \/>If Len (strPassword)=0 Then WScript.Quit <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">Set objShell=CreateObject(&#8220;wscript.shell&#8221;)<br \/>&#8216;Start Telnet<br \/>objShell.Run &#8220;Telnet &#8221; &amp; strComputer<br \/>&#8216;Give app a chance to get started<br \/>WScript.Sleep 5000<br \/>objShell.AppActivate &#8220;Telnet &#8221; &amp; strComputer <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;Send login credentials<br \/>objShell.SendKeys strUsername &amp; &#8220;~&#8221;<br \/>WScript.Sleep 2000<br \/>objShell.SendKeys strPassword &amp; &#8220;~&#8221;<br \/>WScript.Sleep 2000 <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;Send commands<br \/>objShell.SendKeys &#8220;net localgroup administrators&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>objShell.SendKeys &#8220;dir \/w&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>objShell.SendKeys &#8220;netstat&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>&#8216;give lengthy commands time to finish<br \/>WScript.Sleep 10000 <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;make sure we get window again<br \/>objShell.AppActivate &#8220;Telnet &#8221; &amp; strComputer<br \/>&#8216;run another command<br \/>objShell.SendKeys &#8220;net share&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221; <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;Close session<br \/>&#8216;make sure we get window again<br \/>objShell.AppActivate &#8220;Telnet &#8221; &amp; strComputer<br \/>objShell.SendKeys &#8220;exit&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<\/font><\/p>\n<p>If you can launch a Telnet session with integrated credentials you can simplify a few things. This script assumes you need to enter a username and password so it starts by prompting you for connection information:<\/p>\n<p><font face=\"Courier New\" size=\"2\">strComputer=InputBox(&#8220;What server or device do you want to connect to?&#8221;,_<br \/>strTitle,strDefaultServer)<br \/>If Len(strComputer)=0 Then WScript.quit <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">strUsername=InputBox(&#8220;What credential do you want to use&#8221;,strTitle,_<br \/>strDefaultUser)<br \/>If Len (strUsername)=0 Then WScript.Quit <\/font> <\/p>\n<p><font face=\"Courier New\" size=\"2\">strPassword=InputBox(&#8220;What password do you want to use?&#8221;,strTitle,_<br \/>strDefaultPassword)<br \/>If Len (strPassword)=0 Then WScript.Quit<\/font>  <\/p>\n<p>With this information we&#8217;re ready to launch Telnet with the Run method.<\/p>\n<p><font face=\"Courier New\" size=\"2\">objShell.Run &#8220;Telnet &#8221; &amp; strComputer<br \/><\/font><br \/>We need to give the command time to start so the script sleeps 5 seconds.<\/p>\n<p><font face=\"Courier New\" size=\"2\">WScript.Sleep 5000<\/font><\/p>\n<p>Then we need to grab the window. You have to know what is in the title bar of the window you want to activate. With Telnet, I know it is &#8220;Telnet computername&#8221;.<\/p>\n<p><font face=\"Courier New\" size=\"2\">objShell.AppActivate &#8220;Telnet &#8221; &amp; strComputer<\/font><\/p>\n<p>I can then send the logon credentials. The ~ character is the same as {Enter}. Notice I&#8217;m also sleeping for 2 seconds to give the console a chance to keep up. I&#8217;ve found that you need to go slow with this approach and give Telnet time to process the keys you are sending.<\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;Send login credentials<br \/>objShell.SendKeys strUsername &amp; &#8220;~&#8221;<br \/>WScript.Sleep 2000<br \/>objShell.SendKeys strPassword &amp; &#8220;~&#8221;<br \/>WScript.Sleep 2000<\/font><\/p>\n<p>At this point I should have an active Telnet session and can begin sending commands.&nbsp; Again, I&#8217;ve found it more effective to split up commands and pause between them.<\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;Send commands<br \/>objShell.SendKeys &#8220;net localgroup administrators&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>objShell.SendKeys &#8220;dir \/w&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>objShell.SendKeys &#8220;netstat&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<br \/>&#8216;give lengthy commands time to finish<br \/>WScript.Sleep 10000<\/font><\/p>\n<p>If there is a chance that something else could have grabbed focus, you&#8217;ll want to call AppActivate again. You definitely want to do this when you&#8217;re ready to end your session.<\/p>\n<p><font face=\"Courier New\" size=\"2\">&#8216;make sure we get window again<br \/>objShell.AppActivate &#8220;Telnet &#8221; &amp; strComputer<\/font><\/p>\n<p>At this point I can send the Exit command and the Telnet session will terminate. The Telnet window will dismiss itself in a few seconds. <\/p>\n<p><font face=\"Courier New\" size=\"2\">objShell.SendKeys &#8220;exit&#8221;<br \/>WScript.Sleep 200<br \/>objShell.SendKeys &#8220;~&#8221;<\/font><\/p>\n<p>A few final words on SendKeys.&nbsp; You always need to make sure the right window has focus, otherwise key strokes&nbsp; will go to the wrong place. There is no easy way to verify that the telnet connection was successful. So if you ran this script and attempted to connect to an unavailable server or have the wrong credentials, the rest of the script will keep going, trying to send key strokes to whatever window currently has focus. You can mitigate this problem by closing all other apps other than a CMD prompt where you&#8217;re running the script.<\/p>\n<p>Timing is everything with SendKeys, and especially with tools like Telnet. You&#8217;ll need to test in your environment to see how much time you need to pause between commands. Remember that wscript.sleep takes a value in milliseconds. A script like this may not be the speediest, but it can get the job done when there are no other alternatives.<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:11ed0d74-27ae-42d9-b0f8-86cd321335fc\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline;\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/VBScript\" rel=\"tag\">VBScript<\/a>, <a href=\"http:\/\/technorati.com\/tags\/SendKeys\" rel=\"tag\">SendKeys<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Telnet\" rel=\"tag\">Telnet<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There was a recent post in the ScriptingAnswers.com forums about a Telnet COM object. I&#8217;m assuming the poster was looking for a way to automate a telnet session. Well there isnt&#8217; a free and easy way. What you need is some way to pass a string of commands to the Telnet session.  Well, when all else fails, the SendKeys method is a last resort.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}