機器のポートからユーザーマシンへ

その日の親切な時間。



この投稿では、PowerShellを使用して、私たちの生活を少し楽にし、ユーザーのコンピューターが置かれている機器やポートの検索を自動化する方法を説明します。 これは、VLANを転送する必要があるときに必要です(まあ、または単に情報提供のため)。





背景


それはすべて約1年前に始まりました。 かつて、私たちの上級管理者は、このトピックに関するスクリプトを記述しようと提案しました。 カボチャを傷つけた後、試してみることに同意しました。 それまでは、ネットワーク機器を使ったことがなかったので(ホームルーターは考慮されていません)、彼はこれをすべて実行できるコマンドのおおよそのシーケンスを投げました。



Puttyを使用して、すべて手動で実行しようとしましたが、すべてがうまくいき、すべてを自動化する方法を考え始めました。 はい、自動化するだけでなく、PowerShellを使用して行います。 PoShを選ぶ理由 そのとき、私は彼をつぶしました(まだ彼から降りることはできませんが)、私は他の何かでそれをすることができましたが、私は本当にPoShを通してそれをやりたかったです。



TelnetとSSHを介して機器に接続する必要があったため(主にTelnetを介して、当時SSHはどこにでもなかったが、後でさらに詳しく説明しました)、PowerShellがこれらのプロトコルでどのように機能するかを見つけるためにインターネットで多くの時間を費やしました。

次に、2つの接続メカニズムに注目しました。

  1. plink.exeを使用(Puttyから)
  2. Netcmdlets Firm / nソフトウェア


主にplink.exeを使用して、ほとんど芸術作品とは言えないものを得ました。 誰も見せたくなかった巨大なスクリプトです。 さらに、これに関する記事をここに書いてください。

そして、(定期的に、私もそれを使用していました)働いたので、私はその最適化を長時間延期し、他のものを取り上げました。



時間が経ちましたが、この間に多くの変化がありましたが、私は頭脳を忘れず、定期的に書き直し、不必要なものを取り除きます。 ついに成功しました!



ベイダー、上昇!


発生した最も重要なことは、彼らがスイッチのファームウェアを更新したことであり、SSHを介してそれらに接続できるようになりました。 まあ、素晴らしい、それから私たちはtelnetを叩きます。

その後、別の1つの変更が行われました。telnetは不要になったため、plink.exe(Puttyから)を使用できず、すべての名声がNetCmdletsに送られます。

この会社の技術サポートとかなり話し合って、ようやくこれらのツールの配置を考え出し、作業を開始しました。



必要なもの:

  1. コンピューターのIPアドレスが正しく入力されていることを確認してください
  2. ネットワーク上のコンピューターを確認する
  3. 最も重要なものから順に、各機器に直列に接続し、デバイスがハングするポートを見つけます
  4. このすべてをユーザーに見せて、ユーザー(つまり私)が満足するようにします


NetCmdlets


それらを作成した人々、小さな余談、およびこれらのコマンドレットに関するいくつかの言葉に感謝したいので:

それらについて読んで、 ここからダウンロードしてください 。 試用版(30日間)と完全版(コンピューターあたり100ドル)の両方をダウンロードできます。 私は試用版を使用しました 期間終了後は、簡単に再インストールできます。

最近、これらのコマンドレットはpowershellmagazine.comで無料で配布されました。 だから注意してください! しかし、ラムに戻りましょう。

これらのコマンドレットのセットを選択したら、それらの調査を開始しました。 このセットは十分に大きいので、そこにあるすべてを説明するつもりはありません。 2に焦点を当てます。



Invoke-SShのみを省くことができますが、デバイスとの永続的なセッションはありません(つまり、デバイスの「conf t」でコマンドを実行できなくなります)。 したがって、Connect-SShコマンドレットを使用して、デバイスへの接続を作成し、Invoke-SShコマンドレットを実行します。 すべてが非常に簡単です。

重要な点は、これらのコマンドレットがGet-Credentialコマンドレットと連携できることです。Get-Credentialコマンドレットでは、機器に接続するための資格情報を記述できます(スクリプトの例を参照)。 資格情報は、スクリプトにクリアテキストで保存されません(plink.exeを使用していたため)。 私は妄想ではありませんが、安全にプレイしたいです。



出力解析と正規表現


Invoke-SSHコマンドレットがテキスト列に出力を表示するため、Cisco iOSシェルの出力の非常にロマンチックな解析を期待していました。 出力を変数に渡すと、大きなテキストが取得されます。

これは正規表現が助けになるところです。 しかし、大きなテキストを解析する人は誰でも、適切な断片を取り出すのは簡単ではないことを知っていますが、PowerShellのおかげで、この状況から見事に抜け出しました。

今のところ、Action 2-nd EditionのPowerShellを読んでください。 PoShを学んでいる人には、この本を強くお勧めします。 そして、その中でregexpという名前のものについて読みました。 一番下の行は、PowerShellでは、$ Mathces変数が含まれており、正規表現を使用するときにすべての一致を記録します。



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)







All Articles