#!/bin/bash

shell_user="root"
shell_pass="1233"
shell_port="22"
shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l` comad[0]=$1
comad[1]=$2
comad[2]=$3
comad[3]=$4
comad[4]=$5
comad[5]=$6
comad[6]=$7
comad[7]=$8
comad[8]=$9
comad[9]=${10}
comad[10]=${11}
comad[11]=${12}
comad[12]=${13}
comad[13]=${14}
comad[14]=${15}
comad[15]=${16}
comad[16]=${17}
comad[17]=${18}
comad[18]=${19}
comad[19]=${20}
comad[20]=${21} function shell_exp(){ date_start=`date +"%T"`
rm -fr ./shell_log
for temp in `seq 1 $shell_row`
do
Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1` ping -c 1 $Ip_Addr &>/dev/null
if [ $? -eq 0 ]
then
echo -e "\e[34;1m====================================================>$Ip_Addr<=======================================================\e[;m\n"
ssh -p $shell_port $shell_user@$Ip_Addr ${comad[*]}
if [ $? -eq 0 ]
then
echo -e "$Ip_Addr\t\t$shell_port\t\t${comad[*]} \t\t成功\n" >>./shell_log
fi
else
echo -e "$Ip_Addr\t\t$shell_port\t\t${comad[*]} \t\t失败\n" >>./shell_log
continue
fi
done
date_end=`date +"%T"`
clear
echo -e "\e[34;1m===========================================================================================================================\e[;m"
echo -e "\e[34;1m主机IP 端口 命令 状态 \e[;m\n"
cat ./shell_log
echo -e "\e[34;1m===========================================================================================================================\e[;m\n"
echo -e "\e[34;1m 开始时间:$date_start \t\t\t 结束时间:$date_end \t\t\t\t \e[;m\n"
echo -e "\e[34;1m 执行主机数:$shell_row \t\t\t 成功主机:`cat ./shell_log |grep "成功" |wc -l` \t\t\t 失败主机:`cat ./shell_log |grep "失败" |wc -l`\t\t\t\e[;m\n"
echo -e "\e[34;1m 失败主机记录 ↓\e[;m\n"
cat ./shell_log |grep "失败"
echo -e "\e[34;1m===========================================================================================================================\e[;m\n" } function shell_upload(){ date_start=`date +"%T"`
rm -fr ./shell_log for temp in `seq 1 $shell_row`
do take_ip=`cat $shell_list |head -n $temp |tail -n 1`
Ip_Addr="$take_ip"
ping -c 1 $Ip_Addr &>/dev/null
if [ $? -eq 0 ]
then
scp ${comad[1]} $shell_user@$Ip_Addr:${comad[2]} &>/dev/null
if [ $? -eq 0 ]
then
echo -e "\e[34;1m$Ip_Addr\t\t 传输完成....\e[;m\n"
echo -e "$Ip_Addr\t\t$shell_port\t\t${comad[1]}\t\t${comad[2]}\t\t\t\t成功\n" >>./shell_log
fi
else
echo -e "\e[34;1m$Ip_Addr\t\t 传输失败....\e[;m\n"
echo -e "$Ip_Addr\t\t$shell_port\t\t${comad[1]}\t\t${comad[2]}\t\t\t\t失败\n" >>./shell_log
continue
fi done date_end=`date +"%T"`
clear
echo -e "\e[34;1m===========================================================================================================================\e[;m"
echo -e "\e[34;1m主机IP 端口 传输源 传输到 状态 \e[;m\n"
cat ./shell_log
echo -e "\e[34;1m===========================================================================================================================\e[;m\n"
echo -e "\e[34;1m 开始时间:$date_start \t\t\t 结束时间:$date_end \t\t\t\t \e[;m\n"
echo -e "\e[34;1m 执行主机数:$shell_row \t\t\t 成功主机:`cat ./shell_log |grep "成功" |wc -l` \t\t\t 失败主机:`cat ./shell_log |grep "失败" |wc -l`\t\t\t\e[;m\n"
echo -e "\e[34;1m 失败主机记录 ↓\e[;m\n"
cat ./shell_log |grep "失败"
echo -e "\e[34;1m===========================================================================================================================\e[;m\n"
} function shell_meminfo(){ echo -e "\e[34;1m===========================================================================================================================\e[;m"
echo -e "\e[34;1m IP地址\t\t\t总量\t\t已使用\t\t剩余\t\t共享\t\t缓冲\t\t缓存\t\t\n \e[;m"
for temp in `seq 1 $shell_row`
do
take_ip=`cat $shell_list |head -n $temp |tail -n 1`
Ip_Addr="$take_ip"
echo -ne "\e[34;1m $take_ip\t\t \e[;m"
# sleep 0.5
ping -c 1 $Ip_Addr &>/dev/null
if [ $? -eq 0 ]
then
ssh $shell_user@$take_ip free -h |grep "Mem:" |awk '{print $2 "\t\t" $3 "\t\t" $4 "\t\t" $5 "\t\t" $6 "\t\t" $7}'
else
echo -e "0M\t\t0M\t\t0M\t\t0M\t\t0M\t\t0M"
fi
done
echo -e "\e[34;1m===========================================================================================================================\e[;m" } #==============================================批量分发SSH密钥函数体-开始=======================================================================
function shell_ssh_keygen(){
/usr/bin/expect << EOF
set timeout 5
spawn ssh-keygen -t rsa
expect {
"*save the key*" {send "\n";exp_continue}
"Enter passphrase*" {send "\n";exp_continue}
"*passphrase again:" {send "\n"}
}
expect eof
EOF
} function shell_push_sshkey(){
local ssh_user=$1
local ssh_host=$2
local ssh_pass=$3
/usr/bin/expect << EOF
set timeout 10
spawn ssh-copy-id $ssh_user@$ssh_host
expect {
"(yes/no)" {send "yes\n"; exp_continue}
"password:" {send "$ssh_pass\n"}
"id_rsa.pub" {puts "(^_^)\n";exit 2\n}
}
expect eof
EOF
} function shell_expect(){ for temp in `seq 1 $shell_row`
do
Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1`
shell_push_sshkey $shell_user $Ip_Addr $shell_pass
done
}
#==============================================批量分发SSH密钥函数体-结束======================================================================= function shell_add_ip(){ [ -e $shell_list ] || touch $shell_list
echo "${comad[1]}" >>$shell_list
if [ $? -ne 0 ]
then
echo -e "\e[34;1m 添加IP:${comad[1]}失败 \e[;m\n\n"
exit 1
fi
} function shell_show_ip(){ [ -e $shell_list ] || touch $shell_list
echo -e "\e[34;1m====================================================================\n\n\e[;m"
echo -e "\e[34;1m`cat $shell_list` \n\n\e[;m"
echo -e "\e[34;1m总计:`cat $shell_list |wc -l `\t\t\t 操作列表:$shell_list\n\n\e[;m"
echo -e "\e[34;1m====================================================================\n\n\e[;m"
} function shell_drop_ip(){ #sed -i '/echo "${comad[1]}"/d' $shell_list
# for i in `seq 1 $shell_row`
# do
# Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1`
# if [ "$Ip_Addr" == "$del_ip" ]
# then
# donei
# cat $shell_list |grep -v ${comad[1]} >/root/ip_list.tmp
# rm -fr /root/ip_list
# mv /root/ip_list.tmp /root/ip_list rm -fr $shell_list
touch $shell_list } function shell_init(){ touch $shell_list
yum install -y expect
[ $? -eq 0 ]
echo -e "\e[34;1m 初始化成功... \e[m" } function shell_help(){ echo -e "\e[34;1m====================================================================\n" # echo -e "注:如需要多线程执行,请自行在for语句下加入{} 并写入 wait 即可支持多线程,加快传输速度\n\n" # echo -e "\t\t\t[使用时请自行在/root/目录下创建ip_list文件并写入你要控制的主机]\n\n" echo -e "\t-shell [任意命令] 批量远程执行命令\n" echo -e "\t-init 初始化\n" echo -e "\t-show 显示控制列表\n" echo -e "\t-add 添加一个被管理主机\n" echo -e "\t-del 删除一个被管理主机\n" echo -e "\t-drop 清空一个主机列表\n" echo -e "\t-keys 本地生成密钥对\n" echo -e "\t-scpkeys 批量分发密钥对\n" echo -e "\t-upload [本地文件] [传输到] 批量传输文件\n" echo -e "\t-mem 批量统计主机内存使用\n" echo -e "====================================================================\n\n"
echo -e " Powered by LyShark 瑞王保留所有权利\n"
echo -e "====================================================================\e[;m" } case ${comad[0]} in "")
shell_help
exit 1
;;
"-upload")
shell_upload
exit 1
;;
"-mem")
shell_meminfo
exit 1
;;
"-keys")
shell_ssh_keygen
exit 1
;;
"-scpkeys")
shell_expect
exit 1
;;
"-add")
shell_add_ip
exit 1
;;
"-show")
shell_show_ip
exit 1
;;
"-drop")
shell_drop_ip
exit 1
;;
"-init")
shell_init
exit 1
;;
*) shell_exp
exit 1
;; esac

bash 管理小脚本的更多相关文章

  1. Bash玩转脚本1之自己的脚本安装程序

    Bash之打造自己的脚本安装器 前言 还是理所当然的前言,我一直想找一套管理脚本的"框架",能让自己杂乱的脚本有点规整.无奈眼界尚浅,未能找到. 因此萌生自己写一点优化脚本的工具来 ...

  2. 第一个Mac shell 小脚本

    大多数程序员都喜欢偷懒的,我也不例外.相信好多Android开发的coder 在网络http请求方面,会浪费很多时间在接口调试这里..有时候,自己写了一个小测试,行还好,不行的话,还要跟写后台的哥们一 ...

  3. python 小脚本升级-- 钉钉群聊天机器人

    一则小脚本(工作中用) 在这篇文章中写的监控的脚本,发送监控的时候 是利用的邮箱,其实在实际,邮箱查收有着不方便性,于是乎升级, 我们工作中,经常用钉钉,那么如果要是能用到钉钉多好,这样我们的监控成功 ...

  4. svn同步小脚本

    企业中难免会有使用svn 的怎么给svn做好备份呢 下面是用两个脚本进行同步和监控 同步脚本: #!/bin/bash i=1 while true do if [ $(expr $i % 12) = ...

  5. Linux释放内存小脚本

    最近发现渣渣ECS内存总是不够用,内存太小一不小心就用完了,用完就用完吧,内存用来做cache是可以快一些,但是内存用完了老是一顿一顿的卡,实在有点受不了,于是就写了释放内存的小脚本,觉得卡了就释放下 ...

  6. db2巡检小脚本

    写了下db2巡检的一个小脚本,只能做常规检查,减少日常工作量,脚本内容如下: #!/bash/bin echo "物理CPU个数为:"cat /proc/cpuinfo| grep ...

  7. Mac shell 小脚本开发(转)

    大多数程序员都喜欢偷懒的,我也不例外.相信好多Android开发的coder 在网络http请求方面,会浪费很多时间在接口调试这里..有时候,自己写了一个小测试,行还好,不行的话,还要跟写后台的哥们一 ...

  8. MS SQL 日常维护管理常用脚本(二)

    监控数据库运行 下面是整理.收集监控数据库运行的一些常用脚本,也是MS SQL 日常维护管理常用脚本(一)的续集,欢迎大家补充.提意见. 查看数据库登录名信息   Code Snippet SELEC ...

  9. 写了一个常规性生成merge 的小脚本

    现在使用数据库来写存储过程,动不动参数就会用到xml ,当然罗,优势也很明显,参数相对固定,而且灵活,如果要修改或者什么的,中间接口层也不需要做变化,只需要修改封装的存储过程以及程序传参就ok了. 随 ...

随机推荐

  1. AD16 PCB重新定义板型时没有Redefine Board Shape

    1.高版本的AD都没有“Redefine Board Shape” 2.在Keep-Out-Layer层,做好了板子的外形,把外形都选中后,然后快捷键D-S-D即可.

  2. Kafka的安装及与Spring Boot的集成

    安装JDK 下载jdk-8u202-ea-bin-b03-linux-x64-07_nov_2018.tar.gz 解压 配置 $ vi /etc/profile,在最后加入下面两行 export J ...

  3. MySQL开发设计规范

    1.库命名规则:dbname_suffix,分为_dev/_test/_pre/_mertest/_perf/_prod六个环境 2.适度反范式设计,冗余表字段数据减少JOIN关联提高访问效率 3.普 ...

  4. 《笨方法学Python》加分题16

    基础部分 # 载入 sys.argv 模块,以获取脚本运行参数. from sys import argv # 将 argv 解包,并将脚本名赋值给变量 script :将参数赋值给变量 filena ...

  5. day 14:深浅copy,数据结构 ,函数,set集合,变量作用域、返回值

    本节内容: 1,深浅copy 2,数据结构 3,函数 4,变量作用域 5,返回值,return 一.深浅copy 首先理解当给变量赋值的时候,是给变量存在值所在的内存地址 a = 10000 b = ...

  6. CSS碎片

    1.外边距不会应用到行内非替换元素的顶端和底端,因为不会改变它的行高,所有没有视觉效果. 而行内非替换元素上下内边距虽然也不改变行高,但却能延伸背景,所有背景可见,可能会重叠其他行. span{mar ...

  7. openstack 王者归来学习笔记

    rpc.call方法执行的流程:(下次看代码的时候可以根据这流程来看,注意:由于rpc服务器和客户端具有很松的耦合性,因此以上步骤并不是绝对的.) 1.rpc服务器定义和启动rpc服务 2.rpc服务 ...

  8. SpringBoot图片上传

    毕设终于写到头像上传了,本来想用Vue写来着,但是一直不顺利,还是对Vue用的不太熟.所以就用jquery写了. 首先添加以下标签 <img id="avatarPreview&quo ...

  9. ABP 设置默认为中文

    把资源文件 的zh-cn去掉就可以,改成默认文件

  10. mysql 1055

    在 /etc/my.cnf 文件里加上如下: sql_mode=NO_ENGINE_SUBSTITUTION