Perl与系统管理-2

本文探讨了Perl在系统管理中的应用,包括YAML文件解析、数据库操作与配置,以及如何利用Perl进行高效的系统任务自动化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Perl与系统管理-2
  1. YAML文件的解析
    官网 https://yaml.org/

可以使用的模块

  • Config::YAML
  • YAML::XS
  1. 数据库支持
引用模块
use DBI;

连接数据库
my $database='sysadm';
my $dbh=DBI->connect("DBI:mysql:$database", $username, $pw);
die "Unable to connect: $DBI::errstr\n" unless (defined $dbh);
或者
my $dbh=DBI->connect("DBI:mysql:$dbname", $username, $password,
		{RaiseError=>1, ShowErrorStatement=>1});

执行SQL
my $results=$dbh->do(q{
				UPDATE hosts
				SET bldg='Main'
				WHERE name='bendir'});
die "Unable to perform update:$DBI::errstr\n" unless (defined $results);

查询
my $sth=$dbh->prepare(q{SELECT * from hosts}) or
	die "Unable to prep our query: ".$dbh-errstr."\n";
my $rc=$sth->execute or 
	die "Unable to execute our query: ".$dbh-errstr."\n";

支持变量内插
$sth=$dbh->prepare(qq{SHOW COLUMNS FROM $table FROM $db}); #qq相当于双引号
$sth->execute;

占位符?
my @machines=qw(bendir shimmer sander);
my $sth=$dbh-prepare(q{SELECT name, ipaddr FROM hosts WHERE name=?});
foreach my $name (@machines){
	$sth->execute($name);
	...
}

多个占位符
my $sth=$dbh-prepare(q{SELECT name, ipaddr FROM hosts WHERE (name=? AND bldg=? AND dept=?)});
$sth->execute($name, $bldg, $dept);

获取结果集
#假定已经完成了查询语句:SELECT first, second, third FROM table
my $first;
my $second;
my $third;

#建立查询结果的绑定关系
#绑定到变量
$sth->bind_col(1, \$first);
$sth->bind_col(2, \$second);
$sth->bind_col(3, \$third);
或者
$sth->bind_columns(\$first, \$second, \$third);

#绑定到数组
$sth->bind_columns(\(@array)); 
#$array[0] 第一列
#$array[2] 第二列

#绑定到哈希
$sth->bind_col(1, \$hash{first});
$sth->bind_col(2, \$hash{second});

获取数据集的下一行数据
while($sth->fetch){
	#绑定结果到变量,数组,哈希
}

关闭数据库连接
$dbh->disconnect;

密码的输入可以考虑使用Term::Readkey。
DBI的官网 https://dbi.perl.org/

  1. 使用重定向<<输入多行文本
my $ahtml=<<HTML;
<!DOCTYPE html>
<html>
	<head>
		<style>
  			span.red {color:red;}
		</style>
	</head>
	<body>
		<h1>My <span class="red">Important</span> Heading</h1>
	</body>
</html>
HTML
  1. 开源的反垃圾邮件程序
    Apache SpamAssassin
    对应模块Mail::SpamAssassin

  2. LDAP(Lightweight Directory Access Protocol)

LDAP条目数据结构

LDAP是面向对象的。
RDN(Relative Distinguished Name)可以用+组合。
DN是由RDN的序列组成的(中间以逗号或者分号连接),这个序列能够指明去往根条目的道路。

cn=Robert Smith, l=main campus, ou=CSS, o=Hogwarts School, c=US
cn=rsmith, ou=system, ou=people, dc=ccs, dc=hogwarts, dc=edu

回归至树根来产生DN

最细节的条目最先。
主流版本:v2 和v3
LDAP是X.500的简化版。
相关模块
Net::LDAPapi
Net::LDAP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值