免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3223 | 回复: 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

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

本版积分规则 发表回复

SACC2019中国系统架构师大会

【数字转型 架构演进】SACC2019中国系统架构师大会
2019年10月31日~11月2日第11届中国系统架构师大会(SACC2019)将在北京隆重召开。四大主线并行的演讲模式,1个主会场、20个技术专场、超千人参与的会议规模,100+来自互联网、金融、制造业、电商等领域的嘉宾阵容,将为广大参会者提供一场最具价值的技术交流盛会。




----------------------------------------

大会官网>>
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP