2

我正在尝试重写为 IIS 创建的 Windows FTP 服务器配置脚本,现在我们正在尝试为 Filezilla Server 提供类似的工作。

结构是这样的,我们有一个批处理文件,它是另一个批处理文件的 for 循环,所以我们可以批量配置我们的 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

NewWebsiteEntries.txt:

somedomain.com ftpuname
somedomain2.net ftpuname2

文件zilla-user-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 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // 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但这不起作用,我可以看到我的脚本确实有效,只是不是以这种嵌套形式。

也许我缺少解决这个问题的方法?或者我不知道在嵌套批处理脚本中执行脚本的事情?

4

1 回答 1

0

在您的 BatchCreateIIS.bat 中,当您使用它时:

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

父 cmd.exe 不会等待 CreateIIStmp.bat(和子进程)的每个实例完成。由于您似乎正在写入同一个 XML 文件,因此最后写入的 DOM 很可能仍然存在。而是试试这个:

call "CreateIIStmp.bat %%X %%X"
于 2013-07-11T10:37:16.500 回答