我该如何处理论点

时间:2015-03-25 13:02:52

标签: vbscript

我找到了一个适合我需要的脚本。但是,我想加强它。

这是剧本:

strSSH = "C:\Program Files (x86)\PuTTY\putty.exe"
strSSHidentity1 = "C:\id_rsa.ppk"
strSSHidentity2 = "C:\id_dsa.ppk"

Select Case WScript.Arguments.Count
    Case 0
        WScript.Echo "No hostname provided.  Aborting SSH operation."
        WScript.Quit
    Case Else
        Set colArgs = WScript.Arguments
        For i = 0 To WScript.Arguments.Count - 1
            strHost = " " & WScript.Arguments.Item(i)
        Next
End Select

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """" & strSSH & """ """ & CleanHostname(strHost) & """ """ & "-i" & """ """ & strSSHidentity1 & """ """ & "-i" & """ """ & strSSHidentity1 & """"
WScript.Quit

Function CleanHostname(strHost)
    strHost = Trim(strHost)
    'Remove protocol if it was passed
    If InStr(strHost, "ssh://") = 1 Then
        strHost = Right(strHost, Len(strHost) - 6)
    End If
    'Remove trailing slash if present
    If InStrRev(strHost, "/") = Len(strHost) Then
        strHost = Left(strHost, Len(strHost) - 1)
    End If
    'Return cleaned hostname
    CleanHostname = strHost
End Function

我用它来启用ssh://超链接。 如何修改它以便它接受用户在命令行中提供的更多参数而不仅仅是主机名? (例如-p 22)。

请求输出的示例:

CMD:

wscript ssh.vbs ssh://root@myhost -p 22 -i my_key.ppk

最终会运行:

 "C:\Program Files (x86)\PuTTY\putty.exe" myhost -i C:\id_rsa.ppk -i C:\id_dsa.ppk -p 22 -i my_key.ppk

谢谢!

1 个答案:

答案 0 :(得分:0)

当您使用.Arguments(即.Arguments.Unnamed)获取主机列表时, 参数应该通过.Arguments.Named传递。在代码中:

Option Explicit

Dim goWAN : Set goWAN = WScript.Arguments.Named
Dim goWAU : Set goWAU = WScript.Arguments.UnNamed

Dim sPort : sPort = "22"

If 0 = goWAU.Count Then
   WScript.Echo "need at least one hostname"
Else
   Dim u
   For Each u in goWAU
       WScript.Echo "Host:", u
   Next
   If goWAN.Exists("p") Then sPort = goWAN("p")
   WScript.Echo "Port:", sPort
End If

输出:

cscript 29256477.vbs
need at least one hostname

cscript 29256477.vbs pi pa po
Host: pi
Host: pa
Host: po
Port: 22

cscript 29256477.vbs pi /p:23
Host: pi
Port: 23

cscript 29256477.vbs pi /p:4711 pa po
Host: pi
Host: pa
Host: po
Port: 4711