前些天写了一个页面控制台,主要是为了方便远程维护用的.由于系统以ASPNET用户运行控制台程序,所以权限仅为USERS组的权限.可以将此帐户添加到ADMINISTRATORS组中,从而可以得到超级权限.这个功能为远程维护提供了方便.运行效果如下:
页面(Console.aspx)代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Console.aspx.cs" Inherits="Console_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head runat="server">
<title>控制台</title>
<script language="javascript" type="text/javascript">
function $(ctlID)
{
return document.getElementById(ctlID);
}
function Trim(str)
{
var start=0,end=str.length-1;
while(start<str.length && str.charAt(start) == ' ')
start++;
while(end>=start && str.charAt(end) == ' ')
end--;
return str.substring(start,end + 1);
}
window.onload = function()
{
$('txtInput').focus();
}
function CheckInput()
{
if(Trim($('txtInput').value) == "cls")
{
$('txtInput').value = "";
$('txtOutput').value = "";
$('txtOutput').style.display = "none";
return;
}
$('txtInput').style.display = "none";
Console_Test.SolveCommand($('txtInput').value,SolveBack)
if(Trim($('txtOutput').value) == "")
{
$('txtOutput').style.display = "block";
}
$('txtInput').value = "";
function SolveBack(response)
{
var prompt = Trim(response.value.substring(0,response.value.indexOf('>')+1));
var index = prompt.length-1;
while(index>=0 && prompt.charAt(index) != '/r' && prompt.charAt(index) != '/n')
{
index--;
}
$('lblPrompt').innerText = prompt.substring(index+1);
$('txtOutput').value += response.value;
$('txtInput').style.display = "block";
divPanel.doScroll("bottom");
$('txtInput').focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server" ondblclick="if($('txtInput').style.display!='none')$('txtInput').focus();">
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="height: 41px; vertical-align: text-center; font-size: 23px">
控制台
</td>
</tr>
<tr>
<td>
<div id="divPanel" style="width: 99.8%; height: 420px; overflow: auto; border-right: #000000 1px solid;
border-top: #000000 1px solid; border-left: #000000 1px solid; border-bottom: #000000 1px solid;
text-align: center; background-color: Black">
<table style="width: 97%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align: left">
<textarea readonly="readonly" style="width: 100%; display: none; overflow-y: visible;
background-color: Black; color: White; border-top-style: none; border-right-style: none;
border-left-style: none; border-bottom-style: none;" id="txtOutput"></textarea>
</td>
</tr>
<tr>
<td style="text-align: left">
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 0%;">
<label id="lblPrompt" style="background-color: Black; color: White;">
C:/</label>
</td>
<td style="width: 100%;">
<input type="text" id="txtInput" style="width: 100%; background-color: Black; color: White;
border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none;"
onkeydown="if(event.keyCode == 13){CheckInput();return false;}" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
后台(Console.aspx.cs)代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Diagnostics;
public partial class Console_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Console_Test));
}
private Process CreateProcess()
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.Exited += new EventHandler(process_Exited);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
string machinePath = Server.MapPath("..");
if (machinePath.Substring(0, 1).ToUpper() != "C")
{
process.StandardInput.WriteLine(machinePath.Substring(0, 1).ToUpper() + ":");
}
process.StandardInput.WriteLine("CD " + machinePath);
Thread thread = new Thread(process_Watch);
thread.Start(process);
return process;
}
void process_Exited(object sender, EventArgs e)
{
Session["OSShellProcess"] = null;
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Session["OSShellProcessData"]
= (string)Session["OSShellProcessData"] + e.Data + "/r/n";
Session["OSShellProcessTime"] = DateTime.Now;
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Session["OSShellProcessData"]
= (string)Session["OSShellProcessData"] + e.Data + "/r/n";
Session["OSShellProcessTime"] = DateTime.Now;
}
void process_Watch(object e)
{
Process process = (Process)e;
if (process.WaitForExit(600000) == false)
{
process.Kill();
}
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public string SolveCommand(string commandLine)
{
Process process = (Process)Session["OSShellProcess"];
if (process == null)
{
process = CreateProcess();
Session["OSShellProcess"] = process;
}
process.StandardInput.WriteLine(commandLine);
Session["OSShellProcessTime"] = DateTime.Now;
for (int i = 0; i < 100; i++)
{
if (process.WaitForExit(200) == true || DateTime.Now > ((DateTime)Session["OSShellProcessTime"]).AddMilliseconds(160))
{
break;
}
}
string backData = (string)Session["OSShellProcessData"];
Session["OSShellProcessData"] = "";
return backData == null ? "" : backData;
}
}

页面(Console.aspx)代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Console.aspx.cs" Inherits="Console_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head runat="server">
<title>控制台</title>
<script language="javascript" type="text/javascript">
function $(ctlID)
{
return document.getElementById(ctlID);
}
function Trim(str)
{
var start=0,end=str.length-1;
while(start<str.length && str.charAt(start) == ' ')
start++;
while(end>=start && str.charAt(end) == ' ')
end--;
return str.substring(start,end + 1);
}
window.onload = function()
{
$('txtInput').focus();
}
function CheckInput()
{
if(Trim($('txtInput').value) == "cls")
{
$('txtInput').value = "";
$('txtOutput').value = "";
$('txtOutput').style.display = "none";
return;
}
$('txtInput').style.display = "none";
Console_Test.SolveCommand($('txtInput').value,SolveBack)
if(Trim($('txtOutput').value) == "")
{
$('txtOutput').style.display = "block";
}
$('txtInput').value = "";
function SolveBack(response)
{
var prompt = Trim(response.value.substring(0,response.value.indexOf('>')+1));
var index = prompt.length-1;
while(index>=0 && prompt.charAt(index) != '/r' && prompt.charAt(index) != '/n')
{
index--;
}
$('lblPrompt').innerText = prompt.substring(index+1);
$('txtOutput').value += response.value;
$('txtInput').style.display = "block";
divPanel.doScroll("bottom");
$('txtInput').focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server" ondblclick="if($('txtInput').style.display!='none')$('txtInput').focus();">
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="height: 41px; vertical-align: text-center; font-size: 23px">
控制台
</td>
</tr>
<tr>
<td>
<div id="divPanel" style="width: 99.8%; height: 420px; overflow: auto; border-right: #000000 1px solid;
border-top: #000000 1px solid; border-left: #000000 1px solid; border-bottom: #000000 1px solid;
text-align: center; background-color: Black">
<table style="width: 97%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="text-align: left">
<textarea readonly="readonly" style="width: 100%; display: none; overflow-y: visible;
background-color: Black; color: White; border-top-style: none; border-right-style: none;
border-left-style: none; border-bottom-style: none;" id="txtOutput"></textarea>
</td>
</tr>
<tr>
<td style="text-align: left">
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="width: 0%;">
<label id="lblPrompt" style="background-color: Black; color: White;">
C:/</label>
</td>
<td style="width: 100%;">
<input type="text" id="txtInput" style="width: 100%; background-color: Black; color: White;
border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none;"
onkeydown="if(event.keyCode == 13){CheckInput();return false;}" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
后台(Console.aspx.cs)代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Diagnostics;
public partial class Console_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Console_Test));
}
private Process CreateProcess()
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
process.Exited += new EventHandler(process_Exited);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
string machinePath = Server.MapPath("..");
if (machinePath.Substring(0, 1).ToUpper() != "C")
{
process.StandardInput.WriteLine(machinePath.Substring(0, 1).ToUpper() + ":");
}
process.StandardInput.WriteLine("CD " + machinePath);
Thread thread = new Thread(process_Watch);
thread.Start(process);
return process;
}
void process_Exited(object sender, EventArgs e)
{
Session["OSShellProcess"] = null;
}
void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Session["OSShellProcessData"]
= (string)Session["OSShellProcessData"] + e.Data + "/r/n";
Session["OSShellProcessTime"] = DateTime.Now;
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Session["OSShellProcessData"]
= (string)Session["OSShellProcessData"] + e.Data + "/r/n";
Session["OSShellProcessTime"] = DateTime.Now;
}
void process_Watch(object e)
{
Process process = (Process)e;
if (process.WaitForExit(600000) == false)
{
process.Kill();
}
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public string SolveCommand(string commandLine)
{
Process process = (Process)Session["OSShellProcess"];
if (process == null)
{
process = CreateProcess();
Session["OSShellProcess"] = process;
}
process.StandardInput.WriteLine(commandLine);
Session["OSShellProcessTime"] = DateTime.Now;
for (int i = 0; i < 100; i++)
{
if (process.WaitForExit(200) == true || DateTime.Now > ((DateTime)Session["OSShellProcessTime"]).AddMilliseconds(160))
{
break;
}
}
string backData = (string)Session["OSShellProcessData"];
Session["OSShellProcessData"] = "";
return backData == null ? "" : backData;
}
}