Telnet Tool ActiveX控件应用实例代码


在网站建设过程中,我们可以应用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条命令,命令的结果显示在浏览器页面上。

  1. HTML  
  2. HEADTITLEPowerTCP Telnet Tool Demo Page/TITLE/HEAD  
  3. BODY  
  4. *******************************************************************  
  5. Program: PowerTCP Telnet Tool Demo  
  6. Author: Yin  
  7. Date: 07/16/2001  
  8. Purpose: To demonstrate how to login into a SCO UNIX server.  
  9. ********************************************************************}  
  10. Sub DoLogin()   
  11. Session(RESULT) =   
  12. On Error Resume Next   
  13. Create a Telnet control   
  14. Dim Telnet1   
  15. Set Telnet1 = Server.CreateObject(Dart.Telnet.1)   
  16. Use a 10 second timeout   
  17. Telnet1.Timeout = 10000   
  18. Specifies terminal type   
  19. Telnet1.TermType = ansi   
  20. AddResult Connecting to + txtHost + ... + vbCrLf   
  21. Connect to the host on port 23   
  22. Telnet1.Connect txtHost,23   
  23. Dim a string to be used by the search method   
  24. Dim Data   
  25. Data =   
  26. Search for login prompt   
  27. Telnet1.Search Data, txtLoginPrompt   
  28. AddResult Data   
  29. Send username   
  30. Telnet1.Send txtUser + vbCrLf   
  31. Search for password prompt   
  32. Telnet1.Search Data, txtPassPrompt   
  33. AddResult Data   
  34. Send password   
  35. Telnet1.Send txtPass + vbCrLf + vbCrLf   
  36. Search for command prompt   
  37. Telnet1.Search Data, txtCommandPrompt   
  38. AddResult Data   
  39. Send command   
  40. Telnet1.Send ps + vbCrLf   
  41. Search for command prompt   
  42. Telnet1.Search Data, txtCommandPrompt   
  43. AddResult Data   
  44. Send command   
  45. Telnet1.Send netstat -rn + vbCrLf   
  46. Search for command prompt   
  47. Telnet1.Search Data, txtCommandPrompt   
  48. AddResult Data   
  49. Close the connection   
  50. Telnet1.Close   
  51. If Err.number = 0 then   
  52. AddResult vbCrLf + Disconnecting... SUCCESS!   
  53. Else   
  54. Display info about the error   
  55. AddResult vbcrlf + ERROR # + CStr(Err.number) + : + Err.Description   
  56. End if   
  57. Response.Write Session(RESULT)   
  58. End Sub   
  59. Private Sub AddResult(s)   
  60. Session(RESULT) = Session(RESULT) + ReadyForHtml(s)   
  61. End Sub   
  62. Substitute escape codes for some of the HTML reserved characters   
  63. Function ReadyForHtml(Data)   
  64. On Error Resume Next   
  65. ReadyForHtml = Data   
  66. ReadyForHtml = Replace(ReadyForHtml, , gt;)   
  67. ReadyForHtml = Replace(ReadyForHtml, , lt;)   
  68. ReadyForHtml = Replace(ReadyForHtml, , nbsp;)   
  69. ReadyForHtml = Replace(ReadyForHtml, vbCrLf , brnbsp;)   
  70. ReadyForHtml = Replace(ReadyForHtml, vbCrLf, br)   
  71. End Function   
  72. Dim txtHost   
  73. Dim txtLoginPrompt   
  74. Dim txtUser   
  75. Dim txtPassPrompt   
  76. Dim txtPass   
  77. Dim txtCommandPrompt   
  78. txtHost = 192.0.1.6   
  79. txtLoginPrompt = login:   
  80. txtUser = root   
  81. txtPassPrompt = Password   
  82. txtPass = hp123   
  83. txtCommandPrompt = #   
  84. DoLogin() 

小结

通过使用Telnet Tool ActiveX控件,我们可以在ASP程序中登录到UNIX系统上,轻松地完成很多命令行操作。类似的,我们也可以实现通过浏览器查看CISCO路由器、交换机中的网络配置和运行状态。

企业的防火墙设置一般来说应该禁止从外部网直接远程登录到内部的UNIX主机上,但是我们可以通过在浏览器上简单的点击选择特定的操作,由WEB Server端的ASP程序登录到UNIX主机上完成指定操作,在方便管理的同时增加了系统的安全性。

当然,我们在ASP编程中应该随时考虑到安全性问题,尤其是要避免不慎造成的ASP源代码和UNIX口令的泄露。

相关内容

    暂无相关文章