簡體   English   中英

更改FTP密碼的腳本

[英]Script to change FTP password

我有以下腳本,可以通過cronjob每15天更新我的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";

USUW可能會告訴您一些信息。 use strict; use warnings;

有打印嗎?

在開始時,您不需要在DBI部分進行太多錯誤檢查,也許您會遇到連接錯誤。 AIX盒曾經出現過這樣的問題,即無法確定系統是否正在使用客戶機端口。 發生這種情況時,它將無法連接到數據庫。

最后,通過檢查該特定代碼( Errno::EADDRINUSE )的$OS_ERROR (aka $! ),然后等待Errno::EADDRINUSE試,以指數衰減(等待2秒,然后4,然后8 ..),解決了腳本的問題。 )。

如果您的腳本“死於某種原因”,那么腳本可以告訴您該原因很重要。 我將調查正在使用的各個模塊中的錯誤報告主題。

例如Net::FTP允許您傳遞Debug => 1開關,然后您將看到整個對話。

而且我知道DBI還有很多其他功能,您可以在其中獲得錯誤報告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM