脚本作用

从主机上拉取数据生成log文件,再通过expect自动化交互通过sftp上传log文件到指定位置

#!/bin/bash

cur_date="`date +%Y-%m-%d`"

#收取计算机信息
echo "1.1计算机名称" >> /tmp/$(hostname)_quarterly_check_$cur_date_$cur_date.log
hostname >> /tmp/$(hostname)_quarterly_check_$cur_date.log
echo "=========================================================================" >> /tmp/$(hostname)_quarterly_check_$cur_date.log

echo "1.2操作系统版本" >> /tmp/$(hostname)_quarterly_check_$cur_date_$cur_date.log
cat /etc/redhat-release >> /tmp/$(hostname)_quarterly_check_$cur_date.log
echo "=========================================================================" >> /tmp/$(hostname)_quarterly_check_$cur_date.log

#注意需要提前安装expect
#自动上传log到SFTP
targetServerUser=infra
targetserverIP=172.16.16.1
targetServerPasswd='Password@123!'
expect << EOF
log_user 0
spawn sftp $targetserverUser@$targetServerIP
expect {
	-re "Are you sure you want to continue connecting*" {
		sleep 2
		send "yes\r"
		exp_continue
		}
	"*password:*" {
		sleep 2
		send "$targetServerPasswd\r"
		expect {
			"Hello, I'm freeFTPd*" {
				puts "sftp 连接成功,上传日志到$targetserverIP"
				send "put /tmp/*_quarterly_check_$cur_date.log /\r"
				expect {
					"*100%*" {
					puts "sftp 上传完成"
					send "exit\r"
					}
					"*Permission denied*" {
					puts "sftp 上传失败,Permission denied"
					}
				}
			}
			"*Permission denied*" {
				puts "sftp 连接失败,Permission denied"
			}
			"*timeout*" {
				puts "sftp 连接超时"
			}
		}
		}
}
EOF

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐