Call a Unix Script from Excel Vba

后端 未结 3 1596
面向向阳花
面向向阳花 2020-12-03 12:41

Im trying to call a set of Unix commands from VBA using Plink ( putty Command Line) but the commands are not getting Executed . I ll post the code any corrections or suggest

3条回答
  •  被撕碎了的回忆
    2020-12-03 13:19

    There's a problem with how you open and write to the file:

    fNum = FreeFile()
    Open vPath & "\Chg.txt" For Output As #1
    Print #1, "c:\"
    

    You're checking the next available file number, storing the number as a variable "fNum" and then opening a file as #1, regardless of what FreeFile() returned. You might have a file number conflict, as far as I can see. Also, on my end the "-s:" as command line argument fails. Try using a .cmd file instead, and call it as a command:

    Public Sub Chgaccper()
    
    Dim vPath As String
    Dim vFile As String
    Dim vSubpath As String
    Dim vscript As String
    Dim fNum As Long
    Dim oShell
    
    Set fso = CreateObject("scripting.filesystemobject")
    
    vPath = ThisWorkbook.Path
    
    'Mounting file command for ftp.exe
    fNum = FreeFile()
    Open vPath & "\Chg.cmd" For Output As fNum
    Print #fNum, "c:\"
    Print #fNum, "set PATH=" & vPath & ";%PATH% "
    Print #fNum, " "
    
    Print #fNum, "plink server Name -l uname -pw Password "
    Print #fNum, " "
    Print #fNum, "cd /root/home/temp "
    Print #fNum, " "
    Print #fNum, "chmod 666 *.csv "
    Print #fNum, " "
    Print #fNum, "cd /root/home/temp1 "
    Print #fNum, " "
    Print #fNum, "chmod 666 *.csv "
    Print #fNum, " "
    Print #fNum, "exit "
    Close #fNum
    
    vscript = "" & vPath & "\Chg.cmd"
    
    If fso.FolderExists("C:\Windows\System32") = False Then
        Shell "C:\WINNT\system32\cmd.exe /k " & vscript & ""
    Else
        Shell "C:\WINDOWS\system32\cmd.exe /k " & vscript & ""
    End If
    
    
    SetAttr vPath & "\Chg.cmd", vbNormal
    Kill vPath & "\Chg.cmd"
    
    End Sub
    

    Ref: https://msdn.microsoft.com/en-us/library/office/gg264526.aspx

提交回复
热议问题