用于用户交互的perl脚本 [英] perl script for user interaction

查看:105
本文介绍了用于用户交互的perl脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我写了一个perl脚本来执行以下操作.

I'm new to programming. I have written a perl script which does the following.

1)SSH到远程计算机

1) ssh into a remote machine

2)执行一系列命令.

2) execute a sequence of commands.

该脚本需要用户之间的交互,需要用户名和密码.我不确定该如何进行.

The script requires user interaction in between.It needs user name and password. I'm not sure how to proceed with that.

use Net::SSH::Expect;
use strict; 
use warnings;

#login to a remote host
my $ssh = Net::SSH::Expect->new (host => "ipaddr",
                             password=> 'pwd',
                             user => 'username',
                             raw_pty => 1);


my $login_output = $ssh->login();

登录成功.现在,对于下面的scp命令,系统会提示我输入该系统的用户名和密码.脚本在此处停止.

The login is successful. Now, for the below scp command i'm prompted to enter the username and password of that system.The script stops here.

my $cpscp = $ssh->exec("copy scp install ip addr  filename");



Enter usename for remote scp server:

Enter the password for the remote server:

输入用户名后,我需要输入密码.这是我所做的,但是没有用.

Once the user name is entered I need to enter the password.This is what i did but it didn't work.

my $usr = $ssh->waitfor ("Enter usename for remote scp server:\s*\z ", 5);
if ($usr) 
{
print("string found \n");
my $pwd =$ssh->send("root");
} 
else 
{
print("No string found\n");
}

该脚本无法识别它. 找不到字符串"

The script is unable to identify it. "no string found"

我也尝试了以下

$ssh->waitfor('Enter username for remote scp server:\s*\z ', 5) or die "not found";
$ssh->send("root");

推荐答案

我想用户名提示已经作为对exec

I imagine the prompt for username has already arrived as part of the response to the exec

my $cpscp = $ssh->exec("copy scp install ip addr  filename");

因此后续的waitfor将永远等待.

so a subsequent waitfor will wait for ever.

您应该像这样将sendwaitfor而不是exec一起使用

You should use send with waitfor instead of exec, like this

$ssh->exec('copy scp install <ip-addr> <remote-dir> <remote-filename>');
my $found = $ssh->waitfor ('Enter usename for remote scp server:\s*\z', 5);

还请注意,您在waitfor中使用的字符串可能是错误的

Note also that the string you use in waitfor is probably wrong

"Enter usename for remote scp server:\s*\z "

在可能需要username的情况下有usename,并且在字符串的末尾放置了一个空格,这是不可能的,因为\z标记了输入的末尾,并且在输入之后显然没有任何内容结束.

You have usename when you probably need username, and you have put a space at the end of the string, which is impossible because \z marks the end of your input and there clearly can be nothing after the end.

将同一问题同时发布到两个站点也是一种不好的举止.如果Perl Monks不能为您提供一个可行的解决方案 then 在这里提出问题,但是如果您不知道您的问题是否已经在其他地方找到了解决方案,那您就在浪费我们的时间.

It is also bad manners to post the same question to two sites simultaneously. If Perl Monks doesn't come up with a working solution for you then by all means ask the question here, but you are wasting our time if, unknown to us, your problem already has a solution elsewhere.

这篇关于用于用户交互的perl脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆