当前位置:Linux教程 - Linux资讯 - Telnet到Sco unix执行并取得结果

Telnet到Sco unix执行并取得结果

前一段做了一个监控程序,触发事件后实现telnet到Sco unix执行命令并取得命令结果,想了几天终于完成,特和大家分享:

 

 

 

  一、安装TelnetTool控件(提供下载)

  二、VFP代码部分: 

       LOCAL telnet1        telnet1=CREATEOBJECT("Dart.Telnet.1")    Telnet1.Timeout = 100    Telnet1.TermType = "vt100"  False=.t.  True=.t.  vbCrLf=CHR(13)+CHR(10)    ** logfile = "Host" + HostIP + ".log"    ** Telnet1.Trace(logfile, False, True, vbCrLf + "---> ", vbCrLf + "<--- ")         thisform.AddLog(Tsys_kind,"正在连接主机 " + HostIP + " ..." + vbCrLf)             ok = Telnet1.Connect(HostIP, 23)     IF ok=""      thisform.AddLog(Tsys_kind,"银联前置["+HostIP+"]连接失败!同银联中心链路无法检查。")      thisform.showinfo("99","错误:银联前置连接失败!")       thisform.warn       thisform.command4.Picture="deng3.gif"       thisform.command4.refresh        telnet1.close        RETURN     ELSE       telnet1.close       Telnet1.Timeout = 10000       Telnet1.Connect(HostIP, 23)             ENDIF           thisform.AddLog(Tsys_kind,"已连接" + vbCrLf)

    Data = ""

    Telnet1.Search (@Data, "login:")

     * thisform.AddLog( Tsys_kind,Data)

    Telnet1.Send(User_name + vbCrLf)    Data = ""

    Telnet1.Search(@Data, "PassWord:")    *  thisform.AddLog(Tsys_kind,Data)

    Telnet1.Send (User_pwd + vbCrLf)    Data = ""

    Telnet1.Search(@Data, ">")   *   thisform.AddLog(Tsys_kind,Data)

    Telnet1.Send ("su" + vbCrLf)    Telnet1.Search (@Data, "Password:")    *  thisform.AddLog(Tsys_kind,data)    Telnet1.Send(root_pwd + vbCrLf)    Telnet1.Search(@Data, "#")    * thisform.AddLog(Tsys_kind,Data)

    Telnet1.Send(shell_cmd + vbCrLf)    Telnet1.Search(@Data, "#")   * thisform.AddLog(Tsys_kind,data)

      ***************取出的数据

  ok = VAL(STREXTRACT(data,"/","/",3))

   IF ok>WP04 && Warn Point

  thisform.showinfo("01","★警告:银联中心网络延时[9.234.12.21]!当前值:"+ALLTRIM(STR(ok,10,2))+"设定值:"+ALLTRIM(STR(WP04,10,2)))

  thisform.addlog(Tsys_kind,"★警告:银联中心网络延时[9.234.12.21]!当前值:"+ALLTRIM(STR(ok,10,2))+"设定值:"+ALLTRIM(STR(WP04,10,2)))

  ......&&触发事件

  ENDIF

(出处:http://www.sheup.com)