In my windows environment, some projects need to be deployed on many Linux servers. A script can automatically upload files and execute deployment tasks. The conditions required to complete this task include SSH configuration and an execution script.
Preparation
1. Local Environment Configuration
Install SCP
Install cygwin
2. Ssh setting
Step 1: install OpenSSH
Find the cygwin installation application like setup-x86_64.exe and double click to install the OpenSSH, searc the OpenSSH
And you will find the OpenSSH package and click Next to setup the package
Step 2: produce a SSH key
Open the cygwin window to generate and SSH key using
ssh-keygen
Also, you can append with some Params to generate an SSH key but it's OK for us now.
You can find an hiden file in the current folder called. SSH and there are two filesId_rsaId_rsa.pub
Step 3: Create connection between local and the remote server
Here we'll login to the remote server without password using
ssh-copy-id username@ipaddress
Explanation:
UsernameIs what you login with the remote server
IpadressIs the target remote server's IP
Answering yes to the questions and finally you can try to login the remote server with comman like
ssh username@ipadress -p port
You can omitPort if you are using the default port number 22
Step 4: Create an exec script for deployment
If you are not familiar with shell, so you have to read basic about this. I'll give an demonstrator without explaining. Here's the code of deploy_test File
#! /bin/bashtests=(ip1 ip2 ip3)length=${#tests[@]}for ((i=0; i<$length; i++))donode=${tests[$i]}echo $node# copy file to test serverscp /cygdrive/d/git/test.zip $node:~# operation to mv the etcd file and restart the etcd service#first step to backup the previous version of testssh $node "sudo mv /opt/app/testrepo/test.zip /opt/app/etcdrepo/testbak"#stop the etcd servicessh $node "sudo mv /home/op2/test.zip /opt/app/testrepo "done
Step 5: Make script work out
Exec the script using comman
./deploy_test
Congratulations! You make it. Great!
SSH automatic deployment