使用Shell脚本实现SFTP下载文件

一.生成密钥对
ssh-keygen –d
Generating public/private dsa key pair.


Enter file in which to save the key (/home/local_user/.ssh/id_dsa): (直接按回车不要输入任何东西。注意这个路径/home/local_user/.ssh/id_dsa)
# 按回车保存为: /home/local_user/.ssh/id_dsa,即当前用户local_user的私钥


Enter passphrase (empty for no passphrase):  (直接按回车不要输入任何东西)
# 按回车,表示读取密钥时不需要密钥的密码


Enter same passphrase again:  (直接按回车不要输入任何东西)
# 确认密钥的密码,必须和上面的输入相同


Your identification has been saved in /home/local_user/.ssh/id_dsa.
# 私钥保存信息


Your public key has been saved in /home/local_user/.ssh/id_dsa.pub.
# 公钥保存信息


The key fingerprint is:
ec:41:e8:08:38:0b:f8:1e:bc:92:98:32:fc:d7:69:7d ...
# 密钥指纹




二.分发公钥


为了使用密钥,必须将公钥分发到欲登录的远程服务器上,这里远程服务器记为remote_hos
t,欲登录的远程用户记为remote_user


1.copy公钥到欲登录的远程服务器的远程用户的家目录下,例如:


copy id_dsa.pub到remote_host:/home/remote_user/.ssh/(注意这个路径)


若目录/home/remote_user/.ssh/不存在,请先创建之.


2.将copy来的公钥文件改名为authorized_keys


3.修改公钥文件的访问权限


chmod 644 authorized_keys






示例
date=` date +%Y-%m-%d `
##########################################
#                 下载文件               #
##########################################
#sftp -oPort=ssh端口号 用户名@IP地址 << EOF
sftp -oPort=29 root@****** << EOF
#cd 服务器文件的存放路径
cd /var/MeMediaBackup/
# lcd 本地文件存放路径
lcd /backup/memediadbbackup/
#-get 服务器文件的存放路径下的文件名
-get memediadb$date.tar.gz
##########################################
#            减压并导入数据库            #
##########################################
tar -zxvf memediadb$date.tar.gz
/usr/bin/mysql -u root -pmemediahegg.0 -h ****** memediadb < /backup/memediadbbackup/memediadb$date.sql
##########################################
#          删除减压后的sql文件           #
##########################################
rm -rf memediadb$date.sql
quit
EOF
posted @   silentmuh  阅读(1606)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
Live2D
欢迎阅读『使用Shell脚本实现SFTP下载文件』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Welcome Home - Radical Face
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Ben P. Cooper

作曲 : Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

Was never much but we've made the most

Welcome home

Ships are launching from my chest

Some have names but most do not

If you find one,please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

All my nightmares escaped my head

Bar the door, please don't let them in

You were never supposed to leave

Now my head's splitting at the seams

And I don't know if I can

Here, beneath my lungs

I feel your thumbs press into my skin again

点击右上角即可分享
微信分享提示