我想知道是否可以用 perl 编写用于创建 ftp 帐户的脚本?
我有 Unix 服务器。
答案1
几年前,我用 perl 脚本制作了一个模块来管理 vsftpd 帐户。也许我应该将其上传到 CPAN。以下是测试文件,它给出了如何使用它的想法:
#!/usr/bin/perl
use strict;
use warnings;
# test script for ftpuser.pm
use Test::More 'no_plan';
# use Test::Exception;
# if Test::Exception not available
sub dies_ok(&$) {
my $coderef = shift;
my $message = shift;
eval $coderef;
ok( $@, $message)
}
# sub not_ok
sub not_ok(&$) {
my $coderef = shift;
my $message = shift;
eval $coderef;
ok( $@, $message)
}
###
# module to test
use ftpuser;
###########################
# tests OK
# open user database
ok( my $userdb = ftpuser->open_db(), "opening user base");
# open another one
ok( my $userdb2 = ftpuser->open_db('virtual_users_login.db'), "opening user base 2");
# close db
ok( $userdb->close_db(), "closing database" );
# add a user
ok($userdb2->add(user => 'user1'), "add user");
# add user with group
ok($userdb2->add(user => 'user2', group => 'geftp' ), "add user with group");
# ajouter un utilisateur (password fourni)
ok($userdb2->add(user => 'user3', password => 'testpass'), "ajout utilisateur 3");
# add user with group and password
ok($userdb2->add(user => 'user4',group => 'geftp', password => 'testpass2'), "add user with group and password");
# delete users
for my $i ( 1..4) {
ok($userdb2->remove("user$i"), "deleting user $i");
}
###########################
# tests NOT OK
# add a user without name
dies_ok( sub{$userdb2->add(user => '')}, "add user");
# add an existing user
dies_ok( sub{$userdb2->add(user => 'user2')}, "user already exists");
# delete a non existing user
dies_ok( sub{$userdb2->remove(user => 'user1')}, "delete non existing user");