前文请见https://www.jianshu.com/p/b7a35769114a
主要是预上线机器没有python3环境,也不好安装,毕竟不是组内的机器;
用shell的话除了慢点也能用不是。。
脚本如下
执行模块
#!/bin/bash
file_exist(){
file_name=$1
if [ ! -f $file_name ];then
echo 'file not exist! '$file_name
exit 1
fi
}
pre_check(){
help_info="i ==> ip file\nc ==> command\ne ==> exec file\n\tag ==>add info"
command=$1
if [[ -z "$command" ]];then
echo -e $help_info
exit 0
fi
exec_time=$(date '+%Y-%m%d-%H%M%S')
save_dir=./temp/$exec_time
if [ ! -d $save_dir ];then
mkdir -p $save_dir
fi
ip_file=temp-ip
exec_file=temp-script
conf_file=temp.conf
echo $command | awk -F ';;' '{for(i=1;i<=NF;i++){print $i}}' > $save_dir/conf_file
while read li;
do
l_k=`echo $li | awk -F '=' '{print $1}'`
l_v=`echo $li | awk -F '=' '{print $2}'`
if [[ "$l_k" == "i" ]];then
file_exist $l_v
cat $l_v | grep -v '^#' | sort -u > $save_dir/$ip_file
elif [[ "$l_k" == "c" ]];then
echo $l_v > $save_dir/$exec_file
elif [[ "$l_k" == "e" ]];then
file_exist $l_v
cat $l_v > $save_dir/$exec_file
elif [[ "$l_k" == "tag" ]];then
tag_info=$l_v
fi
done < $save_dir/conf_file
if [[ ! -f "$save_dir/$ip_file" ]] || [[ ! -f "$save_dir/$exec_file" ]];then
echo -e $help_info
exit 0
fi
}
### main ###
command=$1
pre_check "$command"
tar_dir=/tmp
li_command='cd '$tar_dir' && chmod +x '$exec_file' && ./'$exec_file' && rm -f '$exec_file''
for li in `cat $save_dir/$ip_file | awk '{print $1}'`;
do
info=`cat $save_dir/$ip_file | grep -w $li`
echo "===>"$info
scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -q -r $save_dir/$exec_file $li:$tar_dir
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 $li "$li_command"
done
if [[ ! -z "$tag_info" ]];then
mv $save_dir $save_dir'_'$tag_info
fi
发送模块
#!/bin/bash
file_exist(){
file_name=$1
if [ ! -f $file_name ];then
echo 'file not exist! '$file_name
exit 1
fi
}
pre_check(){
help_info="i ==> ip file\ns ==> source file\nt ==> target dir\ntag ==>add info\n"
command=$1
if [[ -z "$command" ]];then
echo -e $help_info
exit 0
fi
exec_time=$(date '+%Y-%m%d-%H%M%S')
save_dir=./temp/$exec_time
if [ ! -d $save_dir ];then
mkdir -p $save_dir
fi
ip_file=temp-ip
conf_file=temp.conf
echo $command | awk -F ';;' '{for(i=1;i<=NF;i++){print $i}}' > $save_dir/conf_file
while read li;
do
l_k=`echo $li | awk -F '=' '{print $1}'`
l_v=`echo $li | awk -F '=' '{print $2}'`
if [[ "$l_k" == "i" ]];then
file_exist $l_v
cat $l_v | grep -v '^#' | sort -u > $save_dir/$ip_file
elif [[ "$l_k" == "s" ]];then
file_exist $l_v
source_file=$l_v
elif [[ "$l_k" == "t" ]];then
target_dir=$l_v
elif [[ "$l_k" == "tag" ]];then
tag_info=$l_v
fi
done < $save_dir/conf_file
if [[ ! -f "$save_dir/$ip_file" ]];then
echo -e $help_info
exit 0
fi
}
### main ###
command=$1
pre_check "$command"
tar_dir=/tmp
li_command='cd '$target_dir' && ls -l | grep -w '$source_file''
for li in `cat $save_dir/$ip_file | awk '{print $1}'`;
do
info=`cat $save_dir/$ip_file | grep -w $li`
echo "===>"$info
scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -q -r $source_file $li:$target_dir
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 $li "$li_command"
done
if [[ ! -z "$tag_info" ]];then
mv $save_dir $save_dir'_'$tag_info
fi
获取模块
#!/bin/bash
file_exist(){
file_name=$1
if [ ! -f $file_name ];then
echo 'file not exist! '$file_name
exit 1
fi
}
dir_exist(){
dir_name=$1
[ ! -d $dir_name ] && mkdir -p $dir_name
}
pre_check(){
help_info="i ==> ip file\ns ==> source file or dir\nt ==> target dir\ntag ==>add info\n"
command=$1
if [[ -z "$command" ]];then
echo -e $help_info
exit 0
fi
exec_time=$(date '+%Y-%m%d-%H%M%S')
save_dir=./temp/$exec_time
if [ ! -d $save_dir ];then
mkdir -p $save_dir
fi
ip_file=temp-ip
conf_file=temp.conf
echo $command | awk -F ';;' '{for(i=1;i<=NF;i++){print $i}}' > $save_dir/conf_file
while read li;
do
l_k=`echo $li | awk -F '=' '{print $1}'`
l_v=`echo $li | awk -F '=' '{print $2}'`
if [[ "$l_k" == "i" ]];then
file_exist $l_v
cat $l_v | grep -v '^#' | sort -u > $save_dir/$ip_file
elif [[ "$l_k" == "s" ]];then
source_file=$l_v
elif [[ "$l_k" == "t" ]];then
target_dir=$l_v
dir_exist $target_dir
elif [[ "$l_k" == "tag" ]];then
tag_info=$l_v
fi
done < $save_dir/conf_file
if [[ ! -f "$save_dir/$ip_file" ]];then
echo -e $help_info
exit 0
fi
}
### main ###
command=$1
pre_check "$command"
tar_dir=/tmp
li_command='cd '$target_dir' && ls -l | grep -w '$source_file''
for li in `cat $save_dir/$ip_file | awk '{print $1}'`;
do
info=`cat $save_dir/$ip_file | grep -w $li`
dir_exist $target_dir/$li
echo "===>"$info
scp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -q -r $li:$source_file $target_dir/$li
#ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 $li "$li_command"
ls -l $target_dir/$li
done
if [[ ! -z "$tag_info" ]];then
echo $save_dir'_'$tag_info
echo $save_dir
mv $save_dir $save_dir'_'$tag_info
fi
执行栗子
发送结果 -- 将本地的1.txt文件 发送到upload-ip.txt里IP的对应主机/home目录下
获取结果 -- 如图可见将远程的/home/1.txt 取到了本地的/home/test里面
执行结果 -- 在远程主机上执行 'ls -l /home | grep 1.txt' 这条命令,当然也支持原创执行脚本,将c参数换为e参数即可
坑点
主要是shell的环境变量问题,一开始觉得很简单,后来已解决
网友评论