Samples Slideshow

  1. <# 
  2. ALZDBA 2011-12-27 
  3. Run a given set of telnet commands agaings given ipaddresses ( and wait for the device to reboot if needed ) 
  4.  
  5. #> 
  6. function readResponse { 
  7.     while($stream.DataAvailable)  {   
  8.           $read = $stream.Read($buffer, 0, 1024)     
  9.           write-verbose $encoding.GetString($buffer, 0, $read
  10.        }  
  11.     
  12.  
  13. function Invoke-RunTelnetScript {  
  14.     [CmdletBinding()]  
  15.     param(  
  16.     [Parameter(Position=0, Mandatory=$true)] [string]$remoteHost,  
  17.     [Parameter(Position=1, Mandatory=$true)] [string]$Hostname
  18.     [Parameter(ParameterSetName='Command2BExecuted', Position=2, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] 
  19.         [ValidateNotNullOrEmpty()] 
  20.         [System.String[]] 
  21.         $Command2BExecuted
  22.     [Parameter(Position=3, Mandatory=$false)] [boolean]$Wait4Reboot = 0  #should it monitor a reboot ? 
  23.     )  
  24. <# 
  25. Run a given set of telnet commands agaings given ipaddresses ( and wait for the device to reboot if needed ) 
  26. #> 
  27.     if ($remoteHost -eq "" -or $Hostname -eq "" ) { 
  28.         "Please specify the $remoteHost and $Hostname"  
  29.         return; } 
  30.  
  31.     $port = 23  
  32.     $socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)  
  33.     $socket.SendTimeout = 10 
  34.     $socket.ReceiveTimeout = 10 
  35.  
  36.     if($socket -eq $null) { return; }  
  37.  
  38.     $stream = $socket.GetStream()  
  39.     $writer = new-object System.IO.StreamWriter($stream)  
  40.     $buffer = new-object System.Byte[] 1024  
  41.     $encoding = new-object System.Text.AsciiEncoding  
  42.     readResponse($stream
  43.     foreach ( $cmd in $Command2BExecuted ) { 
  44.         write-verbose $cmd 
  45.         $writer.WriteLine($cmd)  
  46.         $writer.Flush() 
  47.         #wait 500 miliseconds 
  48.         start-sleep -m 500  
  49.         readResponse($stream
  50.         
  51.  
  52.     #If indicated reboot is needed we will use ping to figure out reboot sequence 
  53.     if ( $Wait4Reboot -eq $true ) { 
  54.         $bln = $false  
  55.         $flipflop = $false 
  56.         $ping = new-object System.Net.NetworkInformation.Ping 
  57.         while ( $bln -eq $false ) { 
  58.             #session will loose connection because of reboot. Just ping after 60 seconds to see if it is alive ( restarted ) 
  59.             $pReply = $ping.send($remoteHost
  60.             if ( $pReply.Status -eq 'Success' ){ 
  61.                     if ( $flipflop -eq $true ) { 
  62.                         #reboot ended 
  63.                         $bln = $true  
  64.                         write-verbose 'Back Online' 
  65.                         
  66.                     else
  67.                         write-verbose "Still Online - $hostname"  
  68.                         start-sleep -s
  69.                         
  70.                 
  71.             else
  72.                 'Offline' 
  73.                 if ( $flipflop -eq $false ) { 
  74.                     #first offline is reboot ongoing 
  75.                     $flipflop = $true  
  76.                     
  77.             
  78.         
  79.     
  80.  
  81.     $writer.Close()  
  82.     $stream.Close()  
  83.  
  84.     Write-Host 'End of Telnet command set processing' 
  85.  
  86.  
  87. # -------------- Begin execution --------------------- 
  88. clear-host 
  89.  
  90. $colInputParams = '10.231.234.141,MyDevice1' , '10.231.134.142,MyDevice2' 
  91.  
  92. $InputTelnetCommandSet = get-content C:\temp\Telnet\Script.txt 
  93. #just show all commands in the set on a grid-view --> to be commented at runtime 
  94. $InputTelnetCommandSet | Out-GridView  
  95.  
  96. foreach ($strParams in $colInputParams){ 
  97. $strComputer =$strParams.Substring( 0, $strParams.LastIndexOf(',') ) 
  98. $strHostname =$strParams.Substring( $strParams.LastIndexOf(',') + 1  
  99. $strComputer 
  100. $strHostname 
  101. Invoke-RunTelnetScript -remoteHost $strComputer -Hostname $strHostname -Command2BExecuted $InputTelnetCommandSet -Wait4Reboot $false 
  102.  
  103.  
  104.  
  105.  
  106. <# C:\temp\Telnet_Script.txt contains these records 
  107. mytelnetlogin 
  108. mytelnetpwd 
  109. en 
  110. anotherpwd 
  111. conf t 
  112. max-vlans 15 
  113. end 
  114. write mem 
  115. #> 
 
Loading...
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.