PS [13] > $Matches PS [14] > $a="ass 123 saa" PS [15] > $a -match "\d+" True PS [16] > $Matches Name Value ---- ----- 0 123
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
? , :
PS [17] > $a -match "(?<Numbers>\d+)" True PS [18] > $Matches Name Value ---- ----- Numbers 123 0 123 PS [19] > $Matches.Numbers 123
( split!). , (, PowerShell ), .
, ?
, .
## Function LastSwitch { "IP : {0}, {1}" -f $IpSwitch,$Port If ($CiscoPhone) {" Cisco IP Phone"} Read-Host "Press Enter for continue..." break } ## For (;;) { $ip=Read-host "Enter ip" If ($ip -match "(\d{1,3}\.){3}\d{1,3}") {break} else {Write-Warning "Invalid IP address! Try again..."} } ## if ((test-connection $ip -quiet) -ne "True") { Write-Warning " !" Read-Host "Press Enter to continue..." break } $cred = get-credential Admin ## $IpSwitch = "10.138.30.1" ## $MAC = $null $CiscoPhone = $false For (;;) { $conn = Connect-ssh -Server $IpSwitch -Credential $cred -ShellPrompt "#" -Force Invoke-SSH -Connection $conn -Command "terminal length 0" | out-null Invoke-SSH -Connection $conn -Command "ping $ip" | out-null If (!$MAC) { ## ((Invoke-SSH -Connection $conn -Command "sh arp | i $ip ").text | Where-Object {$_ -match "\w"}) -match "(?<MAC>\w{4}\.\w{4}\.\w{4})" | out-null $MAC = $Matches.MAC } ## , (((Invoke-SSH -Connection $conn -Command "sh mac address-table address $MAC").Text | where-object {$_ -match $mac}) | Select-Object -First 1) -match "(?<port>((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?)" | out-null $port = $Matches.Port ## $portInfo = (Invoke-SSH -Connection $conn -Command "show mac address-table interface $port").Text | where-object {$_ -match $port} If (($portInfo | measure).Count -eq 1) {LastSwitch} ## $DetailPortInfo = (Invoke-SSH -Connection $conn -Command "sh cdp neighbors $port detail" -Force).Text ## "Cisco IP phone", IP . If ($DetailPortInfo -match "Cisco IP phone") {$CiscoPhone = $true; LastSwitch} (($DetailPortInfo | where-object {$_ -match "IP address: (\d{1,3}\.){3}\d{1,3}"}) | Select-Object -First 1) -match "(?<ip>(\d{1,3}\.){3}\d{1,3})" | out-null "IP : {0}, {1}" -f $IpSwitch,$Port $IpSwitch = $Matches.IP Disconnect-SSH $conn }
:
Get-Credential. , , . 1 (.. ( PowerShell: get-help | more
)). , .. Invoke-SSH , . , terminal length 0
((\D{2}\d{1,3})/|Po)(\d{1,3})(/\d{1,3})?
- , . : Fa5, Gi0/2, Te2/0/8, Po255 .. ( , , ) Cisco IP phone, , ( Invoke-SSH), (out-null)
Source: https://habr.com/ru/post/149191/
All Articles