简体   繁体   English

使用密码连接到 SSH 并使用 shell 脚本执行一些命令?

[英]Connect to SSH using password and Perform Some commands using shell script?

I am working on Magento 2 Project.我正在从事 Magento 2 项目。

I did Setup Git on my server.我在我的服务器上安装了 Git。 so I can push and pull changes on server directly without using ftp.所以我可以在不使用 ftp 的情况下直接在服务器上推送和拉取更改。

in my local i do push changes to git.在我的本地,我确实将更改推送到 git。 and I do pull on remote side.我确实拉到了偏远的一侧。 so I get my changes over there.所以我在那里得到了我的改变。

So I am thinking to create shell script that manually written command are perform one by one by just executing one script.所以我想创建一个shell脚本,手动编写的命令只需执行一个脚本就可以一一执行。

In Script I am thinking too.在脚本中,我也在思考。

  • Connect server using ssh.使用 ssh 连接服务器。 hosts name and password.主机名和密码。
  • and then enter in project directory using cd project name.然后使用cd项目名称进入项目目录。
  • and there it perform git pull command.并在那里执行 git pull 命令。
  • and some magento commands to update changes.和一些用于更新更改的 magento 命令。 (Like: bin/magento setup:upgrade etc...) (例如:bin/magento 设置:升级等...)

So is there any way to do something like this?那么有没有办法做这样的事情?

您可以通过ssh user@host 'command1;command2'在 ssh 上执行命令并使用 sshpass(但您必须以纯文本形式存储密码,这有点不可以)或设置基于密钥的身份验证(推荐)

First, you'll have to install sshpass, can install by running following command in your terminal:首先,您必须安装 sshpass,可以通过在终端中运行以下命令来安装:

sudo apt install sshpass

Now go to /home/{youruser}/ and open .bashrc file in any editor ( Press Ctrl+H, if file not visible ).现在转到 /home/{youruser}/ 并在任何编辑器中打开 .bashrc 文件(如果文件不可见,请按 Ctrl+H)。

Now let's create a command alias, create a new line in .bashrc file as per your project details.现在让我们创建一个命令别名,根据您的项目详细信息在 .bashrc 文件中创建一个新行。 For example:例如:

alias connect-magentto=’sshpass -p “your ssh password here” ssh yourusername@yourhostname’

Copy-paste above command in .bashrc file and save it and open a new terminal.将上面的命令复制粘贴到 .bashrc 文件中并保存并打开一个新终端。 Enter “connect-magento” in the terminal and now you'll connect to the server without a copy, pasting, or typing anything.在终端中输入“connect-magento”,现在您无需复制、粘贴或输入任何内容即可连接到服务器。

Similarly, you can make shortcut of other commands whichever you wish to by adding in .bashrc file and giving it an alias.同样,您可以通过在 .bashrc 文件中添加并为其指定别名来创建任何您希望的其他命令的快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM