免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5022 | 回复: 6

用perl写了一个自动登录路由器抓配置的脚本,命令不执行,希望高手指点一下 [复制链接]

论坛徽章:
0
发表于 2019-04-17 09:43 |显示全部楼层
use Net::SSH::Perl ;

$host = 'xxxxxx';
$user = 'abc';
$passwd = 'abc';
open LOG,">$host.log";                            #记录执行日志
select LOG;

my $ssh = Net::SSH::Perl->new( $host ,debug =>1,protocol=>2 );
$ssh->login($user,$passwd );

$cmd = "screen-length 0 temporary\n";
$sh = "dis cur\n";     
$tc = "quit\n";

($stdout, $stderr, $exit)  = $ssh->cmd($cmd);
print $stdout;
=pod
($stdout, $stderr, $exit)  = $ssh->cmd( $sh );
print $stdout;

($stdout, $stderr, $exit)  = $ssh->cmd( $tc );
print $stdout;
=cut
close LOG;
select STDIN;
$ssh->close;
=========================执行结果
test: Service accepted: ssh-userauth.
test: Trying empty user-authentication request.
test: Authentication methods that can continue: publickey,keyboard-interactive,password.
test: Next method to try is publickey.
test: Next method to try is password.
test: Trying password authentication.
test: Login completed, opening dummy shell channel.
test: channel 0: new [client-session]
test: Requesting channel_open for channel 0.
test: channel 0: open confirm rwindow 131072 rmax 32768
test: Got channel open confirmation, requesting shell.
test: Requesting service shell on channel 0.
test: channel 1: new [client-session]
test: Requesting channel_open for channel 1.
test: Entering interactive session.    一直停到这儿不在执行后面的语句了 最后出错退出了

test: Entering interactive session.
Received disconnect message: The connection is closed by SSH server

at /usr/local/perl5.28/lib/site_perl/5.28.1/x86_64-linux/Net/SSH/Perl/SSH2.pm line 530.
请高手看看,程序需要什么修改?


论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
发表于 2019-04-20 13:39 |显示全部楼层
  1. $ssh->shell
  2. Opens up an interactive shell on the remote machine and connects it to your STDIN. This is most effective when used with a pseudo tty; otherwise you won't get a command line prompt, and it won't look much like a shell. For this reason--unless you've specifically declined one--a pty will be requested from the remote machine, even if you haven't set the use_pty argument to new (described above).

  3. This is really only useful in an interactive program.

  4. In addition, you'll probably want to set your terminal to raw input before calling this method. This lets Net::SSH::Perl process each character and send it off to the remote machine, as you type it.

  5. To do so, use Term::ReadKey in your program:

  6. use Term::ReadKey;
  7. ReadMode('raw');
  8. $ssh->shell;
  9. ReadMode('restore');
  10. In fact, you may want to place the restore line in an END block, in case your program exits prior to reaching that line.

  11. If you need an example, take a look at eg/pssh, which uses almost this exact code to implement an ssh shell.
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
发表于 2019-04-20 13:39 |显示全部楼层
  1. $ssh->shell
  2. Opens up an interactive shell on the remote machine and connects it to your STDIN. This is most effective when used with a pseudo tty; otherwise you won't get a command line prompt, and it won't look much like a shell. For this reason--unless you've specifically declined one--a pty will be requested from the remote machine, even if you haven't set the use_pty argument to new (described above).

  3. This is really only useful in an interactive program.

  4. In addition, you'll probably want to set your terminal to raw input before calling this method. This lets Net::SSH::Perl process each character and send it off to the remote machine, as you type it.

  5. To do so, use Term::ReadKey in your program:

  6. use Term::ReadKey;
  7. ReadMode('raw');
  8. $ssh->shell;
  9. ReadMode('restore');
  10. In fact, you may want to place the restore line in an END block, in case your program exits prior to reaching that line.

  11. If you need an example, take a look at eg/pssh, which uses almost this exact code to implement an ssh shell.
复制代码

论坛徽章:
0
发表于 2019-04-29 11:07 |显示全部楼层
高手,这段英文看着比吃力,有没有例子,可以学习的?

论坛徽章:
0
发表于 2019-04-29 11:53 |显示全部楼层
回复 4# jkczh

没注释没缩进的程序,谁愿意看?

论坛徽章:
0
发表于 2019-05-05 23:49 |显示全部楼层
试试Net::OpenSSH,比Net::SSH::Perl更主流一些。

论坛徽章:
0
发表于 2019-05-06 08:51 |显示全部楼层
回复 6# zhouzhen1

谢谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会 限时8.5折

【架构革新 高效可控】2020年6月4日~6日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP