Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 443
Elias Avatar asked Oct 20 '25 04:10

Elias


2 Answers

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.

like image 67
Cpt.Whale Avatar answered Oct 23 '25 01:10

Cpt.Whale


As mentioned in your comment (and edited in your question)

plink <ip> -P 22 -ssh -v -l admin -pw pwd -batch `n;show mac-address

works fine. You could automate this by just joining your statements together with Join-String:

$ip = "192.168.123.123"
@(
    "\n",
    "show-mac-address",
    "just-another-command"
) | Join-String -Separator ";" | ForEach-Object {
    plink $ip -P 22 -ssh -v -l admin -pw pwd -batch $_
}

This basic example simply renders to plink 192.168.123.123 -P 22 -ssh -v -l admin -pw pwd -batch \n;show-mac-address;just-another-command.

This happens in two steps: Join-String takes your input array from the pipeline and joins it together to one string: \n;show-mac-address;just-another-command. This one string is then passed thru the pipeline to ForEach-Object. This cmdlet takes it, sets PsItem to this Value and executes the plink command one time for every string passed to it (which is in fact only one element)

For older PowerShell-Versions [string]::Join(";", $statements) might be of help for you.

like image 28
Clijsters Avatar answered Oct 22 '25 23:10

Clijsters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!