I have an hta script (batch file), which shows me several buttons and when I click these I want a command to be executed in my cmd. For example, when I press the "IP address" button, the command "ipconfig" should be entered in the cmd and executed. What can I do? Or is that even possible or are there other options for executing a command via this hta script?
Kind regards, jcjms
Here is my code:
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="black">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,400);
function sleepFor( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now sleepDuration){ /* do nothing */ }
}
function callShellApplication(command){
var args=document.getElementById('args').value;
var r = new ActiveXObject("WScript.Shell");
var res=r.Exec(command " " args);
var out="";
while (res.Status == 0)
{
sleepFor(100);
}
while (!res.StdOut.AtEndOfStream){
out=out "\r\n" res.StdOut.ReadLine();
}
var StS=new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
StS.Write(out);
//new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());
window.close();
}
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
Aruments:<textarea name="args" style="display:none; cols="40" rows="1"></textarea>
<h1 style="color:white;font-size:40px;">Commands</h1>
<hr>
<button style="background-color:black; border-color:white; color:white" onclick='[what to do here? I want to shutdown other PCs in my local network over the window that opens in cmd (shutdown /iCODELOCK)]'>PC shutdown</button>
<button style="background-color:black; border-color:white; color:white" onclick='[what to do here? I want to get the PCs IP in cmd (ipconfig)]'>IP-Adress</button>
<button style="background-color:black; border-color:white; color:white" onclick="closeHTA(1);">close</button>
</BODY>
</HTML>
CodePudding user response:
In short, you need:
onclick="callShellApplication('ipconfig')"
A working example might look something like this:
<html>
<head>
<HTA:APPLICATION SCROLL="yes" SYSMENU="no" />
<title>HTA Buttons</title>
<script language="JScript">
//window.resizeTo(374,400);
function callShellApplication(command){
var shell = new ActiveXObject("WScript.Shell"),
exec = shell.Exec(command);
output.innerHTML = exec.StdOut.ReadAll().replace(/\r\n/g, '<br>');
}
</script>
<style>
*{color:white}
button{background-color:black; border-color:white}
</style>
</head>
<body bgcolor="black">
<h1 style="font-size:40px;">Commands</h1>
<hr>
<button onclick="callShellApplication('shutdown /s')">PC shutdown</button>
<button onclick="callShellApplication('ipconfig')">IP-Adress</button>
<button onclick="window.close()">close</button>
<hr>
<pre id="output"></pre>
</body>
</html>
CodePudding user response:
Beware those methods are supposed to be depreciating it is a html layout file with relaxed security, thus you must be careful how you craft your cmd, javascript, vb script or PowerShell commands.
The easiest method in that hybrid format is to use a vb method to generate and run buttons there is no problem mixing and matching
Use a simpler body structure like this but ensure your JavaScript works to pass parameters
<HTML>
<HEAD>
<TITLE>HTA Example</TITLE>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE="Run" NAME="Button" VALUE="Click Me">
<SCRIPT FOR="Button" EVENT="onClick" LANGUAGE="VBScript">
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /k echo off & echo/ & echo this could run IPconfig &IPconfig /? |more 38"
</SCRIPT>
</FORM>
</BODY>
</HTML>
For more about security and enhancements like SVG graphics you need to look at
in your head add for testing
<SCRIPT FOR="shutdown" EVENT="onClick" LANGUAGE="VBScript">
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /k echo change /k to /c and remove this comment including echo's SHUTDOWN /iCODELOCK"
</SCRIPT>
<SCRIPT FOR="ipconfig" EVENT="onClick" LANGUAGE="VBScript">
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /k IPconfig"
</SCRIPT>
change your buttons to
<button style="background-color:black; border-color:white; color:white" name="shutdown">PC shutdown</button>
<button style="background-color:black; border-color:white; color:white" name="ipconfig">IP-Adress</button>
<button style="background-color:black; border-color:white; color:white" onclick="closeHTA(1);">close</button>