对未进行主机信任操作的服务器进行批量操作
由于ssh只能在交互模式中输入服务器密码进行登录登操作,不便于进行大批量服务器进行巡检或日志采集。sshpass恰好又解决了这个问题,使用ssh -p passwd可以实现命令行输入密码操作,便于进行规模巡检
首先需要在脚本执行机器上安装sshpass,可以使用yum进行安装
# yum方式安装
yum -y install sshpass
tools.sh脚本以及使用方式
#!/bin/bash
# 利用sshpass实现对服务器进行批量操作
arg_num=$#
file=$2
cmd=$4
c1=$1
c3=$3
c4=$4
c5=$5
c6=$6
function main(){
if [ $arg_num -ne 4 ] && [[ $c1 != "-f" ]]
then
errorMessage
exit 1
elif [[ $c3 = "-c" ]]
then
valus
doCommand
elif [[ $c3 = "-l" ]] && [[ $c5 = "-s" ]]
then
# sh tools.sh -f hostlist -l hostlist -s /home/
# scp -P 22022 hostlist 192.168.1.1:/home
valus
doScpLocalToServer
elif [[ $c3 = "-s" ]] && [[ $c5 = "-l" ]]
then
valus
doScpServerToLocal
else
errorMessage
fi
}
function doScpLocalToServer(){
for ip in $iplist
do
for_num=`expr $for_num + 1`
pd=`echo $passwordlist | awk -v k=$for_num '{print $k}'`
echo -e "
登录查看全部
参与评论
手机查看
返回顶部