0

Excel で VBA を使用して、Linux サーバーでスクリプトを呼び出したいと考えています。これを行うために、パスにある Putty のローカル コピーを使用しています。

パテセッションを作成してサーバーにログインするための単一のコマンドを正常に作成しましたが、これをシェルで開いた後に別のコマンドを渡す方法がわかりません。

何か案は?

以下のサブルーチンは機能しますが、2 番目のコマンドを実行しようとすると問題が発生します。

乾杯

Sub test()
Dim putty As String
Dim strCommand As String
Dim strCommand1 As String
Dim User As String
Dim Pass As String
Dim Host As String
Dim File As String
Dim RemotePath As String

putty = """" & Range("E3").Text & "\Putty.exe"""
User = Range("E8").Text
Pass = Range("E9").Text
Host = Range("E7").Text
'File = """" & Range("E11").Text & """"
RemotePath = Range("E10").Text

strCommand = putty & " -l " & User & " -pw " & Pass & _
    " " & File & " " & Host & ":" & RemotePath
'MsgBox strCommand
Shell strCommand, 1 ' vbNormalFocus '
Module2.WaitFor (5)
Shell "ls -l > shell.log", 1
End Sub
4

1 に答える 1

0

他のツールを使用してシェル コマンドplink を実行するには、次のコードでこのコマンドを実行します。

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Sub ShellAndWait(ByVal program_name As String, _
    ByVal window_style As VbAppWinStyle)
Dim process_id As Long
Dim process_handle As Long

    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, window_style)
    On Error GoTo 0

    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    If process_handle <> 0 Then
        WaitForSingleObject process_handle, INFINITE
        CloseHandle process_handle
    End If

    Exit Sub

ShellError:
    MsgBox Err.Description, vbOKOnly Or vbExclamation, "Error"
End Sub

ShellAndWait "plink -pw password -noagent user@server.com ""rm -f /tmp/file.txt ; gunzip /tmp/file.txt.gz""", vbMaximizedFocus
于 2013-04-07T09:47:12.337 に答える