从SSH会话转储命令输出到procurve

我试图通过光纤PTP连接的2个不同位置的12台procurve交换机中的“show mac-address”命令获取信息。 我想将这个数据导入到SQL中,用于统计,移动,路线等的预定报告。我已经设法生成了一些非常有趣和有用的数据,最终可以让我将这个怪物inheritance到更less的怪物中是。

我有这个工作的所有组件,其实我必须手动抓住每个开关使用putty数据,保存在一个目录,其中一个计划的任务格式化转储到CSV,清理起来,并使用BULK INSERT导入它们。

现在,我想自动从交换机中检索数据,这样我就可以专注于数据方面,并停止在收集方面花费太多时间。

我尝试了PLINK,但似乎有某种模拟问题,数据被解释为不正确的格式,我得到的文件充满了胡言乱语。

使用…

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

交换机是在堆栈中,所以我必须通过“不是HPpipe理”消息和“你想要login到什么开关”的提示,这似乎在文件中工作(\ n \ n)(与我一样当我login腻子时,input两次)但过去,我得到了很长的ASCII序列,似乎很奇怪,因为到目前为止的一切看起来没有问题的工作。

我已经接受了腻子的钥匙,并且由于我得到了所有login和堆栈命令的消息,所以我假设所有这些工作都正常。

如何得到这个工作的任何线索,或者一个合理的select来实现相同?

我得到的例子…

 HP J9148A 2910al-48G-PoE Switch Software revision W.15.13.0005 Copyright (C) 1991-2014 Hewlett-Packard Development Company, LP 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 US Government under vendor's standard commercial license. HEWLETT-PACKARD DEVELOPMENT COMPANY, LP 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.xxxxx [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# 

所以我在交换机控制台上得到了Switch1#的提示。

我的input文件是在这一刻

 show mac-address 

用上面的两个空行来执行两个“任意键”并继续“请求”。

任何帮助不胜感激。

你必须使用Windows的腻子吗? 来自一个Linux的盒子,我会做

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

或者如果这不起作用,我会使用程序expect 。 实际上有一个Windows版本,你可以使用,而不是plink。

期望语法示例closures我的头顶部与变化的开关输出(应该与或不按“按任何键”工作,但我当然没有testing过任何东西):

 #!/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 

但是,按照Paul的build议并尝试使用autoexpect ,它可以让你运行你的脚本,并输出期望的脚本来完成同样的事情。 然后你输出这个输出,用[lindex $argv 0]replace交换机的名字或者IP,并且用交换机的名字作为参数来执行它。