Ssh – Dumping command output from SSH session to procurve

hp-procurvenetworkingplinkputtyssh

I am trying to get information from the command "show mac-address" out of a collection of 12 procurve switches in 2 different locations all connected via fiber PTP. I want to import this into SQL for some scheduled reporting of statistics, movement, routes, etc… I have managed to generate some very interesting and useful data that will eventually allow me to get this monster I inherited into less of the monster it is.

I have all components of this working other that the fact I have to manually go grab the data per switch using putty, save them in a directory where a scheduled task formats the dumps into csv, cleans them up, and imports them using BULK INSERT.

Now I want to automate retrieval of the data from the switches so I can focus on the data side, and stop spending so much time on the collection side.

I have tried PLINK, however there seems to be some sort of emulation issue where the data is interpreted as the incorrect format, and I get files full of gibberish.

using…

plink -batch -ssh -l <username> -pw <password> xxx.xxx.xxx.xxx < cmds.txt > out.txt

the switches are in stacks, so i have to get past the "not hp management" messages, and "what switch do you want to log into" prompts, this seems to work with just \n\n in the file (same as I would when I log into putty, enter twice) But past that I get long ASCII sequences that seems odd because everything up to that point sees to work without issue.

I have already accepts the key in putty, and again since I am getting all the login and stack commander messages, I am assuming all of this is working.

Any clues on how to either get this to work, or a reasonable alternative to achieve the same?

Example of what I am getting…

HP J9148A 2910al-48G-PoE Switch

Software revision W.15.13.0005



Copyright (C) 1991-2014 Hewlett-Packard Development Company, L.P.

                   RESTRICTED RIGHTS LEGEND
 Confidential computer software.  Valid license from HP required for possession,
 use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer
 Software, Computer Software Documentation, and Technical Data for Commercial
 Items are licensed to the U.S. Government under vendor's standard commercial
 license.
                   HEWLETT-PACKARD DEVELOPMENT COMPANY, L.P. 
                   20555 State Highway 249, Houston, TX 77070


Non-HP transceiver detected, which may cause network problems.
Use 'show interface transceiver' command for details.
HP will not support or troubleshoot problems with these transceivers.
[1;15r[1;1H[24;1HPress any key to continue[15;1H[?25h[24;27H[2J[?7l[1;15r[?6l[24;27H[?25h[23;1H  Stack Members

  SN MAC Address   System Name   Device Type          Status                   
  -- ------------- ------------- -------------------- -------------------------
  0  xxxxxx-xxxxxx Switch1        HP 2910al-48G-PoE   Commander Up             
  1  xxxxxx-xxxxxx Switch2        HP 2910al-48G       Member Up                
  2  xxxxxx-xxxxxx Switch3        HP 2910al-24G-PoE   Member Up                                

[23;1HEnter switch number to connect to or <CR>:[23;1H[23;44H[?25h[23;1H[?25h[23;44H[?6l[1;24r[?7l[2J[1;1H[1920;1920H[6n[1;1HYour previous successful login (as manager) was on 2016-01-29 19:31:41     
 from xx.x.x.xxx
[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HSwitch1# [24;1H[24;11H[24;1H[?25h[24;11H[24;0HE[24;1H[24;11H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HSwitch1# [24;1H[24;11H[24;1H[?25h[24;11H[24;0HE[24;1H[24;11H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HSwitch1#

So I am getting so far as the Switch1# prompt at the switch console.

my input file is at the moment just

show mac-address

With two blank lines above to perform the two "any key" and to continue" requests.

Any help greatly appreciated.

Best Answer

Do you have to use putty from Windows? Coming from a Linux box, I would either do

( echo $password ; echo ; echo ; echo show mac-address ) \
| ssh -l $user xxx.xxx.xxx.xxx

or if that does not work, I'd use the program expect. In fact there is a version of expect for Windows that you could probably use instead of plink.

Expect syntax example off the top of my head with varying switch output (should work with or without the "press any key", but of course I have tested nothing):

#!/usr/bin/expect -f
spawn ssh -l loginuser [lindex $argv 0]

set timeout 600

while (1) {
    expect "Press any key to continue" { send "\n" }
           "Enter switch number to connect to or <CR>:" { send "password\n" }
           "#" { break }
}

send "show mac-address\n"

expect "#"

send "quit\n"

expect eof

But do as Paul suggested and try out autoexpect, it will let you run through your script and will output the expect script that will do the same thing. Then you take that output and replace the name or IP of the switch with [lindex $argv 0], and execute it with the name of the switch as argument.