最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】命令行中ftp操作上传到远程服务器crifan.com

crifan.com crifan 3526浏览 0评论

已经在Mac中折腾完毕了gitbook的环境,本地也可以用命令去生成对应的website,pdf等内容。

且已经实现了makefile去自动化build,和zip压缩打包。

之前是通过FileZilla去实现文件上传到自己的Linode的服务器中对应的路径

然后再去SecureCRT的SSH远程连接中去解压缩,删除旧文件,移动文件等操作的:

现在希望:

可以在Mac的命令行下Makefile或shell中实现这些操作:

  • 通过sftp上传zip文件

  • 解压缩

  • 删除旧文件

  • 移动文件

shell sftp 命令 上传

linux下如何使用sftp命令 – BradyChen – 博客园

手把手教你使用 SFTP 安全地传输文件 | Linux Story

技术|如何在 Linux 中使用 sFTP 上传或下载文件与文件夹

如何在Linux中使用sFTP上传或下载文件/目录

linux 终端下上传下载命令 sftp,scp – Yzone

XShell 4使用sftp上传下载文件命令 – CSDN博客

现在要去试试,Makefile中,如何实现交互式的操作sftp,是否可行

makefile interactive sftp

makefile interactive input

makefile interactive shell

Including some SFTP commands in a Makefile – Stack Overflow

好像是可以利用

| 管道

去实现 交互式操作,接着连续输入指令去操作

好像也可以:

用ncftp

用scp?

Ubuntu Manpage: scp — secure copy (remote file copy program)

ncftpput

NcFTP Client

Deploy With FTP, SFTP, SCP, RSYNC, and SSH | Codeship Documentation

好像还有个:

lftp

从传输效率来说:

不建议把本地多个碎片文件一个个上传,

还是压缩后再上传,速度会快些

所以,上传后就需要执行unzip rm mv等操作,

-》所以貌似只有交互式的运行执行远程命令的sftp比较合适。

貌似lftp也可以?

Simple Ways to Send Multiple Line Commands Over SSH

Uploading data files to Kalkyl and creating the Makefile – BILS Wiki

gnu make – read input variable in makefile and set variable upon it`s name – Stack Overflow

通过行末加上:

;\

也可以实现多行命令连续执行?

所以还是先去看看sftp命令:

Ubuntu Manpage: sftp — secure file transfer program

sftp命令详解

SFTP命令详解 – CSDN博客

linux下FTP、SFTP命令详解 – FreeStyle的日志 – 网易博客

但是貌似不能直接登录?还需要手动输入密码?

linux sftp 登录

远程服务器登录以及使用scp、sftp传输文件 – CSDN博客

科学网—Linux sftp用法 – 余慷的博文

linux sftp login with password

shell – How to run the sftp command with a password from Bash script? – Stack Overflow

  • 可以借用lftp和sftp实现这个效果

    • 貌似最好用

  • 或者用expect

How can you do a one liner with sftp to login with the password on the same line? – Server Fault

How to make sftp connection password based in Unix script

也是用lftp

SFTP using user id and password | Unix Linux Forums | Shell Programming and Scripting

➜  crifanLib git:(master) sftp –version

sftp: illegal option — –

usage: sftp [-46aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher]

          [-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]

          [-o ssh_option] [-P port] [-R num_requests] [-S program]

          [-s subsystem | sftp_server] host

       sftp [user@]host[:file …]

       sftp [user@]host[:dir[/]]

       sftp -b batchfile [user@]host

➜  crifanLib git:(master) which sftp

/usr/bin/sftp

然后先去命令行试试sftp如何操作。

然后是可以登录和使用基本的命令的:

➜  http_restful_api git:(master) sftp [email protected]

[email protected]’s password:

Connected to 45.79.205.194.

sftp> pwd

Remote working directory: /root

sftp> ll

Invalid command.

sftp> ls

anaconda-ks.cfg                       crifan_wwwroot                        databases_backup_20170905091747       lnmp-install.log

lnmp1.4                               lnmp1.4.tar.gz

sftp> ls /home/wwwroot/book.crifan.com

/home/wwwroot/book.crifan.com/books                /home/wwwroot/book.crifan.com/common               /home/wwwroot/book.crifan.com/index.html

sftp> cd /home/wwwroot/book.crifan.com/books

sftp> pwd

Remote working directory: /home/wwwroot/book.crifan.com/books

sftp> ll

Invalid command.

sftp> ls

http_restful_api   http_summary

然后输入help看看有哪些命令:

sftp> help

Available commands:

bye                                Quit sftp

cd path                            Change remote directory to ‘path’

chgrp grp path                     Change group of file ‘path’ to ‘grp’

chmod mode path                    Change permissions of file ‘path’ to ‘mode’

chown own path                     Change owner of file ‘path’ to ‘own’

df [-hi] [path]                    Display statistics for current directory or

                                   filesystem containing ‘path’

exit                               Quit sftp

get [-afPpRr] remote [local]       Download file

reget [-fPpRr] remote [local]      Resume download file

reput [-fPpRr] [local] remote      Resume upload file

help                               Display this help text

lcd path                           Change local directory to ‘path’

lls [ls-options [path]]            Display local directory listing

lmkdir path                        Create local directory

ln [-s] oldpath newpath            Link remote file (-s for symlink)

lpwd                               Print local working directory

ls [-1afhlnrSt] [path]             Display remote directory listing

lumask umask                       Set local umask to ‘umask’

mkdir path                         Create remote directory

progress                           Toggle display of progress meter

put [-afPpRr] local [remote]       Upload file

pwd                                Display remote working directory

quit                               Quit sftp

rename oldpath newpath             Rename remote file

rm path                            Delete remote file

rmdir path                         Remove remote directory

symlink oldpath newpath            Symlink remote file

version                            Show SFTP version

!command                           Execute ‘command’ in local shell

!                                  Escape to local shell

?                                  Synonym for help

【已解决】sftp用put上传文件不显示进度

然后确认上传完毕:

sftp> put http_restful_api.zip

Uploading http_restful_api.zip to /home/wwwroot/book.crifan.com/books/http_restful_api/http_restful_api.zip

http_restful_api.zip                                                                                                   100%   25MB 153.6KB/s   02:45

sftp> ls -l

drwxr-xr-x    2 root     root         4096 Dec 14 14:38 epub

-rw-r–r–    1 root     root     26033916 Dec 19 22:45 http_restful_api.zip

drwxr-xr-x    2 root     root         4096 Dec 14 14:38 mobi

drwxr-xr-x    2 root     root         4096 Dec 14 14:38 pdf

drwxr-xr-x   10 root     root         4096 Dec 14 14:38 website

sftp> ls -lha

drwxr-xr-x    ? 0        0            4.0K Dec 19 22:43 .

drwxr-xr-x    ? 0        0            4.0K Dec 14 14:44 ..

drwxr-xr-x    ? 0        0            4.0K Dec 14 14:38 epub

-rw-r–r–    ? 0        0           24.8M Dec 19 22:45 http_restful_api.zip

drwxr-xr-x    ? 0        0            4.0K Dec 14 14:38 mobi

drwxr-xr-x    ? 0        0            4.0K Dec 14 14:38 pdf

drwxr-xr-x    ? 0        0            4.0K Dec 14 14:38 website

然后再去删除旧文件,结果出错:

【规避解决】sftp中rmdir出错:Couldn’t remove directory: Failure

【总结】

最终用rsync去同步本地文件到远程服务器,速度很快,效果很好。

具体命令:

sshpass -f sshpass_password.txt rsync -avz –progress –delete –force /Users/crifan/GitBook/Library/Import/http_restful_api/output/http_restful_api [email protected]:/home/wwwroot/book.crifan.com/books

其中本地文件sshpass_password.txt存放着密码。

更详细的解释详见:

【已解决】用rsync去同步本地文件到远程服务器

命令已整合到Makefile中,更完整的Makefile,详见:

https://github.com/crifan/http_restful_api

 ->

https://github.com/crifan/gitbook_template

->

https://github.com/crifan/gitbook_template/blob/master/GitbookCommon.mk

转载请注明:在路上 » 【已解决】命令行中ftp操作上传到远程服务器crifan.com

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.195 seconds, using 22.15MB memory