Created
November 17, 2015 09:17
-
-
Save timfel/70790f17673a4949f6ae to your computer and use it in GitHub Desktop.
Switch to fastest connected interface to pipe Hyper-V network, trigger DHCP in VM, start Xming, connect via ssh, start xterm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Ping-IPRange { | |
<# | |
.EXAMPLE | |
Ping-IPRange -StartAddress 192.168.1.1 -EndAddress 192.168.1.254 -Interval 20 | |
.LINK | |
http://gallery.technet.microsoft.com/Fast-asynchronous-ping-IP-d0a5cf0e | |
#> | |
[CmdletBinding(ConfirmImpact='Low')] | |
Param( | |
[parameter(Mandatory = $true, Position = 0)] | |
[System.Net.IPAddress]$StartAddress, | |
[parameter(Mandatory = $true, Position = 1)] | |
[System.Net.IPAddress]$EndAddress, | |
[int]$Interval = 30, | |
[Switch]$RawOutput = $false | |
) | |
$timeout = 2000 | |
function New-Range ($start, $end) { | |
[byte[]]$BySt = $start.GetAddressBytes() | |
[Array]::Reverse($BySt) | |
[byte[]]$ByEn = $end.GetAddressBytes() | |
[Array]::Reverse($ByEn) | |
$i1 = [System.BitConverter]::ToUInt32($BySt,0) | |
$i2 = [System.BitConverter]::ToUInt32($ByEn,0) | |
for($x = $i1;$x -le $i2;$x++){ | |
$ip = ([System.Net.IPAddress]$x).GetAddressBytes() | |
[Array]::Reverse($ip) | |
[System.Net.IPAddress]::Parse($($ip -join '.')) | |
} | |
} | |
$IPrange = New-Range $StartAddress $EndAddress | |
$IpTotal = $IPrange.Count | |
Get-Event -SourceIdentifier "ID-Ping*" | Remove-Event | |
Get-EventSubscriber -SourceIdentifier "ID-Ping*" | Unregister-Event | |
$IPrange | foreach{ | |
[string]$VarName = "Ping_" + $_.Address | |
New-Variable -Name $VarName -Value (New-Object System.Net.NetworkInformation.Ping) | |
Register-ObjectEvent -InputObject (Get-Variable $VarName -ValueOnly) -EventName PingCompleted -SourceIdentifier "ID-$VarName" | |
(Get-Variable $VarName -ValueOnly).SendAsync($_,$timeout,$VarName) | |
Remove-Variable $VarName | |
try{ | |
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count | |
}catch [System.InvalidOperationException]{} | |
$index = [array]::indexof($IPrange,$_) | |
Write-Progress -Activity "Sending ping to" -Id 1 -status $_.IPAddressToString -PercentComplete (($index / $IpTotal) * 100) | |
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($index - $pending) -PercentComplete (($index - $pending)/$IpTotal * 100) | |
Start-Sleep -Milliseconds $Interval | |
} | |
Write-Progress -Activity "Done sending ping requests" -Id 1 -Status 'Waiting' -PercentComplete 100 | |
While($pending -lt $IpTotal){ | |
Wait-Event -SourceIdentifier "ID-Ping*" | Out-Null | |
Start-Sleep -Milliseconds 10 | |
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count | |
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($IpTotal - $pending) -PercentComplete (($IpTotal - $pending)/$IpTotal * 100) | |
} | |
if($RawOutput){ | |
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach { | |
If($_.SourceEventArgs.Reply.Status -eq "Success"){ | |
$_.SourceEventArgs.Reply | |
} | |
Unregister-Event $_.SourceIdentifier | |
Remove-Event $_.SourceIdentifier | |
} | |
}else{ | |
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach { | |
If($_.SourceEventArgs.Reply.Status -eq "Success"){ | |
$_.SourceEventArgs.Reply | select @{ | |
Name="IPAddress" ; Expression={$_.Address}}, | |
@{Name="Bytes" ; Expression={$_.Buffer.Length}}, | |
@{Name="Ttl" ; Expression={$_.Options.Ttl}}, | |
@{Name="ResponseTime"; Expression={$_.RoundtripTime}} | |
} | |
Unregister-Event $_.SourceIdentifier | |
Remove-Event $_.SourceIdentifier | |
} | |
} | |
if($Reply -eq $Null){ | |
Write-Verbose "Ping-IPrange : No ip address responded" -Verbose | |
} | |
return $Reply | |
} | |
function EnableICS | |
{ | |
regsvr32.exe /s hnetcfg.dll | |
# pick the fastest connected physical network as master | |
$Master = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected' -and $_.PhysicalMediaType -ne 'Unspecified'} | Sort-Object LinkSpeed -Descending | |
$Slave = Get-NetAdapter | Where-Object {$_.DriverDescription -eq 'Hyper-V Virtual Ethernet Adapter'} | |
$m = New-Object -ComObject HNetCfg.HNetShare | |
$m.EnumEveryConnection | % { $m.INetSharingConfigurationForINetConnection.Invoke($_).DisableSharing() } | |
$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Guid -eq $Master.InterfaceGuid } | |
$m.INetSharingConfigurationForINetConnection.Invoke($c).EnableSharing(0) | |
$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Guid -eq $Slave.InterfaceGuid } | |
$m.INetSharingConfigurationForINetConnection.Invoke($c).EnableSharing(1) | |
} | |
function StartXTerm | |
{ | |
$fileassoc = cmd /c assoc .xlaunch | |
$pathassoc = cmd /c ftype $fileassoc.Split("=")[1] | |
$xlaunchpath = Split-Path -parent -Path $pathassoc.split('"')[1] | |
$xmingexe = Join-Path $xlaunchpath "Xming.exe" | |
$plinkexe = Join-Path $xlaunchpath "plink.exe" | |
$ppk = Join-Path $xlaunchpath "id_rsa.ppk" | |
start -FilePath $xmingexe -ArgumentList '-br','-multiwindow','-clipboard','-dpi','160','-compositewm','-wgl','-silent-dup-error' -NoNewWindow | |
$Slave = Get-NetAdapter | Where-Object {$_.DriverDescription -eq 'Hyper-V Virtual Ethernet Adapter'} | |
Remove-VMNetworkAdapter Ubuntu | |
sleep 2 | |
Add-VMNetworkAdapter -VMName Ubuntu -SwitchName $Slave.Name.Split("(").Split(")")[1] | |
sleep 2 | |
$ip = Get-NetIPAddress | Where-Object {$_.InterfaceAlias -eq $Slave.Name -and $_.AddressFamily -eq "IPv4" } | |
$ip = $ip.IPAddress.Split(".") | |
$ip[3] = 100 | |
$startip = $ip -join "." | |
$ip[3] = 250 | |
$endip = $ip -join "." | |
$response = Ping-IPRange -StartAddress $startip -EndAddress $endip -Interval 10 | |
$clientip = $response.IPAddress.IPAddressToString | |
$vm = 'tim@'+$clientip | |
start -FilePath $plinkexe -ArgumentList '-batch','-ssh','-X','-i',$ppk,$vm,'xfce4-terminal' -NoNewWindow | |
} | |
EnableICS | |
sleep 2 | |
StartXTerm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment