forked from sinmygit/git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-SSHPortForward.ps1
More file actions
52 lines (45 loc) · 1.79 KB
/
Copy pathInvoke-SSHPortForward.ps1
File metadata and controls
52 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Invoke-SSHPortForward {
<#
usage:Invoke-SSHPortForward -pw root -R 80:172.16.5.9:80 -H root@192.168.1.2
#>
param (
[Parameter(Mandatory = $False)][int]$P=22,
[Parameter(Mandatory = $True)][string]$R,
[Parameter(Mandatory = $True)][string]$pw,
[Parameter(Mandatory = $True)][string]$H
)
if ($R.split(":").Count -ne 3){Write-Host "-R para error." ; return}
$BoundPort=[int]($R.split(":")[0])
$RHost=$R.split(":")[1]
$RPort=[int]($R.split(":")[2])
if ($H.split("@").Count -ne 2){Write-Host "-H para error." ; return}
$UserName = $H.split("@")[0]
$Server = $H.split("@")[1]
$BoundHost = '0.0.0.0'
$AssemblyFile = "$Env:TEMP\FXSDebuglog.txt"
if (!(Test-Path $AssemblyFile)) {
$dclient = new-object System.Net.WebClient
$dclient.DownloadFile('https://raw.githubusercontent.com/sinmygit/git/master/ssh.dll', $AssemblyFile)
}
[Reflection.Assembly]::LoadFile($AssemblyFile) | out-null
$Client = New-Object Renci.SshNet.SshClient($Server, $P, $UserName, $pw)
Try {
$Client.Connect()
if ($Client.IsConnected) {
Try {
$ForwardPort = New-Object Renci.SshNet.ForwardedPortRemote([IPAddress]$BoundHost, $BoundPort, [IPAddress]$RHost, $RPort)
}
Catch {Throw "Error create Forwarded port object. $($_.Exception.Message)"}
}
else {Throw "Error connecting ${Server}"}
}
Catch {Throw "Error connecting ${Server}. $($_.Exception.Message)"}
Try {$Client.AddForwardedPort($ForwardPort)}
Catch {Throw "Error adding Forwarded port object. $($_.Exception.Message)"}
Try {$ForwardPort.Start()}
Catch {Throw "Error start. $($_.Exception.Message)"}
$ForwardPort |fl
while($Client.IsConnected){
Start-Sleep -Milliseconds 100
}
}