更新本地代码到服务器的shell脚本

用Ubuntu做开发机OS遇到的第一个问题就是将更新的代码放上测试服务器运行

之前用windows的时候是用Winscp更新代码到服务器上去的

ubuntu下面没有类似Winscp的程序,只能自己写个简单的脚本做这种事情了

#要更新的的文件数组 相对根目录的路径
file_array=("xxx/xxxxx.py" \
"xxx/xxxxx.py")

#我机子项目根目录
my_dir="/home/xxx/workspace/xxx/"
#开发机项目根目录
net_dir="[email protected]:/usr/local/xxx/"

length=${#file_array[@]}
echo ${length}
for ((i=0; i<${length}; i++));
do
scp ${my_dir}${file_array[$i]} ${net_dir}${file_array[$i]}
done


其实就是一个简单的循环执行 scp命令而已

但这么做可能需要重复输入服务器密码
可以执行:
ssh-keygen -t rsa

在用户的主目录/.ssh目录下面生成一对密钥
id_rsa     私钥
id_rsa.pub 公钥
将公钥拷贝到远端主机,并写入授权列表文件
touch /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys


其实就是把id_rsa.pub里面的内容复制到authorized_keys里面

这部分参考:《使用ssh公钥实现ssh免密码登录》 http://hi.baidu.com/meloidea/item/15d43d2dd11d010e72863eb4

猜你喜欢

转载自dongerjin.iteye.com/blog/1826401