I'm using a series of commands for deploying my files to production. This is currently done manually.

Is it possible to make these commands into an executable file? So that I don't have to copy paste these commands each and every time.

When the first line is executed i.e., connecting to the root server, it will ask for the password and I have to paste the password: xxxxxxxxxxxxxx

ssh root@server
pwd: xxxxxxxxxxxxxx
ssh-agent bash
ssh-add bi-master
cd /home/trans/bimaster
git status
git pull [email protected]:xxxxxxxxx/bimaster.git master

Note : I'm the admin of the server, Since the project is been done by different teams as of now, we have decided to do manual deployment (cannot use Jenkins or any other tools for automated deployment), as we have to check the status of some files.

  • Welcome to Ask Different :) After connecting to the remote server and entering the password, are you remaining commands run on local machine or the remote server? Did you consider using SSH keypair to do away with entering password manually every time?
    – Nimesh Neema
    May 17, 2019 at 10:23
  • After connecting to the remote server i'm running commands on my local machine through bash.No haven't tried SSH key-pair ,will check on that. But all the command are redundant is it possible create a batch or exe files with those commands, so that i just have to run the batch for deployment?
    – Sandeep
    May 17, 2019 at 10:46
  • The commands from ssh-agent bash onwards, do they run locally or (over the ssh connection) on the server? If they run locally, what do you need to connect to the server for first?
    – nohillside
    May 17, 2019 at 12:52
  • Can we assume you run your check after the entire script runs? If not - can you lop off all the commands with an edit so you only show the commands you need to run every time, without fail, without interruption. Also - it goes without saying - when you script this, your machine needs to be protected as well as root on the remote. Also - see my comment on GRG. I’m super glad you asked here for help, but I think you’re asking to automate a less-ideal or “wrong” approach to the constraints you face.
    – bmike
    May 17, 2019 at 13:28

2 Answers 2


You can use ‘expect’ to enter the SSH password when prompted. Changes to your script are bolded.

#!/usr/bin/expect -f
spawn ssh root@server
expect "*: "
send "xxxxxxxxxxxxxx
expect "$ "
send "… commands

spawn starts the command as normal, which in this case is SSH
expect waits until the matching text is shown on screen
send enters the text given, which can be a password or command to be run
expect waits for the shell to be shown (this should be changed to match against your shell $PS1)

Save this file, add the executable bit (chmod +x /path/to/file) and open to run the commands.

However, instead of this, you should be using SSH keys to connect to the server without entering a password, and the commands you wish to execute over SSH should be in a script on the server which can be run. Set up SSH keys, then you can run a remote script with ssh -t root@server "/path/to/script".

  • I didn’t have time to write an answer, but mine would be: a) don’t script expect - set up keys. b) make a script remotely to do all the work c) ssh to execute that one script (basically, I’m saying the OP is doing it “wrong” and looking to automate more wrong, rather than making small steps to “right”), so I’d want to take time to craft that message so it’s not harsh.
    – bmike
    May 17, 2019 at 13:30

I would suggest you use ssh-copy-id to grant your computer automated ssh access to the remote server of your choice. The utility is found under /usr/bin/ on most systems (including recent versions of macOS). On older macOS versions you can run

cat ~/.ssh/id_rsa.pub |
    ssh [email protected] "[ -d ~/.ssh ] || mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Then it is trivial to execute a script already on the remote server:

ssh -t [email protected] /Users/johnfoo/myscript.sh

If necessary you can create such script locally and use scp to transfer it to the remote server at the location of your choice:

scp myscriptscript.sh [email protected]:/Users/johnfoo/myscript.sh 

But if your script is relatively short - I would simply run it from the terminal session invoking a here document after your ssh command:

ssh -t [email protected] <<< END_OF_COMMAND
ssh-agent bash
ssh-add bi-master
cd /home/trans/bimaster
git status
git pull [email protected]:xxxxxxxxx/bimaster.git master

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .