open a command prompt with javascript or vbscript

WMIF
WMIF used Ask the Experts™
on
this is for use internally so whatever security issues are not a problem.  i just need to know what to setup to make it work.  i would like to be able to click on a link, or button or something and have that bring up the same window that would come up if you went to start > run and type cmd.  i found a couple examples for javascript and vbscript, but they dont seem to be working properly.

vbscript:
<script language="VBScript">
function pingIt(ipAddress)
      set WshShell = CreateObject("WScript.Shell")
      WshShell.Run("cmd.exe /k ping " & ipAddress)
end function
</script>
trying to call with:
<div onclick="call pingIt('172.17.100.3')">ping</div>

i get an error saying "activex component cant create object: 'wscript.shell'"


javascript example:
<script type="text/javascript" language="javascript">
function pingItjs(ipAddress) {
      var oShell = new ActiveXObject("wscript.shell");
      oShell.Run("cmd.exe /k ping" + ipAddress);
      
}
</script>
calling it with:
<a href="javascript:pingItjs('172.17.100.3')">ping</a>

but this gives me an error: "automation server cant create object"


please fix my code or direct me to an example that will work for this.  this for only 2 client machines to use on an intranet so setting up security settings is not a problem.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2005
Commented:
The code

<script type="text/javascript" language="javascript">
function pingItjs(ipAddress) {
     var oShell = new ActiveXObject("wscript.shell");
     oShell.Run("cmd.exe /k ping " + ipAddress);
     
}
</script>

<a href="javascript:pingItjs('172.17.100.3')">ping</a>

works on my computer(Win XP/IE6), but my AntiVirus software starts yelling.

Do you have access to server side scripts? I think the best solution could be to post the ip address to the server and let the server ping the address and send the result back to the browser.

Batalf
Top Expert 2005

Commented:
Example with PHP which I'm most familiar with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
pre{
    margin-top:0px;
    margin-bottom:0px;
}
</style>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF'] ; ?>" method="Post">
<input type="text" name="ipAddress">
<input type="submit" value="Ping">
</form>

<?

if(isset($_POST['ipAddress'])){
    echo "<pre>";
    system("(ping ".$_POST['ipAddress'].")2>&1");    
    echo "</pre>";
}

?>
</body>
Top Expert 2005

Author

Commented:
ive read through the tutorials on using the command to ouput to a file on the server and then get back, but my other goal was to be able to bring this window up and ping again, or modify the command to give a bigger packet.  basically just a shortcut to open this window and let me and another guy move on from there.  once i got this working, i was gonna add another link there to allow us to click and open a telnet window.

i just remembered a group policy that is hiding the security page for ie browsers on my domain, so im gonna try dropping myself out of that group for now.
Top Expert 2005

Author

Commented:
well, just removed myself from that policy and changed the intranet zone to a low security setting and it finally brought up the window for the ping.  its not keeping the space in there though between the ping and the ip address.
Top Expert 2005

Commented:
I changed the function to preserve the spaces.

try changing it to

<script type="text/javascript" language="javascript">
function pingItjs(ipAddress) {
     var oShell = new ActiveXObject("wscript.shell");
     oShell.Run("cmd.exe /k ping " + ipAddress);
     
}
</script>
Top Expert 2005

Author

Commented:
ya i realized that just after i posted that above.  i had changed the vbscript function to have the space, and realized i hadnt changed the javascript function to have the space.  its working great now.  it brings up a box telling me that an acitve x script is trying to run and asking if i want to allow it.  it only shows once, and then i can click on the rest of them with no more prompts.  i also got my telnet working.

<script type="text/javascript" language="javascript">
function pingIt(ipAddress) {
      var oShell = new ActiveXObject("wscript.shell");
      oShell.Run("cmd.exe /k ping " + ipAddress);
}
function telnetIt(ipAddress) {
      var oShell = new ActiveXObject("wscript.shell");
      oShell.Run("cmd.exe /k telnet " + ipAddress);
}
</script>

<a href="javascript:telnetIt('172.17.100.3')">telnet</a> | <a href="javascript:pingIt('172.17.100.3')">ping</a>


you helped me greatly by testing this on your machine and showing that it works.  my machine was responding with script errors, not security errors.  thanks so much!
Top Expert 2005

Author

Commented:
for anyone following, i actually changed the code slightly now.  this was working perfectly, but it brings up the prompt in the c:\docs\user\etc folder.  kinda irritating, but livable.  also, once the cmd window is open it runs the command, but its not in the history of commands.  so you cant use the up arrow to run the command again.  you have to retype it or close and click the link again.  i found the function "sendkeys" which some say is unreliable, but its been working great for me so far.  now i call the cmd window with the title command so i can track the windows and the "cd \" to go to the root folder.  then i use the sendkeys to give it the ping or telnet command.  so now it remembers the history of that command so if i lose the connection from inactivity to the router i can just [up arrow] to the command and hit enter again.  this also allows me to put any other arguements on the ping command such as -t for continuous, or -l for packet size.

<script type="text/javascript" language="javascript">
function pingIt(ipAddress) {
      var oShell = new ActiveXObject("wscript.shell");
      oShell.Run('%comspec% /k "title ping ' + ipAddress + '&&cd \\"')
      delay(100)
      oShell.Sendkeys("ping " + ipAddress + "{ENTER}")
}
function telnetIt(ipAddress) {
      var oShell = new ActiveXObject("wscript.shell");
      oShell.Run('%comspec% /k "title telnet ' + ipAddress + '&&cd \\"')
      delay(100)
      oShell.Sendkeys("telnet " + ipAddress + "{ENTER}")
}
function delay(gap) { /* in millisecs */
      var then, now;
      then = new Date().getTime();
      now = then;
      while((now-then) < gap) {
            now = new Date().getTime();
      }
}
</script>

and calling it the same as above:
<a href="javascript:telnetIt('172.17.100.3')">telnet</a> | <a href="javascript:pingIt('172.17.100.3')">ping</a>

thanks again, Batalf

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial