脚本作用
从主机上拉取数据生成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