通过putty执行Linux命令

本文介绍了一个使用VBA编写的程序,该程序能够远程连接到指定服务器,检查网络连通性,并从远程服务器获取当前日期。通过Ping命令验证网络连接状态,成功后执行远程命令以获取日期信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Module Module1
    Dim m_RemoteServerAddr As String = "99.99.99.99"
    Sub Main()
        Try
            If Not My.Computer.Network.Ping(m_RemoteServerAddr, 1000) Then
                Console.WriteLine("ping failed")
                Return
            End If

            Dim Results As String = ""
            Dim RC As New RemoteControl(m_RemoteServerAddr)
            RC.RunRemoteCommand("echo $(date ""+%Y-%m-%d"")")
            Results = RC.m_Results.Substring(RC.m_Results.LastIndexOf("-%d")).Substring(7, 10)
            Console.WriteLine(Results)
        Catch ex As Exception

        End Try
    End Sub

End Module
/*****************************************************************************************/


Public Class RemoteControl
    Implements IDisposable

    Public RemoteIP As String = "99.99.99.99"
    Public RemoteUser As String = "root"
    Public conProcess As Process
    Public conInfo As System.Diagnostics.ProcessStartInfo
    Public m_Results As String = ""

    Public Sub New(Optional ByVal ServerIP As String = "99.99.99.99")
        RemoteIP = ServerIP
        conProcess = New Process
        conInfo = New System.Diagnostics.ProcessStartInfo()
        conInfo.FileName = "C:\Program Files\PuTTY\plink.exe"
        conInfo.Arguments = " -ssh" + " -l " + RemoteUser + " -pw " & "1234" + " " + RemoteIP
        conInfo.RedirectStandardInput = True
        conInfo.RedirectStandardOutput = True
        conInfo.UseShellExecute = False
        conInfo.WindowStyle = ProcessWindowStyle.Hidden
        conInfo.CreateNoWindow = True
        conInfo.WindowStyle = ProcessWindowStyle.Normal

        conProcess.StartInfo = conInfo

    End Sub

    Private disposedValue As Boolean
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                If Not IsNothing(conProcess) Then
                    conProcess.Close()
                End If
            End If

            Finalize()
        End If
        disposedValue = True
    End Sub


    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        ' TODO: uncomment the following line if Finalize() is overridden above.
        GC.SuppressFinalize(Me)
    End Sub

    Public Function RunRemoteCommand(ByVal cmd As String, Optional ByVal IsSkipResponse As Boolean = False) As Boolean
        Try
            conProcess.Start()
            Dim Writer As System.IO.StreamWriter = conProcess.StandardInput
            Writer.WriteLine(cmd)
            Writer.WriteLine("exit")
            Writer.Close()
            If Not IsSkipResponse Then
                Dim Reader As System.IO.StreamReader = conProcess.StandardOutput
                m_Results = Reader.ReadToEnd()
                Reader.Close()
            End If
            Threading.Thread.Sleep(1000)
            conProcess.Close()
            Return True
        Catch ex As Exception

        End Try
        Return False
    End Function

End Class
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值