????????д????????????????????????????????
????#!/bin/bash
????# ???ssh????????????
????# ????????key????????ssh????ip??port????????
????# ????????????????????????ο?ssh??-t??-tt????
????# ?????????????????????ж???????????ssh?????????
????# ?ο???http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing
????# ??????????????????????"ssh github.com -O exit"????"ssh github.com -O stop"
????#                                              --by coding my life
????#???????ssh????????????????????????????????????
????SSH_USER='xzc_ssh'
????DB_USER='xzc_db'
????DB_PWD='xzc_db_pwd123'
????EXP_PATH=export_data/
????# ??????????
????# $1 ??????ip
????# $2 ssh???
????# $3 ???
????function exec_remote_command()
????{
????ssh $SSH_USER@$1 -p $2 '$3'
????}
????# ??????sql??????????
????# $1 ??????ip
????# $2 ssh???
????# $3 ??????sql?????select * from user;select * from bag;?????У??????????д?????????
????# s4 ??????
????# $5 ???????
????function export_remote_sql()
????{
????echo export from $4 ...
????cmd="echo "$3" | mysql $4 -u$DB_USER -p$DB_PWD --default-character-set=utf8"
????ssh $SSH_USER@$1 -p $2 "$cmd" > $EXP_PATH$4_$5    #???????????????????????? > $EXP_PATH$4_$5???cmd??
????}
????# $1 ??????
????# $2 ip
????# $3 ???
????function exec_sqls()
????{
????cat SQLS | while read sql ; do
????fc=${sql:0:1}
????if [ "#" == "$fc" ]; then    #???????????
????continue
????fi
????#sql????а?????????????????????????????????????????????
????exp_file="${sql##* }"                #????#???????????????*????????(*???????)????????μ?????exp_file
????sql_cmd="${sql%% $exp_file}"         #????%??????????????%%????????
????export_remote_sql $2 $3 "$sql_cmd" $1 "$exp_file"
????done
????}
????# ??????????′??????????б????SERVERS??????"??????? ip ssh???"????"xzc_game_s99 127.0.0.1 22"
????# ??????????′???sql?????б????SQLS??????"sql??? ?????????"????"select * from user; user.xls"
????# ???sql???????;?????sql??????;????
????# ??????в????????????????????"???????_?????"????"xzc_game_s99_user.xls"
????mkdir -p $EXP_PATH
????cat SERVERS | while read server ; do
????fc=${server:0:1}
????if [ "#" == "$fc" ]; then    #???????????
????continue
????fi
????name=`echo $server|awk '{print $1}'`
????ip=`echo $server|awk '{print $2}'`
????port=`echo $server|awk '{print $3}'`
????exec_sqls $name $ip $port
????done
??????????μ???????£?????SERVERS????????б??????????????????ip??ss????SQLS?????sql?????????????????????????????#?????????????
????xzc@xzc-HP-ProBook-4446s:~/????/remote_cmd$ ls
????remote_cmd.sh  SERVERS  SQLS
????xzc@xzc-HP-ProBook-4446s:~/????/remote_cmd$ cat SERVERS
????xzc_game_s99 120.0.0.99 6162
????xzc_game_s91 120.0.0.91 6162
????xzc_game_s92 120.0.0.92 6162
????xzc_game_s93 120.0.0.93 6162
????xzc_game_s94 120.0.0.94 6162
????#xzc_game_s91 120.0.0.91 6162
????xzc@xzc-HP-ProBook-4446s:~/????/remote_cmd$ cat SQLS
????#select * money from money; money.xls
????select * from user; user.xls
????xzc@xzc-HP-ProBook-4446s:~/????/remote_cmd$
?????????????????????????