安装并配置一个本地TFTP服务的shell script
说明:
1、适用于CentOS/RHEL 5.x/6.x
2、建议系统已经安装并配置好yum,repository也配置正确
3、如果没有yum,请在脚本所在的文件夹里提前放好xinetd*.rpm和tftp-server*.rpm这两个安装包,当脚本判断yum无法使用时就会直接从脚本所在的目录下寻找并安装这两个所需要的rpm包,若yum和rpm都安装失败,脚本直接退出!
4、无论是否启动了iptables,都会判断这条 -I INPUT -p udp --dport 69 -j ACCEPT 规则是否存在,若不存在则插入此规则到第一条中;若存在,则不做任何操作,这样做的目的完全是为了确保默认的tftp(UDP 69号端口)服务不被iptables所阻挡
5、当检测到SELinux的状态为Enforcing时,会递归设置/tftpboot文件夹中所有的东西都能被tftp这个服务的进程所读写
6、如果判断tftp-server已经正常安装,则只会执行STARTTFTP模块里的指令
[Bash shell] 纯文本查看 复制代码 #!/bin/bash
#Author: kashu
#Date: 2013-07-03
#Description:Install and configure local tftp server
#User has insufficient privilege.
test "$UID" -ne "0" && echo "You're not root." && exit 1
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export PATH
TFTP_PATH=/etc/xinetd.d/tftp
#Start tftp service
STARTTFTP(){
if [ -d "/tftpboot" ];then
chmod 777 /tftpboot
elif [ ! -d "/tftpboot" ];then
mv /tftpboot /tftpboot.bak &> /dev/null
mkdir -m 777 /tftpboot &> /dev/null
else :
fi
sed -i 's:server_args.*:server_args\t\t= -s /tftpboot -c:g' "$TFTP_PATH" &> /dev/null
sed -i 's:disable.*:disable\t\t\t= no:g' "$TFTP_PATH" &> /dev/null
if ! grep -i "server_args" < "$TFTP_PATH" &> /dev/null; then
sed -i '10i \\tserver_args\t\t= -s /tftpboot -c' "$TFTP_PATH"
elif ! grep -i "disable" < "$TFTP_PATH" &> /dev/null; then
sed -i '10i \\tdisable\t\t\t= no' "$TFTP_PATH"
else :
fi
#Insert an ACCEPT rule
TFTP69=$(iptables -nL|grep ^'ACCEPT'|grep udp|sort -u|awk '/dpt:69/{print $4}'|grep '0.0.0.0/0')
if [ "$TFTP69" != "0.0.0.0/0" ]; then
iptables -I INPUT -p udp --dport 69 -j ACCEPT &> /dev/null
fi
#Configure SELinux
if [ "`getenforce`" == "Enforcing" ];then
setsebool -P tftp_anon_write=1 &> /dev/null
restorecon -R /tftpboot &> /dev/null
fi
service xinetd restart
}
#Use yum to install tftp-server
YUMTFTP(){
yum install tftp-server -y
}
#Use rpm to install tftp-server
RPMTFTP(){
rpm -hiv ./xinetd*.rpm
rpm -hiv ./tftp-server*.rpm
}
#Check if the tftp-server is there.
if ! rpm -qi tftp-server &> /dev/null; then
#Check if the yum is executable.
if [ -x "/usr/bin/yum" ]; then
if yum list tftp-server &> /dev/null; then
YUMTFTP
#If yum is not executable,then try to use rpm to install tftp-server.
else
echo "The repository configuration errors."
echo "Try to use rpm to install tftp-server."
RPMTFTP
fi
#Exit while installation failed.
if [ "$?" -ne "0" ]; then
echo "Installation Failed."
exit 1
else
STARTTFTP
fi
else
echo "YUM ins't installed. Try to use rpm to install tftp-server."
RPMTFTP
if [ "$?" -eq "0" ]; then
STARTTFTP
else
echo "Installation Failed."
exit 1
fi
fi
elif rpm -qi tftp-server &> /dev/null; then
#install xinetd and tftp-server
STARTTFTP
else :
fi
此脚本在RHEL 5.4、CentOS6.3中实测通过。
如有错误或您有什么好的建议,请不要犹豫,及时通知我吧,谢谢。
|