在网站建设过程中,我们可以应用Telnet Tool ActiveX来进行一些编程。对于Telnet Tool ActiveX控件的功能我们在前一篇文章中已经介绍过了。那么下面我们就来对这方面进行一下应用举例。
编程实例
下面是一个简单的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()
小结
通过使用Telnet Tool ActiveX控件,我们可以在ASP程序中登录到UNIX系统上,轻松地完成很多命令行操作。类似的,我们也可以实现通过浏览器查看CISCO路由器、交换机中的网络配置和运行状态。
企业的防火墙设置一般来说应该禁止从外部网直接远程登录到内部的UNIX主机上,但是我们可以通过在浏览器上简单的点击选择特定的操作,由WEB Server端的ASP程序登录到UNIX主机上完成指定操作,在方便管理的同时增加了系统的安全性。
当然,我们在ASP编程中应该随时考虑到安全性问题,尤其是要避免不慎造成的ASP源代码和UNIX口令的泄露。