如何实现SFTP自动上传下载?
- By : Will
- Category : Bash, FTP & SFTP & FTPS
Bash
1 前言
一个问题,一篇文章,一出故事。
笔者最近需要实现sftp自动上传下载,于是整理此文。
以下是笔者的自动上传下载脚本的测试环境,如果你有需要请参阅,
2 最佳实践
2.1 创建脚本
vim ~/scripts/autoSftp.exp
创建如下脚本,
#!/usr/bin/expect set sftpHost "sftp.cmdschool.org" set sftpPort "989" set sftpUser "cmdschool.org\\will" set sftpPasswd "will@passwd" spawn /usr/bin/sftp -P ${sftpPort} ${sftpUser}@${sftpHost} expect { "Are you sure you want to continue connecting" { send "yes\r" exp_continue } "Password:" { send "${sftpPasswd}\r" } } expect "sftp>" send "cd /myhome\r" expect "sftp>" send "lcd /etc\r" expect "sftp>" send "put redhat-release\r" expect "sftp>" send "lcd ~\r" expect "sftp>" send "get redhat-release\r" expect "sftp>" send "bye\r"
另外,你可能还需要安装如下软件包,
yum install -y expect
2.2 执行脚本
expect ~/scripts/autoSftp.exp
没有评论