您的位置:首页 > 运维架构 > Shell

shell ssh sftp 远程自动部署(上传文件)

2016-09-06 16:14 429 查看
前提:ssh sftp 无密码登陆配置方法

1.生成密钥对:ssh-keygen -t rsa

2.将私有密钥保存本地: ~/.ssh/id_rsa,

  将公有密钥复制到远程服务器: ~/.ssh/authorized_keys

3.修改公钥文件的访问权限 chmod 644 authorized_keys

4.可能出现的异常

异常:sign_and_send_pubkey: signing failed: agent refused operation

原因: ssh-agent isn't working

命令:`eval ssh-agent -s`

`ssh-add` 或者 `ssh-add ~/.ssh/my_other_key`

5.远程部署指定目录

#!/bin/bash
sourcePath="/home/xinhuanet/workspace/out/artifacts/web_war_exploded"
targetPaht="/home/workspace/ttt"
len=${#sourcePath}
user="root"
targetIP="ip"
#创建远程目录
mkdir_remote_dir(){
ssh $user@$targetIP<<EOF
if [ ! -d $1 ]; then
mkdir $1
fi
exit
EOF
}
#上传远程文件
sftp_upload_file(){
sftp $user@$targetIP<<EOF
put -r $1 $2
quit
EOF
}
#遍历本地目录
list_file(){
for file in `ls  $1`
do
#file 文件名 $1 路径名 ; local_path本地全路径
local_path=$1/$file
#截取本地根路径${local_path:${len}} ;remote_path远程全路径
remote_path=$targetPaht${local_path:${len}}
echo $local_path"--->to---->"$remote_path
if [ -d $local_path ]; then
mkdir_remote_dir $remote_path
list_file $local_path
elif [ -f $local_path ];  then
sftp_upload_file $local_path $remote_path
else
echo 'not d and f'
fi
done
}

#部署整个目录
#list_file $sourcePath

#--------一下方式---基于git------------------------------------------------
#基于git diff commit_id 查看差异文件清单进行差异化的自动化部署
#由于git中文件路径与本地编译后的文件路径存在差异,需要进一步处理.
#处理方式不统一,根据个人实际情况

git_file_list(){
list_files=`git diff $1 $2 --name-only`
for file in $list_files
do
file=${file/#"home/WebRoot/"/""}#替换。源码路径与编译路径转换
file=${file/#"home/src"/"WEB-INF/classes"}#替换。源码路径与编译路径转换
file=${file/%".java"/".class"}#替换。源码文件与编译文件转换
echo $sourcePath/$file
echo $targetPaht/$file
#创建远程目录(不存在时)
mkdir_remote_dir `dirname $targetPaht/$file`
sftp_upload_file $sourcePath/$file $targetPaht/$file
done
}
#两个commit 之间的差异文件清单
git_file_list $1 $2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OpenSSL