会员登录
用户名:
密  码:
 海岸线公告
    为更好的服务广大用户,厦门海岸线网站在版面和功能上进行升级,欢迎各位朋友给我们提出你宝贵的意见!(可以将你的意见发送到chifengming@163.com或者加QQ:53938519)  
 国内推广
    没有资金的投入,很难做推广而且在国内,这些类型的品牌很多,那要看生产商能不能...  
网络实名
中国搜索联盟
百度竞价排名
新浪网站推广
搜狐网站推广
网易网站推广
 国外推广
    企业既然上网,就要充分利用互联网的特点与优势,采用互联网全新的宣传营销模式,不囿开传统媒介宣传...  
Google
Lycos
Yahoo国外推广
日本推广
 软件推广
    本站开发销售各类推广软件信息发布软件,网站推广软件,网站推广软件网络推广软件推广...  
短信群发软件
信息发布软件
网站推广软件
仓库管理软件
海岸信息



标题:用VB编写一个可以在ASP环境下调用的运行CMD命令的组件
有时我们在管理服务器时为了安全起见会禁用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"%>
==厦门海岸线网络==
==厦门软件开发==
==海岸线OA办公软件==
==厦门OA办公软件==
==海岸线进销存软件==
==厦门进销存软件==
==厦门iphone软件==
==龙岩软件开发==
==海岸线三维动画==
==厦门三维动画==
==中国古董网==
==中国树苗网==
==厦门化妆品网==
==中国冬笋批发网==
==棒球网==
首  页 | 关于我们 | 人才招聘 | 付款方式 | 联系我们 | 客户中心 | 网站地图
地址:厦门市软件园二期 E-mail:chifengming@163.com 邮编:361000
厦门海岸线网络科技有限公司 版权所有 CopyRight WWW.COAST-LINE.CN,All Right Reserved.
电话:0592-8962864 5839031 5839032 5839033 5839034 传真:0592-3237389 手机:13616028684 QQ1:53938519 QQ2:418143918