powershellplink

Get Mac-Address-Table from HPE Aruba Swtich with plink ssh


this is my code:

@( 
  ' ', #for "Press any key to continue" 
  'show mac-address'
  ) | plink <ip> -P 22 -ssh -v -l admin -pw pwd -batch 

Read-Host -Prompt “Press Enter to exit”

sometimes i get this output:

←[1;13r←[1;1HSession sent command exit status 0
Main session channel closed
All channels closed

and sometimes i get this output:

Remote side unexpectedly closed network connection
FATAL ERROR: Remote side unexpectedly closed network connection

If i write the plink command without Pipe:

plink <ip> -P 22 -ssh -v -l admin -pw pwd -batch 

It works. But i need it automated


Solution

  • I happen to have an Aruba switch and found that specifying non-interactive SSH with -batch, or commands with -m, just causes the switch to disconnect your session like you're seeing. Maybe there's a setting to allow it?

    I found this works pretty well, using more of a .net approach due to issues with Start-Process redirection. This opens plink in a separate process, and sends commands to its stdin:

    # set up a process with redirection enabled
    $psi = New-Object System.Diagnostics.ProcessStartInfo
      $psi.FileName               = "plink.exe" #process file
      $psi.UseShellExecute        = $false      #start the process from it's own executable file
      $psi.WorkingDirectory       = 'C:\Program Files (x86)\PuTTY' # or add to path
      $psi.RedirectStandardInput  = $true       #enable the process to read from our standard input
      $psi.RedirectStandardOutput = $true       #enable reading standard output from powershell
      # update these:
      $psi.Arguments = "$IPADDRESS -P 22 -ssh -l admin -pw $PASS"
    
    # launch plink
    $plink = [System.Diagnostics.Process]::Start($psi);
    sleep 1
    
    # send key presses to plink shell
    $plink.StandardInput.AutoFlush = $true
    $plink.StandardInput.WriteLine() # press return to begin session
    $plink.StandardInput.WriteLine() # press any key to continue (banner)
    $plink.StandardInput.WriteLine('show mac-address') # get macs
    $plink.StandardInput.WriteLine('exit') # exit and confirm logoff
    $plink.StandardInput.WriteLine('exit') 
    $plink.StandardInput.WriteLine('y')
    
    # read output from process
    $output = $plink.StandardOutput.ReadToEnd()
    
    # exit plink
    $plink.Close()
    
    
    # outputs like so (trimmed of banners etc)
    $output
    
    MAC Address       Port                            VLAN
    ----------------- ------------------------------- ----
    005056-00000X     15                              1
    005056-00000Y     9                               1
    005056-00000Z     11                              1
    

    You may have to play around with the commands a bit, but I found plink was pretty generous with how quickly input gets thrown at it

    I did notice if the plink process is waiting (ssh session still connected), then ReadToEnd() will wait forever. Maybe it can be replaced with a ReadLine() loop, but it can be hard to tell when it's done.