首页 > 代码库 > SSH自动部署(转)

SSH自动部署(转)

我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上。写了个脚本能够自动上传文件和执行部署任务。完成这个任务需要的条件包括SSH配置和一个执行脚本。

准备

1. 本地环境配置

    install scp

    install cygwin

2. SSH setting

   step1: 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

   step2: produce a ssh key

   open the cygwin window to generate and ssh key using

1
ssh-keygen

  also, you can append with some params to generate a 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 files id_rsa  id_rsa.pub

step 3: create connection between local and the remote server

  here  we‘ll login to the remote server without password using

1
ssh-copy-id username@ipaddress

  explanation:

    username  is what you login with to the remote server

    ipadress    is 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 omit the port if you are using the default port number 22

step4: 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /bin/bash
 
tests=(ip1 ip2 ip3)
length=${#tests[@]}
for ((i=0; i<$length; i++))
do
node=${tests[$i]}
echo $node
# copy file to test server
scp /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 test
ssh $node "sudo mv /opt/app/testrepo/test.zip /opt/app/etcdrepo/testbak"
 
#stop the etcd service
ssh $node  "sudo mv /home/op2/test.zip /opt/app/testrepo "
done

step5: make script work out

exec the script using comman

1
./deploy_test

 

Congratulations! You make it. Great!

 

http://www.cnblogs.com/ilawrence/p/3983587.html

SSH自动部署(转)