Files
hNetworkSwitchDiscovery/Start-SwitchDiscovery.ps1
2026-05-06 10:43:38 +03:00

233 lines
9.5 KiB
PowerShell

#Requires -Version 5.1
Set-StrictMode -Version Latest
$ErrorActionPreference = "SilentlyContinue"
$configFile = Join-Path $PSScriptRoot "discovery_config.json"
function Write-Banner {
Clear-Host
Write-Host " ======================================================" -ForegroundColor Cyan
Write-Host " Network Switch Discovery Tool v1.1" -ForegroundColor Cyan
Write-Host " SSH + SNMP | Cisco / HP / Ubiquiti / Huawei / TP-Link" -ForegroundColor Cyan
Write-Host " ======================================================" -ForegroundColor Cyan
Write-Host ""
}
function Write-Menu {
param([hashtable]$c)
Write-Host " Mevcut Ayarlar:" -ForegroundColor DarkGray
Write-Host (" | IP Aralik : " + $c.IPRange) -ForegroundColor Gray
Write-Host (" | Kullanici : " + $c.Username) -ForegroundColor Gray
Write-Host (" | Yontem : " + $c.Method) -ForegroundColor Gray
Write-Host (" | SNMP Comm. : " + $c.SNMPCommunity) -ForegroundColor Gray
Write-Host (" | SSH Port : " + $c.SSHPort) -ForegroundColor Gray
Write-Host (" | Max Thread : " + $c.MaxThreads) -ForegroundColor Gray
Write-Host (" | Timeout (s) : " + $c.Timeout) -ForegroundColor Gray
Write-Host ""
Write-Host " +------------------------------------------+" -ForegroundColor Cyan
Write-Host " | [1] Taramayi Baslat |" -ForegroundColor White
Write-Host " | [2] IP Araligini Degistir |" -ForegroundColor White
Write-Host " | [3] SSH Kimlik Bilgilerini Gir |" -ForegroundColor White
Write-Host " | [4] SNMP Ayarlarini Duzenle |" -ForegroundColor White
Write-Host " | [5] Gelismis Ayarlar |" -ForegroundColor White
Write-Host " | [6] Yontemi Degistir (SSH/SNMP/Both) |" -ForegroundColor White
Write-Host " | [7] Profil Kaydet / Yukle |" -ForegroundColor White
Write-Host " | [8] Bagimlilik Kontrol/Kur |" -ForegroundColor White
Write-Host " | [Q] Cikis |" -ForegroundColor DarkGray
Write-Host " +------------------------------------------+" -ForegroundColor Cyan
Write-Host ""
}
function rInput {
param([string]$p,[string]$d="")
$disp = if ($d) { "$p [$d]" } else { $p }
Write-Host " $disp : " -ForegroundColor Yellow -NoNewline
$v = Read-Host
if ([string]::IsNullOrWhiteSpace($v)) { return $d }
return $v
}
function rSecret {
param([string]$p)
Write-Host " $p : " -ForegroundColor Yellow -NoNewline
$s = Read-Host -AsSecureString
return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($s))
}
function Get-DefaultConfig {
return @{
IPRange="192.168.1.0/24"; Username="admin"; Password=""
SNMPCommunity="public"; SNMPVersion="v2c"; Method="Both"
OutputPath=$PSScriptRoot; MaxThreads=20; SSHPort=22; Timeout=5
Profiles=@{}
}
}
function Load-Config {
if (Test-Path $configFile) {
try {
$j = Get-Content $configFile -Raw | ConvertFrom-Json
$cfg = Get-DefaultConfig
foreach ($k in @("IPRange","Username","SNMPCommunity","SNMPVersion","Method","OutputPath","MaxThreads","SSHPort","Timeout")) {
if ($j.PSObject.Properties.Name -contains $k) { $cfg[$k] = $j.$k }
}
return $cfg
} catch { }
}
return Get-DefaultConfig
}
function Save-Config {
param([hashtable]$c)
$s = $c.Clone(); $s.Password = ""
$s | ConvertTo-Json -Depth 5 | Out-File $configFile -Encoding UTF8
}
function Set-IPRange {
param([hashtable]$c)
Write-Host "`n Format ornekleri:" -ForegroundColor DarkGray
Write-Host " CIDR : 192.168.1.0/24" -ForegroundColor DarkGray
Write-Host " Tire : 10.0.0.1-10.0.0.50" -ForegroundColor DarkGray
Write-Host " Tekil: 172.16.0.1" -ForegroundColor DarkGray
$c.IPRange = rInput "Yeni IP araligi" $c.IPRange
}
function Set-SSH {
param([hashtable]$c)
$c.Username = rInput "SSH Kullanici adi" $c.Username
$c.Password = rSecret "SSH Sifre (gizli)"
Write-Host " [+] SSH bilgileri guncellendi" -ForegroundColor Green
}
function Set-SNMP {
param([hashtable]$c)
$c.SNMPCommunity = rInput "Community string" $c.SNMPCommunity
Write-Host " Versiyon: [1] v1 [2] v2c [3] v3" -ForegroundColor Yellow
$v = rInput "Secim" "2"
$c.SNMPVersion = switch ($v) { "1"{"v1"} "3"{"v3"} default{"v2c"} }
}
function Set-Advanced {
param([hashtable]$c)
$c.MaxThreads = [int](rInput "Thread sayisi (1-100)" $c.MaxThreads)
$c.SSHPort = [int](rInput "SSH port" $c.SSHPort)
$c.Timeout = [int](rInput "Timeout (saniye)" $c.Timeout)
$c.OutputPath = rInput "Cikti dizini" $c.OutputPath
}
function Set-Method {
param([hashtable]$c)
Write-Host " [1] SSH [2] SNMP [3] Both" -ForegroundColor Gray
$m = rInput "Yontem" "3"
$c.Method = switch ($m) { "1"{"SSH"} "2"{"SNMP"} default{"Both"} }
Write-Host " [+] Yontem: $($c.Method)" -ForegroundColor Green
}
function Manage-Profiles {
param([hashtable]$c)
Write-Host " [1] Kaydet [2] Yukle [3] Listele" -ForegroundColor Gray
$sel = rInput "Secim" "1"
switch ($sel) {
"1" {
$n = rInput "Profil adi"
if ($n) {
if (-not $c.Profiles) { $c.Profiles = @{} }
$c.Profiles[$n] = @{
IPRange=$c.IPRange; Username=$c.Username
SNMPCommunity=$c.SNMPCommunity; Method=$c.Method
SNMPVersion=$c.SNMPVersion; SSHPort=$c.SSHPort; MaxThreads=$c.MaxThreads
}
Save-Config $c
Write-Host " [+] Profil '$n' kaydedildi" -ForegroundColor Green
}
}
"2" {
if ($c.Profiles -and $c.Profiles.Count -gt 0) {
$keys = @($c.Profiles.Keys)
for ($i=0;$i -lt $keys.Count;$i++) { Write-Host " [$($i+1)] $($keys[$i])" -ForegroundColor Gray }
$idx = [int](rInput "Profil no") - 1
if ($idx -ge 0 -and $idx -lt $keys.Count) {
$p = $c.Profiles[$keys[$idx]]
foreach ($k in $p.PSObject.Properties.Name) { $c[$k]=$p.$k }
Write-Host " [+] Yuklendi: $($keys[$idx])" -ForegroundColor Green
}
} else { Write-Host " Kayitli profil yok" -ForegroundColor DarkYellow }
}
"3" {
if ($c.Profiles -and $c.Profiles.Count -gt 0) {
$c.Profiles.Keys | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }
} else { Write-Host " Profil yok" -ForegroundColor DarkGray }
}
}
}
function Install-Deps {
Write-Host "`n Kontrol ediliyor..." -ForegroundColor Yellow
if (Get-Module -ListAvailable -Name Posh-SSH) {
Write-Host " [+] Posh-SSH kurulu" -ForegroundColor Green
} else {
Write-Host " [-] Posh-SSH eksik" -ForegroundColor Red
$a = rInput "Kurulsun mu? (E/H)" "E"
if ($a -match '^[Ee]') {
Install-Module Posh-SSH -Scope CurrentUser -Force -AllowClobber
Write-Host " [+] Posh-SSH kuruldu" -ForegroundColor Green
}
}
if (Get-Module -ListAvailable -Name ThreadJob) {
Write-Host " [+] ThreadJob kurulu" -ForegroundColor Green
} else {
Write-Host " [!] ThreadJob eksik, kuruluyor..." -ForegroundColor Yellow
Install-Module ThreadJob -Scope CurrentUser -Force
Write-Host " [+] ThreadJob kuruldu" -ForegroundColor Green
}
$dll = Join-Path $PSScriptRoot "lib\SharpSnmpLib.dll"
if (Test-Path $dll) { Write-Host " [+] SharpSnmpLib.dll mevcut" -ForegroundColor Green }
else { Write-Host " [i] SharpSnmpLib.dll yok - yerlesik SNMP UDP kullanilir" -ForegroundColor Gray }
Read-Host "`n Devam icin Enter"
}
function Start-Discovery {
param([hashtable]$c)
if (-not $c.Password -and $c.Method -ne "SNMP") {
$c.Password = rSecret "SSH Sifre"
}
$script = Join-Path $PSScriptRoot "Invoke-SwitchDiscovery.ps1"
if (-not (Test-Path $script)) {
Write-Host " [-] Invoke-SwitchDiscovery.ps1 bulunamadi: $script" -ForegroundColor Red
Read-Host " Enter"; return
}
& $script `
-IPRange $c.IPRange `
-Username $c.Username `
-Password $c.Password `
-SNMPCommunity $c.SNMPCommunity `
-SNMPVersion $c.SNMPVersion `
-Method $c.Method `
-OutputPath $c.OutputPath `
-MaxThreads $c.MaxThreads `
-SSHPort $c.SSHPort `
-Timeout $c.Timeout
Read-Host "`n Menuye donmek icin Enter"
}
# === ANA DONGU ===========================================================
$cfg = Load-Config
while ($true) {
Write-Banner
Write-Menu -c $cfg
Write-Host " Seciminiz: " -ForegroundColor White -NoNewline
$ch = Read-Host
switch ($ch.Trim().ToUpper()) {
"1" { Start-Discovery $cfg }
"2" { Set-IPRange $cfg; Save-Config $cfg }
"3" { Set-SSH $cfg; Save-Config $cfg }
"4" { Set-SNMP $cfg; Save-Config $cfg }
"5" { Set-Advanced $cfg; Save-Config $cfg }
"6" { Set-Method $cfg; Save-Config $cfg }
"7" { Manage-Profiles $cfg }
"8" { Install-Deps }
"Q" { Write-Host "`n Gule gule!`n" -ForegroundColor Cyan; exit 0 }
default { Write-Host " [!] Gecersiz secim" -ForegroundColor Red; Start-Sleep 1 }
}
}