二、TFTP+H3C
TFTP(Trivial File Transfer Protocol)是一种简单的文件传输协议,通常用于在计算机网络中传输小文件,如配置文件、固件和其他小型数据文件。TFTP被设计成一种轻量级的协议,因此相对于其他文件传输协议(如FTP)而言,功能较少但更易于实现。安装tftp
关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config && setenforce 0
通过yum安装tftp服务器
yum -y install xinetd tftp tftp-server
安装了expect软件包,用于自动化交互式的命令行会话
yum -y install expect
创建TFTP主目录
mkdir -p /data/tftpboot
修改主目录权限
chmod 777 /data/tftpboot
修改tftp主配置文件
vi /etc/xinetd.d/tftp
修改后的配置文件如下
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /data/tftpboot -c
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
server_args = -s /data/tftpboot -c # 注意这行,如果允许上传,一定要加上参数 -c
disable = no #这行默认为yes,改成no,允许
启动tftp服务并设置开机自启
systemctl start xinetd.service && systemctl enable xinetd.service
#查看tftp是否启动
netstat -ntlup |grep 69
netstat -a |grep tftp
如果有启动iptables,需要放行端口
iptables -A INPUT -p udp --dprot 69 -j ACCEPT
交换机备份
<1>手动备份
登录交换机,执行命令:tftp 172.16.0.10 put startup.cfg 192.168.100.1.cfg
tftp 172.16.0.10 put startup.cfg 192.168.100.1.cfg
<2>脚本自动备份
创建脚本文件
mkdir -p /usr/local/src/srcipts
cd /usr/local/src/srcipts
vi sw2_back.sh
复制粘贴以下脚本:
#!/bin/bash
# 交换机列表,每个元素格式为 "交换机IP"
switches=(
"192.168.100.1"
"192.168.100.2"
"192.168.100.3"
"192.168.100.5"
"192.168.100.10"
"192.168.100.11"
"192.168.100.12"
"192.168.100.13"
"192.168.100.14"
"192.168.100.15"
"192.168.100.16"
"192.168.100.20"
"192.168.100.21"
"192.168.100.22"
"192.168.100.23"
"192.168.100.24"
"192.168.100.25"
"192.168.100.26"
"192.168.100.27"
"192.168.100.28"
"192.168.100.30"
"192.168.100.31"
"192.168.100.40"
"192.168.100.41"
"192.168.100.50"
"192.168.100.51"
"192.168.100.52"
"192.168.100.60"
"192.168.100.63"
"192.168.100.64"
"192.168.100.65"
"192.168.100.66"
# 可以继续添加更多交换机
)
# TFTP服务器的信息
tftp_server="172.16.0.10"
backup_directory="/data/tftpboot"
# 账号密码
username="交换机ssh登录账户"
password="登录密码"
for switch_ip in "${switches[@]}"; do
backup_filename="${switch_ip}.cfg"
expect << EOF
spawn ssh $username@$switch_ip
expect {
"Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect "#"
send "tftp $tftp_server put startup.cfg $backup_filename\r"
expect {
"100%" { exp_continue }
"#" { send "quit\r" }
}
expect eof
EOF
# 检查备份是否成功
# tftp_status=$(tftp $tftp_server -c get $backup_filename $backup_directory/$backup_filename 2>&1)
# if [ $? -eq 0 ]; then
# echo "交换机 $switch_ip 备份成功"
# else
# echo "交换机 $switch_ip 备份失败: $tftp_status"
# fi
done
脚本赋权
chmod +x sw2_back.sh
手动执行脚本
sh sw2_back.sh
查看执行情况:
ll /data/tftpboot/
查看172.16.0.10的备份日志
cat /data/log/h3c/switch_log | grep 172.16.0.10
定时备份,使用Crontab 表达式做定时执行脚本
编辑cron表
crontab -e
打开文本编辑器,添加定时任务:
每周五凌晨1点定时执行脚本
0 1 * * 5 sh /usr/local/src/srcipts/sw2_back.sh > /dev/null 2>&1