2013-02-25 153 views
2

我在尝试重写为IIS创建的Windows FTP服务器配置脚本,现在我们正在尝试为Filezilla Server获得类似的工作。从嵌套的批处理脚本中执行PHP脚本

结构如下,我们有一个批处理文件,它是另一个批处理文件的循环,所以我们可以批量配置我们的FTP网站。我目前正在尝试使用的这个批处理文件包含一行代码,用于执行PHP脚本以在Filezilla中设置FTP用户名和密码以及执行其他一些整洁的事情。

现在,运行CreateIIStmp.bat var1 var2工作得很好。但是执行BatchCreateIIS.bat似乎会跳过执行php脚本,或者php脚本失败。 (我只是研究如何将一些错误处理放入PHP脚本中以捕获任何错误并显示它,但我不是(PHP)开发人员/编码人员,因此我会在更新此错误时进行更新。)

这里是一个精简版有什么我谈论:

最初的批处理文件BatchCreateIIS.bat:

@Echo off 

for /f %%X in (NewWebsiteEntries.txt) do start cmd.exe /c "CreateIIStmp.bat %%X %%X" 

echo ... 
echo *** BATCH PROCESS HAS BEEN COMPLETED *** 
pause 

的CreateIIStmp.bat:

@ echo off 
C:\php5\php-win.exe -f filezilla-user-script.php -- %1 %2 
pause 
@echo on 

NewWebs iteEntries.txt:

somedomain.com ftpuname 
somedomain2.net ftpuname2 

的FileZilla中,用户的script.php:

<?php 

$xmlfolder = 'C:/Program Files (x86)/FileZilla Server/'; 
$xmlfilename = 'FileZilla Server.xml'; 

$ftpRoot = 'C:/inetpub/wwwroot/'; 
$ftpDocumentation = 'C:/Documentation ftp server/'; 

$xmlfile = $xmlfolder . $xmlfilename; 
$xmlbackupfile = $xmlfolder . @date("Y-m-d-H-i-s") . '_FileZilla_Server.xml'; 


// Copy Config for backup 
createXMLbackup($xmlfile,$xmlbackupfile); 



//Load XML file 
$xml = simplexml_load_file($xmlfile); 

$msg = "Allowed usernames: 20 characters out of a...z A...Z 0...9 _ \n\nPlease input username (Ctrl+C to quit)"; 

// Copy Config for backup before each change, too. 
createXMLbackup($xmlfile,$xmlbackupfile); 

    echo "\n\n"; 
    $input = ($argv[2]); 
    echo "\n"; 
//echo 'Userinput: ' . $input . "\n"; 
    $isvalid = isUserID($input); 
//var_dump($isvalid); 

    if($isvalid) 
    { 

     $ftpUserFolder = $ftpRoot . ($argv[1]); 

     if ((file_exists($ftpUserFolder) && is_dir($ftpUserFolder))) 
     { 
      echo "The directory $ftpUserFolder exists.\nPlease select another user name.\n"; 
     } 
     else 
     { 
      //echo "The directory $ftpUserFolder does not exist\n"; 

      if(!check_user_exists($xml,$input)) 

      { 
       echo "Adding user $input...\n"; 

       if (!mkdir($ftpUserFolder)) 
       { 
        die("Could not create directory $ftpUserFolder \n"); 
       } 
       else 
       { 
        echo "Directory $ftpUserFolder created.\n"; 
       } 

       $password = generatePassword(); 
       //echo 'Password: ' . $password . "\n"; 

       $user = $xml->Users->addChild('User'); 
       $user->addAttribute('Name', $input); 

       $option = $user->addChild('Option', md5($password)); 
       $option->addAttribute('Name', 'Pass'); 

       $option = $user->addChild('Option'); 
       $option->addAttribute('Name', 'Group'); 

       $option = $user->addChild('Option', '0'); 
       $option->addAttribute('Name', 'Bypass server userlimit'); 

       $option = $user->addChild('Option', '0'); 
       $option->addAttribute('Name', 'User Limit'); 

       $option = $user->addChild('Option', '0'); 
       $option->addAttribute('Name', 'IP Limit'); 

       $option = $user->addChild('Option', '1'); 
       $option->addAttribute('Name', 'Enabled'); 

       $option = $user->addChild('Option', 'none'); 
       $option->addAttribute('Name', 'Comments'); 

       $option = $user->addChild('Option', '0'); 
       $option->addAttribute('Name', 'ForceSsl'); 

       $filter = $user->addChild('IpFilter'); 
       $filter->addChild('Disallowed'); 
       $filter->addChild('Allowed'); 

       $permissions = $user->addChild('Permissions'); 
       $permission = $permissions->addChild('Permission'); 

       $permission->addAttribute('Dir', str_replace("/","\\",$ftpUserFolder)); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'FileRead'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'FileWrite'); 

       $option = $permission->addChild('Option', '0'); 
       $option->addAttribute('Name', 'FileDelete'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'FileAppend'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'DirCreate'); 

       $option = $permission->addChild('Option', '0'); 
       $option->addAttribute('Name', 'DirDelete'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'DirList'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'DirSubdirs'); 

       $option = $permission->addChild('Option', '1'); 
       $option->addAttribute('Name', 'IsHome'); 

       $option = $permission->addChild('Option', '0'); 
       $option->addAttribute('Name', 'AutoCreate'); 

       $speed = $user->addChild('SpeedLimits'); 
      $speed->addAttribute('DlType', '1'); 
      $speed->addAttribute('DlLimit', '10'); 
      $speed->addAttribute('ServerDlLimitBypass', '0'); 
      $speed->addAttribute('UlType', '1'); 
      $speed->addAttribute('UlLimit', '10'); 
       $speed->addAttribute('ServerUlLimitBypass', '0'); 
       $speed->addChild('Download'); 
       $speed->addChild('Upload'); 

       $rv = $xml->asXML($xmlfile); 
       //echo $rv . "\n"; 
       if(!$rv) 
       { 
        die('SimpleXML could not write file'); 
       } 


//$newentry = $xml->addChild('element', iconv('ISO-8859-1', 'UTF-8', $write)); 
//The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() 
//to work with texts in ISO-8859-1 encoding or Iconv for other encodings. 
//make human readable, parse using DOM function 
//otherwise everything will be printed on one line 

       if(!file_exists($xmlfile)) die('Missing file: ' . $xmlfile); 
       else 
       { 
        $dom = new DOMDocument("1.0","ISO-8859-1"); 
        //Setze die Flags direkt nach dem Initialisieren des Objektes: 
        $dom->preserveWhiteSpace = false; 
        $dom->formatOutput = true; 

        //$dl = @$dom->load($xmlfile); // remove error control operator (@) to print any error message generated while loading. 
        $dl = $dom->load($xmlfile); // remove error control operator (@) to print any error message generated while loading. 
        if (!$dl) die('Error while parsing the document: ' . $xmlfile); 
        //echo $dom->save($xmlfile) . "\n"; 
        if(!$dom->save($xmlfile)) 
        { 
         die('DOMDocument could not write file'); 
        } 
       } 

//Create documentation 

       $docuFile = $ftpDocumentation . $input . '.txt'; 
       //echo $docuFile . "\n"; 

       $docuString = "Username: " . $input . "\n"; 
       $docuString = $docuString . "Password: " . $password . "\n"; 
       $docuString = $docuString . "Folder: " . str_replace("/","\\",$ftpUserFolder) . "\n"; 
       $docuString = $docuString . "Date: " . @date("d.m.Y") . "\n"; 
       // $docuString = $docuString . "\n"; 
       // $docuString = $docuString . "Direct link:\n"; 
       // $docuString = $docuString . "ftp://" . $input . ":" . $password . "@ftp.yourcompany.com"; 




       $handleDocuFile = fopen($docuFile, "wt"); 
       if(!$handleDocuFile) 
       { 
        die('Could not fopen docu file'); 
       } 

       $rv = fwrite($handleDocuFile, $docuString); 
       if(!$rv) 
       { 
        die('Could not fwrite docu file'); 
       } 

       // Close xml file 
       $rv = fclose($handleDocuFile); 
       if(!$rv) 
       { 
        die('Could not fclose docu file'); 
       } 
       echo "Documentary file written.\n"; 

       $ftpExecutable = "\"C:\\Program Files (x86)\\FileZilla Server\\FileZilla server.exe\" /reload-config"; 

       $command = $ftpExecutable; 

       $last_line = system($command, $retval); 

       echo ("Filezilla reloaded, user active.\n"); 

       echo ("Close Notepad to add another user or quit.\n"); 

       $command = "C:\\Windows\\System32\\notepad.exe $docuFile"; 
       $last_line = system($command, $retval); 

      } 
      else 
      { 
       echo "Username $input already exists...\n"; 

      } 

     } 

    } 
    else 
    { 
     echo "Username $input is invalid\n"; 
    } 



function check_user_exists($xml,$username) 
{ 
    $children=$xml->Users->children(); 

    foreach($children as $child) 
    { 
     if ($child->getName()=='User') 
     { 

      foreach($child->attributes() as $attributes) 
      { 
       if(trim($attributes) == trim($username)) 
       { 
        echo "Username $username already exists... \n"; 
        return true; 
       } 

      } 

     } 


    } 

    return false; 
} 




function isUserID($username) 
{ 
    //return preg_match('/^\w{2,20}$/', $username); 
    return preg_match('/^[A-Za-z0-9][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/', $username); 
} 

function isValid($str) 
{ 
    //return !preg_match('/[^A-Za-z0-9.#\\-$]/', $str); 
    return !preg_match('/[^A-Za-z0-9\_\-]/', $str); 
} 


function getInput($msg) 
{ 
    fwrite(STDOUT, "$msg: "); 
    $varin = trim(fgets(STDIN,20)); 
    return $varin; 

    //$input = fgets($fr,128);  // read a maximum of 128 characters 
} 

function createXMLbackup($xmlfile,$xmlbackupfile) 
{ 
    // Copy Config for backup 
    $rv = copy($xmlfile,$xmlbackupfile); 
    if(!$rv) 
    { 
     die('Problem creating xml backup file'); 
    } 
    echo "\nBackup file created\n"; 
} 


function generatePassword ($length = 15) 
{ 

    // start with a blank password 
    $password = ""; 

    $possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

    // we refer to the length of $possible a few times, so let's grab it now 
    $maxlength = strlen($possible); 

    // check for length overflow and truncate if necessary 
    if ($length > $maxlength) 
    { 
     $length = $maxlength; 
    } 

    // set up a counter for how many characters are in the password so far 
    $i = 0; 

    // add random characters to $password until $length is reached 
    while ($i < $length) 
    { 

     // pick a random character from the possible ones 
     $char = substr($possible, mt_rand(0, $maxlength-1), 1); 

     // have we already used this character in $password? 
     if (!strstr($password, $char)) 
     { 
      // no, so it's OK to add it onto the end of whatever we've already got... 
      $password .= $char; 
      // ... and increase the counter by one 
      $i++; 
     } 

    } 

    // done! 
    return $password; 

} 
?> 

我环顾四周,并有一些地方是推荐在调用PHP这样的前使用@:@C:\php5\php-win.exe -f filezilla-user-script.php -- %1 %2但那不起作用,而且我可以看到我的脚本确实有效,而不是以这种嵌套形式。

也许有一种方法来解决这个问题,我错过了?或者我不知道如何在嵌套批处理脚本中执行脚本?

回答

0

在你BatchCreateIIS.bat,当你使用这个:

start cmd.exe /c "CreateIIStmp.bat %%X %%X" 

父CMD.EXE不会等待CreateIIStmp.bat(和子进程)的每个实例来完成。由于您似乎正在写入相同的XML文件,因此最后写入的DOM很可能会持续存在。相反,试试这个:

call "CreateIIStmp.bat %%X %%X" 
+0

谢谢你的回应,但是,最终,我买了一个许可证CoreFTP服务器和我们这样做手工了。 作为一种时间效率的解决方案并不是很好,但它对我们来说很有用,这是为了支持一个传统系统,该传统系统很快将在正在进行的生产中关闭,为此我正在设置这个系统。 – 2013-07-12 11:40:49