我正在编写perl脚本以将目录复制到远程计算机。我正在使用Expect模块来期望密码并发送密码。
但是出现如下语法错误
sh:-c:第0行:意外令牌系统附近的语法错误(scp -r / home / user1 / data / 192.168.0.1:/tmp/)'
scp'
sh: -c: line 0:
perl脚本是:
use Expect;
$Expect::Debug=2;
$cmd="scp -r /home/user1/data/ 192.168.0.1:/tmp/";
my $exp1=Expect-> spawn("system($cmd)") or die print "cannot spwan process\n";
$exp1-> expect(10,["[email protected]'s password: "=>sub{ $exp1-> send("usr1234\r");}]);
先感谢您
我不完全确定您的脚本出了什么问题(spawn(system($cmd))
当然是execpt ,但是已经在注释中解决了。也许=>
不需要了吗?或者您需要用\n
而不是密码结尾\r
吗?无论如何,这很简单我使用了以下类似的方法,效果很好(请注意,这$rsync
是我的Expect对象)。
$rsync=Expect->spawn ("rsync $remUser\@$remIp:$remFiles $locFolder");
if (! defined($rsync))
{
die "spawning failed. Probably fork failed: $!";
}
my $matchStatus = $rsync->expect(100, '-re', '(.+)');
if (! defined $matchStatus)
{
die "Nothing got returned within 100 seconds. $!";
}
my $spawned = 0;
while ($spawned eq 0)
{
my $return = $rsync->match();
if ($return =~ m/command not found/)
{
die "Command not found, there appears to be no rsync... $!";
}
elsif ($return =~ m/RSA key fingerprint is/)
{
$rsync->send ("yes\n");
sleep(5);
}
elsif ($return =~ m/password/)
{
$rsync->send($password,"\n");
$spawned = 1;
}
else
{
print "rsync answered:\n\n";
print "$return \n";
print "I did not expect that. EXIT\n";
exit(1);
}
}
print "Sent Pw...\n";
这样可以处理更多不同的答案,并且可以更轻松地对其进行修改和调试。但是显然要长得多。我更喜欢露骨而不是优雅。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句