www.pudn.com > source.rar > Form1.frm


VERSION 5.00 
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" 
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.1#0"; "RICHTX32.OCX" 
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX" 
Begin VB.Form frmMain  
   Caption         =   "在 WINDOWS 应用程序中远程执行 Unix 命令" 
   ClientHeight    =   7290 
   ClientLeft      =   1575 
   ClientTop       =   795 
   ClientWidth     =   9345 
   LinkTopic       =   "Form1" 
   MaxButton       =   0   'False 
   ScaleHeight     =   486 
   ScaleMode       =   3  'Pixel 
   ScaleWidth      =   623 
   Begin ComctlLib.StatusBar stb  
      Align           =   2  'Align Bottom 
      Height          =   375 
      Left            =   0 
      TabIndex        =   5 
      Top             =   6915 
      Width           =   9345 
      _ExtentX        =   16484 
      _ExtentY        =   661 
      SimpleText      =   "" 
      _Version        =   327680 
      BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7}  
         NumPanels       =   2 
         BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7}  
            Object.Width           =   5292 
            MinWidth        =   5292 
            TextSave        =   "" 
            Key             =   "state" 
            Object.Tag             =   "" 
         EndProperty 
         BeginProperty Panel2 {0713E89F-850A-101B-AFC0-4210102A8DA7}  
            Alignment       =   2 
            Object.Width           =   11377 
            MinWidth        =   11377 
            Text            =   "请输入执行后能返回命令提示符的Unix命令,因为上方仅是命令结果回显!" 
            TextSave        =   "请输入执行后能返回命令提示符的Unix命令,因为上方仅是命令结果回显!" 
            Key             =   "tip" 
            Object.Tag             =   "" 
         EndProperty 
      EndProperty 
      MouseIcon       =   "Form1.frx":0000 
   End 
   Begin VB.Frame Frame1  
      Caption         =   " Unix命令执行结果回显 (请在下方命令输入框中输入类似ls、pwd等执行完毕后能返回命令提示符的Unix命令) " 
      ForeColor       =   &H00FF0000& 
      Height          =   6255 
      Left            =   120 
      TabIndex        =   3 
      Top             =   0 
      Width           =   9135 
      Begin RichTextLib.RichTextBox rtb  
         Height          =   5895 
         Left            =   120 
         TabIndex        =   4 
         Top             =   240 
         Width           =   8895 
         _ExtentX        =   15690 
         _ExtentY        =   10398 
         _Version        =   327680 
         BackColor       =   -2147483641 
         ReadOnly        =   -1  'True 
         ScrollBars      =   3 
         TextRTF         =   $"Form1.frx":001C 
      End 
   End 
   Begin VB.Timer Timer1  
      Interval        =   1000 
      Left            =   6240 
      Top             =   6480 
   End 
   Begin VB.TextBox Text1  
      BeginProperty Font  
         Name            =   "宋体" 
         Size            =   9 
         Charset         =   134 
         Weight          =   700 
         Underline       =   0   'False 
         Italic          =   0   'False 
         Strikethrough   =   0   'False 
      EndProperty 
      ForeColor       =   &H00FF0000& 
      Height          =   270 
      Left            =   2160 
      TabIndex        =   1 
      Text            =   "Text1" 
      Top             =   6480 
      Width           =   3735 
   End 
   Begin MSWinsockLib.Winsock sck  
      Left            =   8760 
      Top             =   6480 
      _ExtentX        =   741 
      _ExtentY        =   741 
   End 
   Begin VB.CommandButton Command1  
      Caption         =   "远程执行Unix命令" 
      Height          =   375 
      Left            =   6720 
      TabIndex        =   0 
      Top             =   6420 
      Width           =   2175 
   End 
   Begin VB.Label Label1  
      Caption         =   "输入要执行的Unix命令" 
      ForeColor       =   &H00FF0000& 
      Height          =   255 
      Left            =   240 
      TabIndex        =   2 
      Top             =   6480 
      Width           =   1815 
   End 
   Begin VB.Menu mnuFile  
      Caption         =   "文件(&F)" 
      Begin VB.Menu mnuFileConnect  
         Caption         =   "连接(&C)" 
      End 
      Begin VB.Menu b  
         Caption         =   "-" 
      End 
      Begin VB.Menu mnuFileSet  
         Caption         =   "设置(&S)" 
      End 
      Begin VB.Menu as  
         Caption         =   "-" 
      End 
      Begin VB.Menu mnuFileExit  
         Caption         =   "退出(&X)" 
      End 
   End 
End 
Attribute VB_Name = "frmMain" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = True 
Attribute VB_Exposed = False 
Option Explicit 
Dim blnLoginSuccess As Boolean     '登录是否成功标志 
Dim intConnectTime As Integer    '登录时间 
'============================ 
'  远程执行Unix命令 
Private Sub Command1_Click() 
Dim strUnixCommand As String 
strUnixCommand = Trim(Text1.Text) 
If LCase(strUnixCommand) = "vi" Then 
    MsgBox "请输入执行完毕后能返回命令提示符的Unix命令,比如ls、pwd...等等!", 48 
    Text1.Text = "" 
    Text1.SetFocus 
    Exit Sub 
End If 
If Not RemoteExeUnixCommand(sck, strUnixCommand) Or StrComp(UCase(strUnixCommand), "EXIT", vbTextCompare) = 0 Then 
    blnLoginSuccess = False 
    Command1.Enabled = False 
    mnuFileConnect.Enabled = True 
    rtb.Text = "" 
    If sck.State <> sckClosed Then sck.Close 
    DoEvents 
    stb.Panels("state").Text = "" 
Else 
    stb.Panels("state").Text = "命令 " + Trim(Text1.Text) + " 执行完毕!" 
End If 
Text1.Text = "" 
End Sub 
'=========| 载入窗体 |======== 
Private Sub Form_Load() 
rtb.Text = "" 
Text1.Text = "" 
blnLoginSuccess = False 
Command1.Enabled = False 
If Not GetUnixInfo Then 
    MsgBox "你还没有设置UNIX主机的IP地址、登录用户名及密码!" + vbCrLf + _ 
           "请先在“文件”菜单中进行设置!", 48 
    Call InitUnixInfo 
    mnuFileConnect.Enabled = False 
End If 
Timer1.Enabled = False 
intConnectTime = 0 
stb.Panels("state").Text = "" 
End Sub 
 
 
'=========| 菜单--连接 |========= 
Private Sub mnuFileConnect_Click() 
Timer1.Enabled = True 
intConnectTime = 0 
blnLoginSuccess = False 
Command1.Enabled = False 
'------------- 
If sck.State <> sckClosed Then sck.Close 
DoEvents 
InitTelnetWinSock sck, strIP 
On Error GoTo ERR 
sck.Connect 
Me.MousePointer = 11 
stb.Panels("state").Text = "正在与 " + strIP + " 连接..." 
Do 
    DoEvents 
Loop Until sck.State = sckConnected Or intConnectTime > 6 
If sck.State <> sckConnected Then 
    MsgBox "与UNIX主机的连接失败!请检查网络或IP地址!", 16 
    stb.Panels("state").Text = "连接失败!" 
Else 
    mnuFileConnect.Enabled = False 
    Command1.Enabled = True 
    stb.Panels("state").Text = "连接成功!" 
End If 
Me.MousePointer = 0 
Timer1.Enabled = False 
Exit Sub 
ERR: 
    MsgBox "连接出错!请检查IP地址或网络!", 16 
End Sub 
'=========| 菜单--退出 |========= 
Private Sub mnuFileExit_Click() 
Unload Me 
End Sub 
'=========| 菜单--设置 |========= 
Private Sub mnuFileSet_Click() 
frmSet.Show 1, Me 
DoEvents 
If blnLoginSuccess Then 
    mnuFileConnect.Enabled = False 
Else 
    mnuFileConnect.Enabled = True 
End If 
End Sub 
 
 
 
'============================ 
'  WinSock数据到达 
Private Sub sck_DataArrival(ByVal bytesTotal As Long) 
Dim byteSckReceiveBuf() As Byte 
Dim lngLenBuf As Long 
Dim i As Integer 
Dim strTp As String 
      '协商过程中,得到主机返回的命令结果 
      '命令执行过程中,得到主机返回的执行结果 
sck.GetData byteSckReceiveBuf, vbByte + vbArray, bytesTotal 
lngLenBuf = bytesTotal 
strTp = "" 
For i = 0 To lngLenBuf - 1 
    strTp = strTp + Chr(byteSckReceiveBuf(i)) 
Next i 
rtb.Text = rtb.Text + vbCrLf + strTp 
 
rtb.SelLength = Len(rtb.Text) 
rtb.SelColor = &HFF00& 
rtb.SelStart = Len(rtb.Text) 
Text1.SetFocus 
If Not blnLoginSuccess Then  '一直和Unix主机协商,直到登录成功 
    blnLoginSuccess = RemoteLogin(sck, strUserName, strPassword, byteSckReceiveBuf) 
    If blnLoginSuccess Then 
        Command1.Enabled = True 
        MsgBox "OK!远程登录UNIX主机成功!", 48 
        stb.Panels("state").Text = "OK!远程登录UNIX主机成功!" 
    End If 
End If 
End Sub 
 
 
Private Sub Text1_KeyPress(KeyAscii As Integer) 
If KeyAscii = 13 Then Command1_Click 
End Sub 
 
Private Sub Timer1_Timer() 
intConnectTime = intConnectTime + 1 
End Sub