简体   繁体   中英

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

I am working on Magento 2 Project.

I did Setup Git on my server. so I can push and pull changes on server directly without using ftp.

in my local i do push changes to 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.

In Script I am thinking too.

  • Connect server using ssh. hosts name and password.
  • and then enter in project directory using cd project name.
  • and there it perform git pull command.
  • and some magento commands to update changes. (Like: bin/magento setup:upgrade etc...)

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:

sudo apt install sshpass

Now go to /home/{youruser}/ and open .bashrc file in any editor ( Press Ctrl+H, if file not visible ).

Now let's create a command alias, create a new line in .bashrc file as per your project details. 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. Enter “connect-magento” in the terminal and now you'll connect to the server without a copy, pasting, or typing anything.

Similarly, you can make shortcut of other commands whichever you wish to by adding in .bashrc file and giving it an alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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