用于将文件上传到远程服务器上相同子目录的Shell脚本

A very common task for a web developer is uploading a single file from a subdirectory on your development box to the same subdirectory on a remote server. Unfortunately, this always ends up being an annoying manual process involving switching directories on both servers, and wastes a large amount of time.

对于Web开发人员而言,一项非常常见的任务是将单个文件从开发框中的子目录上载到远程服务器上的同一子目录中。 不幸的是,这总是最终成为一个烦人的手动过程,涉及在两个服务器上切换目录,并且浪费了大量时间。

So, I’ve taken it upon myself to write a shell script to automate this for me, using scp for secure file copying. (Important Side Note:ftp is horribly insecure, use WinSCP instead)

因此,我自己编写了一个shell脚本,使用scp进行安全的文件复制,从而为我自动化了这一过程。 (重要注意事项:ftp非常不安全,请改用WinSCP )

The main problem to solve was that my WordPress install on my development machine has a different base directory than the installation on my server. On my development machine it’s /var/www, and on my server is something more like /var/www/howtogeek/docs/. I decided to just move those into variables at the beginning of the script, like this:

要解决的主要问题是,开发机器上的WordPress安装与服务器上的安装具有不同的基本目录。 在我的开发机器上,它是/ var / www,在我的服务器上是/ var / www / howtogeek / docs /。 我决定只在脚本开始时将它们移入变量,如下所示:

SSHSERVER=thegeek@hostname.com RDIR=/var/www/howtogeek/docs LDIR=’\/var\/www’

SSHSERVER=thegeek@hostname.com RDIR = / var / www / howtogeek / docs LDIR ='\ / var \ / www'

Note that the local directory(LDIR) in the script has a \ before each / in the path. This is because we are going to pass this into a sed command later in the script, so we have to escape those values. I’m sure there’s some other way around this, but I’m unaware of it.

请注意,脚本中的本地目录(LDIR)在路径中的每个/之前都有一个\。 这是因为我们稍后将在脚本中将其传递给sed命令,因此我们必须转义那些值。 我敢肯定还有其他解决方法,但是我没有意识到。

Next, I set up automated ssh logins. To do this, you need to first run the ssh keygen utility like so:

接下来,我设置了自动ssh登录。 为此,您需要先运行ssh keygen实用程序,如下所示:

ssh-keygen -t rsa

ssh-keygen -t rsa

Then you need to copy the ~/.ssh/id_rsa.pub file to ~/.ssh/authorized_keys file on the remote server. If the ~/.ssh directory is already created on the remote server (might want to check), then you can run this command to copy the keys over:

然后,您需要将〜/ .ssh / id_rsa.pub文件复制到远程服务器上的〜/ .ssh / authorized_keys文件中。 如果〜/ .ssh目录已经在远程服务器上创建(可能要检查),则可以运行以下命令来复制密钥:

cat ~/.ssh/id_rsa.pub | ssh user@hostname ‘cat >> .ssh/authorized_keys’

猫〜/ .ssh / id_rsa.pub | ssh user @ hostname'cat >> .ssh / authorized_keys'

Now we’ve got automated ssh logins, which will enable using the scp utility without a password. Now let’s take a look at the scp command we’ll use in the script:

现在,我们有了自动ssh登录,这将使您无需密码即可使用scp实用程序。 现在让我们看一下将在脚本中使用的scp命令:

scp $1 $SSHSERVER:$RDIR`pwd|sed “s/${LDIR}//”`/`echo $1|sed ‘s/.*\///’`

scp $ 1 $ SSHSERVER:$ RDIR`pwd | sed“ s / $ {LDIR} //”`/`echo $ 1 | sed's /.*\///``

The $1 is the parameter you’ll pass to the script for the filename you are trying to upload. $SSHSERVER and $RDIR are variables we’ll set at the beginning of the script to set the user@hostname and the preceding remote path.

$ 1是您将传递给脚本的参数,用于尝试上载的文件名。 $ SSHSERVER和$ RDIR是我们在脚本开头设置的变量,用于设置user @ hostname和前面的远程路径。

The first block that starts with pwd|sed takes the current directory path and strips off the preceding part of the path that doesn’t match the remote server, but leaves the rest of the path. For instance, if you were in /var/www/wp-content/ when you ran the command, the sed block would strip off the /var/www based on our LDIR variable, but leave the /wp-content/ part of it.

以pwd | sed开头的第一个块采用当前目录路径,并剥离与远程服务器不匹配的路径的前一部分,但保留其余部分。 例如,如果您在运行命令时位于/ var / www / wp-content /中,则sed块将基于我们的LDIR变量剥离/ var / www,但保留/ wp-content /部分。

The second block just makes sure to strip off the entire preceding path off the filename passed to the script so you just get the filename.

第二个块只是确保从传递给脚本的文件名中删除整个前面的路径,这样您就可以获取文件名。

For example, if you named the script up.sh and ran it from your local /var/www/wp-content/plugins/ directory on a file:

例如,如果您将脚本命名为up.sh并从文件的本地/ var / www / wp-content / plugins /目录中运行它:

up.sh myplugin.php

up.sh myplugin.php

This would have been expanded by the shell as the following:

Shell会对此进行如下扩展:

scp myplugin.php thegeek@hostname.com:/var/www/howtogeek/docs/wp-content/plugins/myplugin.php

scp myplugin.php thegeek@hostname.com:/var/www/howtogeek/docs/wp-content/plugins/myplugin.php

Instead of trying to create your own script file based on the article, I’ve provided it for you to download. That way there won’t be any issues with special characters not working… and yes, I used the script to upload itself =)

我没有尝试根据文章创建自己的脚本文件,而是提供了供您下载。 这样就不会出现特殊字符不起作用的任何问题……是的,我使用脚本上传了自己=)

Download geek-uploader.sh

下载geek-uploader.sh

Just put it somewhere in your path, and name it whatever you want. I renamed mine to just “up”, so I can just type “up filename.txt” to upload. Also remember to open it up and change the variables at the beginning of the script to match your environment.

只需将其放在您的路径中的任何位置,然后随便命名即可。 我将我的重命名为“ up”,所以我可以键入“ up filename.txt”进行上传。 还记得打开它并在脚本的开头更改变量以匹配您的环境。

This script should work on anything with a *nix shell and the scp utility. I’m using Ubuntu, but it should work on Solaris, OS X, or even Windows with Cygwin installed.

该脚本应该可以在具有* nix shell和scp实用程序的任何东西上使用。 我正在使用Ubuntu,但是它应该可以在Solaris,OS X或什至安装了Cygwin的Windows上运行。

翻译自: https://www.howtogeek.com/howto/ubuntu/shell-script-to-upload-a-file-to-the-same-subdirectory-on-a-remote-server/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值