XAMPPのFileZillaServerのユーザー管理を自動化する (Windows11)
XAMPPのFileZillaServerを使って、
学習者さんたちからファイルを送ってもらったりしている。
ただ、新しい学習サイクルが始まるたびに、
(1) ユーザーフォルダの作成
(2) ユーザー名とパスワードの設定
(3) ユーザーフォルダの権限の設定
をするのがとても大変。
で、今回、その作業を自動化してみた。
それが “remakeuser.ps1” である。
事前準備
(1) XAMPPのインストール(今回は XAMPP8.2.12を使用)
(a) XAMPPコントロールパネルは管理者権限で起動できなければならない。
これは、c:\xampp\xampp-control.exeのプロパティ—互換性で設定する。
(b) FileZillaサーバーは、サービスに登録しておく。
(PowerShellスクリプトの中で、サービスを停止、起動している)
(2) “c:\students” フォルダの作成
スクリプトは、このフォルダの中に各ユーザーのフォルダを作成する。
(3) users.csv の準備
username, password <- 1行目はそのままにして
tanaka, tanaka <- 2行目から修正
suzuki, suzuki
(4) “FileZilla Server.xml”の修正
"c:\xampp\FileZillaFTP\FileZilla Server.xml" を開いて、
<Settings>
.....
</Settings>
の下に
<Users>
</Users>
の項目を追加する。 同じディレクトリに “FileZillaServer.xml” もあるので、注意する。 ファイル名の中に 半角空白 がある方である。
今回は、このようになった。
FileZilla Server.xml
<FileZillaServer>
<Settings>
<Item name="Admin port" type="numeric">14147</Item>
<Item name="Service name" type="string">FileZillaServer</Item>
<Item name="Service display name" type="string">FileZillaServer</Item>
</Settings>
<Users>
</Users>
</FileZillaServer>
(5) remakeuser.ps1を 管理者権限で実行するためのショートカット(“FileZillaRemakeUser”)の作成
以下の内容のショートカットとする。
リンク先
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File %USERPROFILE%\Documents\FileZillaServer-remakeuser\remakeuser.ps1"
作業フォルダ
"C:\Users\user\Documents\FileZillaServer-remakeuser"
このショートカットを 管理者権限で実行することになる。
作成したスクリプト
remakeuser.ps1
# FileZilla Server.xmlから、削除するユーザーを取得して
# XMLからユーザーを削除する。
# また、それに対応するユーザーフォルダも削除する。
# このスクリプトの場所をカレントディレクトリとする。
Set-Location $PSScriptRoot
# 管理者権限チェック
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "管理者権限で実行してください"
exit 1
}
$xmlPath = "C:¥xampp¥FileZillaFTP¥FileZilla Server.xml"
Stop-Service "FileZillaServer"
Copy-Item $xmlPath "$xmlPath.bak" -Force
[xml]$xml = Get-Content $xmlPath -Encoding UTF8
$usersNode = $xml.SelectSingleNode("/FileZillaServer/Users")
# 既存ユーザー名一覧を取得
$oldUsers = $usersNode.SelectNodes("User") |
ForEach-Object { $_.GetAttribute("Name") }
# 既存ユーザーのフォルダを削除
foreach ($u in $oldUsers) {
$dir = "C:¥students¥$u"
if (Test-Path $dir) {
Remove-Item $dir
}
}
# 既存ユーザー全削除
$usersNode.RemoveAll()
# users.csvから、新しいユーザー名を取得して
# フォルダを作成し、XMLファイルを作成する。
Import-Csv users.csv | ForEach-Object {
$user = $_.username
$pass = $_.password
$homeDir = "C:¥students¥$user"
# フォルダ作成
New-Item "C:¥students¥$user" -ItemType Directory -Force
$passBytes = [Text.Encoding]::UTF8.GetBytes($pass)
# MD5でハッシュ
$hash = [System.BitConverter]::ToString(
[System.Security.Cryptography.MD5]::Create().ComputeHash($passBytes)
).Replace("-", "").ToLower()
$userNode = $xml.CreateElement("User")
$userNode.SetAttribute("Name", $user)
$optPass = $xml.CreateElement("Option")
$optPass.SetAttribute("Name", "Pass")
$optPass.InnerText = $hash
$userNode.AppendChild($optPass) | Out-Null
$optEnabled = $xml.CreateElement("Option")
$optEnabled.setAttribute("Name", "Enabled")
$optEnabled.InnerText = "1"
$userNode.AppendChild($optEnabled) | Out-Null
$perms = $xml.CreateElement("Permissions")
$perm = $xml.CreateElement("Permission")
$perm.SetAttribute("Dir", $homeDir)
foreach ($name in @(
"FileRead","FileWrite","FileDelete",
"DirCreate","DirDelete","DirList",
"DirSubdirs","IsHome"
)) {
$opt = $xml.CreateElement("Option")
$opt.SetAttribute("Name", $name)
$opt.InnerText = "1"
$perm.AppendChild($opt) | Out-Null
}
$perms.AppendChild($perm) | Out-Null
$userNode.AppendChild($perms) | Out-Null
$usersNode.AppendChild($userNode) | Out-Null
Write-Host "作成: $user"
}
$xml.Save($xmlPath)
Start-Service "FileZillaServer"
Write-Host "全ユーザー再作成 完了"
実行
今回は、”%HOME%\Documents\LaLa\FileZillaServer-remakeuser” という
フォルダで作業した。
この中に入っているのは、
remakeuser.ps1
FileZillaRemakeUser.lnk
users.csv
の3つである。
別の名前のフォルダで作業するなら、ショートカットの中のフォルダを指定
部分を修正しなければならない。
このショートカットを右クリックして” 管理者として実行”で、実行される。
カテゴリー: memo, Windows, XAMPP
タグ: FileZilla, FileZillaServer, ftp, ftpサーバー
カウント: 111
My開発メモ