【发布时间】:2011-03-08 08:01:22
【问题描述】:
我有以下脚本,每 15 天通过 cronjob 更新我的一个 FTP 密码,并在尝试后通过电子邮件发送给适当的人。它会随机失败,所以我将再次手动运行它,它会起作用。我似乎找不到哪里出错了。
该脚本正在连接到本地 mysql 数据库,获取帐户的登录名和密码,然后在 FTP 上更改该密码。在更改密码部分之前,一切都是成功的。同样是随机的,有时有效,有时无效。
谢谢!
#!/usr/bin/perl -w
#
use DBI;
use Net::FTP;
our $dbh = DBI->connect('DBI:mysql:database:127.0.0.1','user','password') or die "Aargh $!\n";
$transquery=q{SELECT dest_login,dest_password FROM list where id=123};
$sth=$dbh->prepare($transquery);
$sth->execute();
while($co=$sth->fetchrow_hashref){
$login=$co->{'dest_login'};
$pass=$co->{'dest_password'};
}
$changeresult='FAIL';
$actionlog='';
$newstring='';
$upperchars='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$lowerchars='abcdefghijklmnopqrstuvwxyz';
$allowedchars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$';
$l=length($upperchars);
$newstring.=substr($upperchars,int(rand($l)),1);
$newstring.=substr($lowerchars,int(rand($l)),1);
$l=length($allowedchars);
for ($i=0;$i<6;$i++){
$newstring.=substr($allowedchars,int(rand($l)),1);
}
print "$newstring\n";
$actionlog .= "Setting Password for $login from $pass to $newstring\n";
$username=
eval{
$ftp=Net::FTP->new('x.x.x.x',Timeout=>480,Debug=>1) or die "Error connecting FTP $!\n";
$changepassword="$pass/$newstring/$newstring";
$ftp->login($login,$changepassword) or die "Error changing password $!\n";
#If we are here, time to update the password
$changeresult='SUCCESS';
$actionlog .= "Password successfully updated\n";
$transquery=q{UPDATE list set dest_password=(?) where id=123};
$sth=$dbh->prepare($transquery);
$sth->execute($newstring);
};
if ($@) {
$actionlog = $actionlog . "$@\n";
};
if($actionlog ne ""){
#print $actionlog;
#my $send_to = "To: someone\@example.com\n";
my $send_to = "To: databaseusers\@example.com\n";
my $sendmail = "/usr/sbin/sendmail -t";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL "Reply-to: databasepassword\@example.com\n";
print SENDMAIL "Subject: Password Change Information [$changeresult]\n";
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $send_to;
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL $actionlog;
close(SENDMAIL);
$actionlog='';
}
else{
#print "Nothing done this session\n";
【问题讨论】:
-
随机失败怎么办?发送电子邮件?更改密码?
-
“更改密码错误”失败
-
总是包括
use strict; use warnings;。在您需要在 SO 上发布之前,它会捕获许多错误。 -
如果我尝试重现错误,我不会收到任何错误,这是非常随机的。大约每 15 到 30 天,它就会失败并给我发一封电子邮件。我很好奇是否有人经历过这种情况。 ftp 用户也是一个自动化的过程,会不会是在我尝试更改密码时正在传输?
-
你的 FTP 服务器有日志吗?
标签: mysql linux perl ftp debian