MENU

自动交互脚本ftp,ssh,scp

转2015年笔记
http://www.attacker.club/article/1

自动交互脚本ftp,ssh,scp

ftp 自动登录上传下载

#!/bin/bash
yum install ftp -y
ftp -ivn  10.139.xx.xx <<EOF
user  ftp  password #user后面跟账号密码
lcd /localdir      #切换本地目录
cd /remotedir/     #切换ftp目录
put backup.sql   #上传文件
get access.log   #将文件下载到本地
rm 
bye              
EOF
rm  access.log  
exit

ssh 自动登陆 执行命令

#!/bin/bash
passwd='password'
/usr/bin/expect <<EOF
set time 30
spawn ssh root@192.168.1.220
expect { "*yes/no" { send "yes\r"; exp_continue
} "*password:" { send "$passwd\r" }
}
expect "*#"
send "/root/auto.sh\r"
exec sleep 3
send "exit\r"
expect eof
EOF
pkill goapp
tar zxf /data/z.tar.gz -C /home/go/z
rm /data/zcm.tar.gz -rf
cd /home/go/z && nohup ./goapp &
echo "################# update success #################"

scp: 自动登录上传下载

#!/bin/bash
passwd='123456'
#定义密码

/usr/bin/expect <<-EOF
set time 300
spawn  scp -P 2201  /home/go/z/goapp.tar.gz  root@10.139.1.1:/data
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "$passwd\r" }
}
expect eof
EOF

rm -rf /home/go/z/goapp.tar.gz
删除本地文件
exit

MYSQL数据库备份

#!/bin/bash
#Author:  LJ
#Email:   admin@attacker.club
#Last Modified: 2018-12-23 22:49:12

HOMEDIR=/data/rds_backup #备份路径
LogFile=result.log #备份日志


Install_ftp_client()
{
if [ ! -f /bin/ftp ];then
yum install ftp -y
# 判断是否有ftp
fi

which mysqldump  &> /dev/null || yum install mysql -y
# 判断是否有mysqldump命令
}


Backup_rds()
{
#mysqldump -h10.91.xx.xx  -uroot -ppasswd   db table1 ${HOMEDIR}/$Date.sql
touch ${HOMEDIR}/$Date.sql
#备份数据库
}

Upload_ftp()
{
#ftp自动传送
ftp -ivn  122.224.xx.xx <<EOF
user   ftp   passwod
lcd ${HOMEDIR}
cd /mysql/
put $Date.sql
bye
EOF
#登陆ftp主机;lcd切换本地路径,cd远程路径,put上传
}





#########################  执行  ###########################
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`


Install_ftp_client

Backup_rds
Last=`date +"%Y年%m月%d日 %H:%M:%S"` && echo "备份开始:$Begin 备份结束:$Last  success"  >> ${HOMEDIR}/$LogFile

Upload_ftp
echo "完成上束:$(date +"%Y年%m月%d日 %H:%M:%S")  success"  >> ${HOMEDIR}/$LogFile

#echo "find  ${HOMEDIR}"
find ${HOMEDIR}  -name "*.sql" -mtime +3  -exec rm -rvf {} \;
#保留最近3天的备份
exit

免秘钥ssh,scp

ssh-keygen -t rsa -P ''
# 生成ssh密钥
ssh-copy-id  -i 10.0.1.32
# 推送公钥到目标主机

bash /root/ssh.sh
免密钥scp互传和ssh登录例子

#!/bin/bash

scp /root/ssh.sh  root@10.0.1.32:/root
# 推送本地文件到 目标

/usr/bin/expect <<EOF
spawn ssh 10.0.1.32

expect "*#"
send "touch /root/new.txt\r"
exec sleep 3
send "exit\r"

expect eof
EOF
# ssh远程执行命令


scp -P22 root@10.0.1.32:/root/new.txt /root/old.txt
# 拷贝目标文件到 本地
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code
Leave a Comment