首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
ASP | ASP.NET | JSP | PHP | AJAX | XML | Java script | HTML/CSS | 服务器类
各大城市软件开发培训、软件人才免费咨询热线:400-700-5807
 您现在的位置: 中国IT实验室 >> WEB开发 >> asp学习教程 >> 正文
以在ASP环境下调用的运行CMD命令的VB组件
ChinaItLab  2004-9-1  保存本文    收藏本站


  有时我们在管理服务器时为了安全起见会禁用Windows Scripting Host,这样能防止某些不法用户利用WSH生成一个WebShell,对服务器造成很大的安全隐患。但如果我们又想禁用WSH,又想使用自己的WebShell用于服务器的管理怎么办呢?这里介绍了一种实现ASP中运行CMD并显示结果的组件编程。希望对大家能有所帮助。
  
  首先我们新建一个ActiveDLL工程,命名为ASPCMD,新建的类命名为CMDShell。在“Project“的“Referenct“中添加一个引用:Microsoft Active Server Pages Object Library。
  
  然后我们的思路是使用Window API ShellExecute调用cmd.exe,将运行的结果保存到一个临时文本文件,然后读出这个文件的内容显示出来。
  
  以下是工程ASPCMD的类CMDShell.cls的代码。
  Option Explicit
  Dim rp As Response
  Dim rq As Request
  Dim ap As Application
  Dim sr As Server
  Dim sn As Session
  Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  
  
  Private Sub ShellEx(ByVal sLocation As String, ByVal sPara As String, Optional MaxedForm As Boolean = False)
  On Error GoTo errhandle:
  Dim lR As Long
  Dim Style As Long
  Dim hWnd As Long
  If MaxedForm Then
  Style = vbMaximizedFocus
  Else
  Style = vbNormalFocus
  End If
  
  lR = ShellExecute(hWnd, "open", sLocation, sPara, "", Style)
  If (lR < 0) Or (lR > 32) Then
  'success
  Else
  rp.Write "Error Occered when starting the program " & sLocation
  End If
  errhandle:
  rp.Write "Error:" & Err.Description
  End Sub
  
  Public Sub OnStartPage(ByVal mysc As ScriptingContext)
  Set rp = mysc.Response
  Set rq = mysc.Request
  Set sr = mysc.Server
  Set ap = mysc.Application
  Set sn = mysc.Session
  End Sub
  
  Public Sub OnEndPage()
  Set rp = Nothing
  Set rq = Nothing
  Set sr = Nothing
  Set ap = Nothing
  Set sn = Nothing
  End Sub
  
  Private Function FileExists(Filename As String) As Boolean
  Dim i  As Integer
  On Error Resume Next
  i = Len(Dir$(Filename))
  If Err Or i = 0 Then FileExists = False Else FileExists = True
  End Function
  
  Private Function IsOpen(Filename As String) As Boolean
  Dim fFile As Integer
  Dim msg As String
  fFile = FreeFile()
  On Error GoTo ErrOpen
  Open Filename For Binary Lock Read Write As fFile
  Close fFile
  Exit Function
  ErrOpen:
  If Err.Number <> 70 Then
  msg = "Error # " & Str(Err.Number) & " was generated by " _
  & Err.Source & Chr(13) & Err.Description
  Else
  IsOpen = True
  End If
  End Function
  
  Public Sub Exec1(ByVal strCmd As String)
  On Error GoTo errhandle:
  Dim myTimer As Integer
  myTimer = 0
  
  Dim strOut As String
  Dim strFname As String
  //生成一个临时文件
  If Len(App.Path) = 3 Then
  strFname = App.Path & "lhtmp.txt"
  Else
  strFname = App.Path & "\lhtmp.txt"
  End If
  //如果在运行前文件已存在则删除之
  If FileExists(strFname) Then
  Kill strFname
  End If
  
  //运行行用户的CMD命令,并将结果输出到临时文件中
  //注意cmd.exe的/c参数是指运行完一个命令后马上结束会话状态。等同于在windows的run中输入的CMD命令。
  Dim strPara As String
  strPara = "/c " & strCmd & ">" & strFname
  ShellEx "cmd.exe", strPara
  //等待生成输出文件
  Do While Not FileExists(strFname)
  Sleep 1000
  DoEvents
  myTimer = myTimer + 1
  If myTimer = 15 Then
  Exit Do
  End If
  Loop
  myTimer = 0
  //等待文件输出完毕
  Do While IsOpen(strFname)
  Sleep 1000
  DoEvents
  myTimer = myTimer + 1
  If myTimer = 15 Then
  Exit Do
  End If
  Loop
  
  //显示输出文件的内容
  Open strFname For Input As #1
  Do While Not EOF(1)
  Line Input #1, strOut
  rp.Write strOut & vbCrLf
  Loop
  Close #1
  Sleep 1000
  //删除临时文件
  Kill strFname
  Exit Sub
  errhandle:
  rp.Write "error occured:" & Err.Description
  End Sub
  
  生成ASPCMD.dll,使用regsvr32 aspcmd.dll注册组件。
  
  以下是调用该DLL的一个ASP程序例子:
  
  <%@LANGUAGE="VBSCRIPT"%>
  <style type="text/css">
  <!--
  .singleborder {
  border: 1px solid;
  background-color: #000000;
  font-family: Arial, Helvetica, sans-serif;
  color: #FFFFFF;
  }
  .noborder {
  border: 1px none;
  background-color: #000000;
  font-family: Arial, Helvetica, sans-serif;
  color: #FFFFFF;
  }
  body{background-color: #000000;SCROLLBAR-FACE-COLOR: #333333; FONT-SIZE: 12px; SCROLLBAR-HIGHLIGHT-COLOR: #000000; SCROLLBAR-SHADOW-COLOR: #000000; SCROLLBAR-3DLIGHT-COLOR: #000000; SCROLLBAR-ARROW-COLOR: #000000; SCROLLBAR-TRACK-COLOR: #000000; SCROLLBAR-DARKSHADOW-COLOR: #000000
  font-family: Fixedsys;  font-size: 9pt}
  -->
  </style>
  <form action="" method="post">
  <input name="cmd" class="singleborder" value="<%=request.form("cmd")%>" size=102>
  <input type="submit" class="singleborder" value="EXECUTE">
  </form>
  <%
  if request.form("cmd")<>"" then
  set testme=server.createobject("aspcmd.cmdshell")
  %>
  <div class="noborder"><%=request.Form("cmd")%></div><br>
  <textarea cols="120" rows="30" class="noborder">
  <%=testme.exec1(request.form("cmd"))%></textarea>
  
  <% set testme=nothing
  end if
  %>
  
  以下是运行Ipconfig /all的结果:
  
  Windows 2000 IP Configuration
  
  Host Name . . . . . . . . . . . . : ibm-wrk-02
  Primary DNS Suffix . . . . . . . :
  Node Type . . . . . . . . . . . . : Broadcast
  IP Routing Enabled. . . . . . . . : No
  WINS Proxy Enabled. . . . . . . . : No
  
  Ethernet adapter 本地连接:
  
  Connection-specific DNS Suffix . :
  Description . . . . . . . . . . . : Intel(R) PRO/100 VM Network Connection
  Physical Address. . . . . . . . . : 00-08-02-BD-D7-EB
  DHCP Enabled. . . . . . . . . . . : No
  IP Address. . . . . . . . . . . . : 192.168.0.4
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.0.1
  DNS Servers . . . . . . . . . . . : 202.106.196.115
  
  
  
  
  
中国IT教育热线咨询
相关文章
使用AJAX技术构建更优秀的Web应用程序
ASP应用程序设计的Web状态管理分析
Ajax驱动的Web站点
如何使用Ajax开发Web应用程序
XMLHttpRequest和AJAX虎视Web应用开发
最新文章
·PHP正则表达式从url中取得域名
·php设计模式介绍之迭代器模式
·简单学习php遇到的主要问题
·asp根据表单自动生成sql语句的函
·雅虎选项卡特效
 文章评论

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
ASP.NET ASP PHP JSP
·extjs ComboBox联动下拉菜单示例08-01
·漫谈.Net开发关于命名空间和目录划分07-31
·在Silverlight应用程序中操作Cookie07-28
·带附加条件的NewID()用法(downmoon)07-28
·对自定义路由进行单元测试07-28
·javascript实现yield07-28
·在ASP.NET中使用Google Maps07-28
·Sql Server2005 实现Oracle10g的hash表分区功07-28
·asp.net get set用法07-26
·Asp.net 控件开发—数据回传07-26
·接口vs. 的实体类07-26
·php设计模式介绍之迭代器模式08-02
·简单学习php遇到的主要问题08-02
·asp根据表单自动生成sql语句的函数08-02
·教你优化你的ASP程序03-07
·asp去除HTML标记的三个实用函数03-07
·ASP添加验证码的解决方法03-07
·ASP通用文章分页函数:非记录集分页03-07
·ASP教程基础:十天学会ASP第三天03-07
·ASP教程基础:十天学会ASP第二天03-07
·ASP教程基础:十天学会ASP第一天03-07
·能够生成google xml地图的asp源码03-06
·Linux系统下让PHP提高性能的工具APC05-06
·一个完整、安全的PHP用户登录系统11-14
·Apache+PHP+MySQL建立数据库驱动的动态网站08-24
·用SSH与PHP相连接 确保数据传输的安全性08-23
·PHP5手动最简安装方法08-03
·PHP程序加速探索之服务器负载测试07-11
·完全讲解PHP+MySQL的分页显示示例分析05-30
·用Suhosin加强PHP脚本语言安全性05-26
·初学入门 PHP 和 MySQL05-17
·传奇的诞生 PHP三位创始人简介05-10
·大型系统上PHP令人不爽的九大原因05-10
·ASP.NET和PHP、JSP究竟学哪个?07-30
·JAVA (Jsp)利用Google的Translate开发API07-29
·由Servlet获得FacesContext及ManagedBeans07-24
·用JOTM向Servlet中添加事务07-18
·用servlet生成验证码07-16
·JSP/Servlet伪静态网页实现07-08
·JSP和Servlet的关系浅谈06-15
·妙用异步Servlet扩展AJAX应用程序06-11
·servlet生成验证码图片06-02
·java.servlet.Filter的应用05-30
·Java程序员必看--扩展鼠标右键菜单功能05-13
  培训中心
人才交流中心 技术交流中心
  ITLab技术交流平台: