1.服务端与客户端建立公钥及私钥信任关系。服务端作为原始文件存储地,ansible主机。
1.1. 在ansible主机上创建公钥。
ssh-keygen -b 1024 -t rsa
[[email protected] ~]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair. #提示正在生成rsa密钥对
Enter file in which to save the key (/home/usrname/.ssh/id_dsa): #询问公钥和私钥存放的位置,回车用默认位置即可
Enter passphrase (empty for no passphrase): #询问输入私钥密语,输入密语 (这边输入的是2Hbl$qt)
Enter same passphrase again: #再次提示输入密语确认
Your identification has been saved in /home/usrname/.ssh/id_dsa. #提示公钥和私钥已经存放在/root/.ssh/目录下
Your public key has been saved in /home/usrname/.ssh/id_dsa.pub.
The key fingerprint is:
x6:68:xx:93:98:8x:87:95:7x:2x:4x:x9:81:xx:56:94
-b 1024 采用长度为1024字节的公钥/私钥对
-t rsa 采用rsa加密方式的公钥/私钥对
有人说使用登录使用密码短语,登录时还要使用密码短语没有比使用用户名和密码方便多少。其实不然。
1.2. 传输公钥到远程主机上,命令:sshpass -p ‘远程主机密码’ [email protected] ssh-copy-id -o StrictHostKeyChecking=no
1.3 如何避免ssh 连接远程主机ip ,输入密码短语呢
(这个一般在云服务器管理中常见,堡垒机到内网主机ssh连接,但是每连一台主机都输入密码短语都比较麻烦,可用以下方式设置)
运行eval命令自动声明环境变量。eval ssh-agent
ssh环境变量加入当前会话环境变量。
ssh-add 把专用密钥添加到ssh-agent的高速缓存中。提示输入密码短语,输入即可。
在当前的环境变量中直接ssh就能连接到远程主机。退出当前shell的时候,ssh-agent也退出。下次连接的时候,再次eval即可。
2.相关脚本定义
思路:基于第一步,建立了ansible主机到各个客户端信任关系后,运行tracker服务,生成大文件的.torrent的文件,播种需要分发的文件包,最后在客户端定义下载文件脚本。
2.1 首先定义两个目录 :存放大文件目录/opt/data/ ;定义脚本目录/opt/app/
2.2 修改/opt/app/murder下的seeder.conf配置文件内容。
#大文件
deploy_file=/opt/data/update_v0.27-91-2
#要生成的torrent文件存放地址,放在ansible的相应目录下。
torrent_file=/etc/ansible/roles/update/files/update_v0.27-91-2.torrent
#tracker服务,其他成员依赖tracker
tracker_ip=21.0.0.153:8998
local_ip=21.0.0.153
2.3 /opt/app/murder/murder_tracker.sh 定义,启动。
#/bin/sh
name="murder-tracker"
murder_tracker_bin="/opt/app/murder/murder-master/dist/murder_tracker.py"
murder_tracker_log="/opt/log/murder/murder_tracker.log"
murder_tracker_data="/opt/data/murder/tracker_data"
find_tracker_process(){
PID=`ps -ef |grep murder_tracker|grep python |grep -v $0|grep -v grep |grep -v sh|awk ‘{print $2}‘`
}
start(){
LOG_DIR=`dirname $murder_tracker_log`
DATA_DIR=`dirname $murder_tracker_data`
if [ ! -d $LOG_DIR ];then
#echo -e "\e[35mlog dir $LOG_DIR doesn‘t exist,creating\e[0m"
printf "log dir $LOG_DIR doesn‘t exist,creating...\n"
mkdir -p $LOG_DIR
fi
if [ ! -d $DATA_DIR ];then
printf "data dir $DATA_DIR doesn‘t exist,creating...\n"
mkdir -p $DATA_DIR
fi
find_tracker_process
echo $PID
if [ "$PID" != "" ]; then
printf "$name is already running...\n"
else
python $murder_tracker_bin > /dev/null 2>&1 &
printf "starting $name done....\n"
fi
}
stop(){
if [ `netstat -lnpt |grep 8998 |wc -l` -eq 1 ];then
find_tracker_process
kill $PID
printf "stoping $name done...\n"
else
printf "$name is already stopping...\n"
fi
}
restart(){
stop
sleep 2
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: $0 {start|stop|restart}\n"
esac
exit
2.4 /opt/app/murder/murder_seeder.sh定义,启动。
#!/bin/sh
name="murder-seeder"
muder_seeder_data="/opt/data/murder"
muder_seeder_log="/opt/log/murder/muder_seeder.log"
murder_make_torrent_bin="/opt/app/murder/murder-master/dist/murder_make_torrent.py"
murder_seeder_bin="/opt/app/murder/murder-master/dist/murder_client.py"
seeder_conf_path="/opt/app/murder/seeder.conf"
deploy_file=$(awk -F= ‘/deploy_file/{print $2}‘ $seeder_conf_path)
torrent_file=$(awk -F= ‘/torrent_file/{print $2}‘ $seeder_conf_path)
tracker_ip=$(awk -F= ‘/tracker_ip/{print $2}‘ $seeder_conf_path)
local_ip=$(awk -F= ‘/local_ip/{print $2}‘ $seeder_conf_path)
#echo $murder_make_torrent_bin $deploy_file $tracker_ip $torrent_file
find_seed_process(){
PID=`ps -ef |grep murder_client|grep seed |grep -v $0|grep -v grep |grep -v sh|awk ‘{print $2}‘`
}
start(){
#make torrent
python $murder_make_torrent_bin $deploy_file $tracker_ip $torrent_file
#echo $?
if [ $? != 0 ];then
python $muder_make_torrent_bin $deploy_file $tracker_ip $torrent_file
fi
find_seed_process
#echo $PID
if [ "$PID" != "" ]; then
printf "$name is already running...\n"
else
python $murder_seeder_bin seed $torrent_file $deploy_file $local_ip >/dev/null 2>&1 &
printf "starting $name done....\n"
fi
}
stop(){
find_seed_process
if [ $PID != "" ];then
kill $PID
printf "stoping $name done...\n"
else
printf "$name is already stopping...\n"
fi
}
restart(){
stop
sleep 2
start
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: $0 {start|stop|restart}\n"
esac
exit
2.5
原文地址:https://blog.51cto.com/12191723/2432761