如何在ASP程序中使用Telnet协议(下)
4 编程实例
下面是一个简单的Telnet Tool ActiveX控件的ASP编程实例。 实验环境是: Windows 2000 Professional、IIS 5.0、SCO OpenServer 5.0.2和Telnet Tool ActiveX控件。Telnet Tool ActiveX控件可以在www.dart.com下载30天试用版,只需要安装在WEB服务器上。
程序并不复杂,登录到一个SCO UNIX系统后,执行了2条命令,命令的结果显示在浏览器页面上。
HTML
HEADTITLEPowerTCP Telnet Tool Demo Page/TITLE/HEAD
BODY
*******************************************************************
Program: PowerTCP Telnet Tool Demo
Author: Yin
Date: 07/16/2001
Purpose: To demonstrate how to login into a SCO UNIX server.
********************************************************************}
以下是引用片段:
Sub DoLogin()
Session(RESULT) =
On Error Resume Next
Create a Telnet control
Dim Telnet1
Set Telnet1 = Server.CreateObject(Dart.Telnet.1)
Use a 10 second timeout
Telnet1.Timeout = 10000
Specifies terminal type
Telnet1.TermType = ansi
AddResult Connecting to + txtHost + ... + vbCrLf
Connect to the host on port 23
Telnet1.Connect txtHost,23
Dim a string to be used by the search method
Dim Data
Data =
Search for login prompt
Telnet1.Search Data, txtLoginPrompt
AddResult Data
Send username
Telnet1.Send txtUser + vbCrLf
Search for password prompt
Telnet1.Search Data, txtPassPrompt
AddResult Data
Send password
Telnet1.Send txtPass + vbCrLf + vbCrLf
Search for command prompt
Telnet1.Search Data, txtCommandPrompt
AddResult Data
Send command
Telnet1.Send ps + vbCrLf
Search for command prompt
Telnet1.Search Data, txtCommandPrompt
AddResult Data
Send command
Telnet1.Send netstat -rn + vbCrLf
Search for command prompt
Telnet1.Search Data, txtCommandPrompt
AddResult Data
Close the connection
Telnet1.Close
If Err.number = 0 then
AddResult vbCrLf + Disconnecting... SUCCESS!
Else
Display info about the error
AddResult vbcrlf + ERROR # + CStr(Err.number) + : + Err.Description
End if
Response.Write Session(RESULT)
End Sub
Private Sub AddResult(s)
Session(RESULT) = Session(RESULT) + ReadyForHtml(s)
End Sub
Substitute escape codes for some of the HTML reserved characters
Function ReadyForHtml(Data)
On Error Resume Next
ReadyForHtml = Data
ReadyForHtml = Replace(ReadyForHtml, , gt;)
ReadyForHtml = Replace(ReadyForHtml, , lt;)
ReadyForHtml = Replace(ReadyForHtml, , nbsp;)
ReadyForHtml = Replace(ReadyForHtml, vbCrLf , brnbsp;)
ReadyForHtml = Replace(ReadyForHtml, vbCrLf, br)
End Function
Dim txtHost
Dim txtLoginPrompt
Dim txtUser
Dim txtPassPrompt
Dim txtPass
Dim txtCommandPrompt
txtHost = 192.0.1.6
txtLoginPrompt = login:
txtUser = root
txtPassPrompt = Password
txtPass = hp123
txtCommandPrompt = #
DoLogin()
5 小结
通过使用Telnet Tool ActiveX控件,我们可以在ASP程序中登录到UNIX系统上,轻松地完成很多命令行操作。类似的,我们也可以实现通过浏览器查看CISCO路由器、交换机中的网络配置和运行状态。
企业的防火墙设置一般来说应该禁止从外部网直接远程登录到内部的UNIX主机上,但是我们可以通过在浏览器上简单的点击选择特定的操作,由WEB Server端
相关新闻>>
- 发表评论
-
- 最新评论 更多>>