4

I have a deployment script located on a build server. Each time I need to generate the build I need to login to the server using ssh and then trigger the deployment script. I managed to generate the public/private keys so that I need not enter the password to login to the build server. But still I need to login and run the deployment script. Is there a way where to automate the login, executing the deployment script on the build server and then exit from in one local script. How to achieve this

zilcuanu
  • 3,451
  • 8
  • 52
  • 105
  • Well, you can certainly execute a call to `ssh` in a local script and hand over code or a remote file to be executed remotely. That is well documented and you can find endless examples for that on the internet. – arkascha Apr 28 '17 at 14:50
  • Did some1 answer your question, or do you want to automate the login too ? If so what are those criteria (like time or event)? – Mario Apr 28 '17 at 15:22

3 Answers3

2

You can use a pipe (assuming you are using a *nix OS):

echo "your --command --here" | ssh user@host
afdggfh
  • 135
  • 9
0

Usually it's just

ssh buildserver /path/to/build.sh

You may need to tweak the options though.

weirdan
  • 2,499
  • 23
  • 27
0

Just do ssh <HOST> <COMMAND> in a single line. If you can already log in using keys you won't have to type a password. Example:

$ ssh localhost 'echo hi'
hi

This <COMMAND> is run synchronously. That means that ssh won't finish until <COMMAND> run on the remote server has finished. See yourself:

$ ssh localhost 'sleep 10'

This command will wait for 10 seconds and you won't be able to type new commands until it's finished.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • Then it is synchronous right. – zilcuanu Apr 28 '17 at 14:56
  • Sorry, I don't understand - is it a question? – Arkadiusz Drabczyk Apr 28 '17 at 14:57
  • @Pradeep everything without a `&` at the end is synchronous. – ceving Apr 28 '17 at 14:59
  • You have mentioned it is asynchronous in your answer. I think it is synchronous since you will not be able to run any commands till the command on the remote server is finished. – zilcuanu Apr 28 '17 at 15:00
  • That's correct. Just add `&` after the `COMMAND` to make it *asynchronous* – Arkadiusz Drabczyk Apr 28 '17 at 15:04
  • There are two ways in which this can be made asynchronous. The remote command can be started in the background, allowing `ssh` to exit once it is started, or `ssh` itself can be run in the background on the local machine. – chepner Apr 28 '17 at 15:19
  • That's right. And I think that putting `&` after the whole command would make things slightly faster because the wrapper that calls `ssh` could immediately move on to the next line w/o even waiting for connection to be established. It just depends on what OP wants to do. – Arkadiusz Drabczyk Apr 28 '17 at 15:27