1

同样由于某种原因,它昨天还在工作,但今天却不工作。我认为它不是代码,而是一些外部问题。

无论如何

    Dim virtualFolder As String = "~/Scripts/"
    Dim physicalFolder As String = Server.MapPath(virtualFolder)
    Dim unixLogin As String = (USERNAME & "@" & COMPUTERNAME & ":" & UNIXSCRIPTNAME)

    ' Send file to Unix server via pscp
    Dim Proc As New System.Diagnostics.Process
    Proc.StartInfo = New ProcessStartInfo("C:\Windows\System32\cmd.exe")
    'MsgBox("/C C:\pscp.exe -pw " & PASSWORD & " " & physicalFolder & "\" & UNIXSCRIPTNAME & " " & unixLogin)
    Proc.StartInfo.Arguments = "C:\pscp.exe -pw " & PASSWORD & " " & physicalFolder & "\" & UNIXSCRIPTNAME & " " & USERNAME & "@" & COMPUTERNAME & ":" & UNIXSCRIPTNAME
    Proc.StartInfo.RedirectStandardInput = True
    Proc.StartInfo.RedirectStandardOutput = False
    Proc.StartInfo.UseShellExecute = False
    'Proc.StartInfo.CreateNoWindow = True
    Proc.Start()
    ' Allows script to execute sequentially instead of simultaneously
    Proc.WaitForExit()

    ' Make file executable
    Proc.StartInfo = New ProcessStartInfo("C:\plink.exe")
    'MsgBox("-ssh -pw " & PASSWORD & " " & USERNAME & "@" & COMPUTERNAME & " chmod u+x ./" & UNIXSCRIPTNAME)
    Proc.StartInfo.Arguments = "-ssh -pw " & PASSWORD & " " & USERNAME & "@" & COMPUTERNAME & " chmod u+x ./" & UNIXSCRIPTNAME
    Proc.StartInfo.RedirectStandardInput = True
    Proc.StartInfo.RedirectStandardOutput = False
    Proc.StartInfo.UseShellExecute = False
    ' Proc.StartInfo.CreateNoWindow = True
    Proc.Start()
    Proc.WaitForExit()

    ' Execute File
    Proc.StartInfo = New ProcessStartInfo("C:\plink.exe")
    Proc.StartInfo.Arguments = "-ssh -pw " & PASSWORD & " " & USERNAME & "@" & COMPUTERNAME & " ./" & UNIXSCRIPTNAME
    Proc.StartInfo.RedirectStandardInput = True
    Proc.StartInfo.RedirectStandardOutput = False
    Proc.StartInfo.UseShellExecute = False
    'Proc.StartInfo.CreateNoWindow = True
    Proc.Start()
    Proc.WaitForExit()

    ' Remove File
    Proc.StartInfo = New ProcessStartInfo("C:\plink.exe")
    Proc.StartInfo.Arguments = "-ssh -pw " & PASSWORD & " " & USERNAME & "@" & COMPUTERNAME & " rm ./" & UNIXSCRIPTNAME
    Proc.StartInfo.RedirectStandardInput = True
    Proc.StartInfo.RedirectStandardOutput = False
    Proc.StartInfo.UseShellExecute = False
    'Proc.StartInfo.CreateNoWindow = True
    Proc.Start()
    Proc.WaitForExit()

这个可以缩短吗?我是... 1) 将文件发送到 unix 系统 2) 使其可执行 3) 运行文件(它的脚本) 4) 之后删除它

4

1 回答 1

2

您可能想看看这是否适合您。我没有 Unix 系统来检查它,但它似乎正确地填充了进程。出于测试目的,我还为您的变量设置了默认值。这消除了很多冗余并缩短了代码量。

Public Class Form1
    Dim USERNAME As String = "USERNAME"
    Dim COMPUTERNAME As String = "COMPUTERNAME"
    Dim UNIXSCRIPTNAME As String = "UNIXSCRIPTNAME"
    Dim PASSWORD As String = "PASSWORD"
    Dim virtualFolder As String = "~/Scripts/"
    Dim physicalFolder As String = "physicalFolder"
    Dim unixLogin As String = (USERNAME & "@" & COMPUTERNAME & ":" & UNIXSCRIPTNAME)
    Dim processCmdFileTransfer As String = "C:\pscp.exe -pw " & PASSWORD & " " & physicalFolder & "\" & UNIXSCRIPTNAME & " " & USERNAME & "@" & COMPUTERNAME & ":" & UNIXSCRIPTNAME
    Dim processCmdFileActions As String = "-ssh -pw " & PASSWORD & " " & USERNAME & "@" & COMPUTERNAME & "XX" & UNIXSCRIPTNAME

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        test()
    End Sub

    Public Sub test()

        RunProcess("C:\Windows\System32\cmd.exe", processCmdFileTransfer)
        RunProcess("C:\plink.exe", processCmdFileActions, " chmod u+x ./")
        RunProcess("C:\plink.exe", processCmdFileActions, " ./")
        RunProcess("C:\plink.exe", processCmdFileActions, " rm ./")

    End Sub

    Private Sub RunProcess(processPath As String, startInfo As String, Optional command As String = "")


        Dim Proc As New System.Diagnostics.Process
        Proc.StartInfo = New ProcessStartInfo(processPath)

        If (InStr(startInfo, "XX") > 0) And (command <> "") Then
            startInfo = startInfo.Replace("XX", command)
        End If


        Proc.StartInfo.Arguments = startInfo
        Proc.StartInfo.RedirectStandardInput = True
        Proc.StartInfo.RedirectStandardOutput = False
        Proc.StartInfo.UseShellExecute = False
        Proc.Start()
        Proc.WaitForExit()

    End Sub

结束类

于 2012-08-04T02:33:58.937 回答