shell脚本批量检测远程主机是否能免密ssh登陆
将所需检测的脚本全部放入ip.txt中
起初:
先是执行以下的脚本,执行中途会跳出让你输入密码,这样不能继续执行完
#!/bin/bash
for i in ip.txt
do
ssh $i “ls” > /dev/null 2>&1
if [
?
−
e
q
0
]
;
t
h
e
n
e
c
h
o
"
? -eq 0 ];then echo "
?−eq0];thenecho"i" >> sucess.txt
else
echo “$i” >> error.txt
fi
done
一顿搜索查询后,发现,需要加参数才行,执行以下shell脚本即可
#!/bin/bash
for i in ip.txt
do
echo -e “$i \c” && ssh $i -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no “date” > /dev/null 2>&1
if [
?
=
0
]
;
t
h
e
n
e
c
h
o
"
? = 0 ]; then echo "
?=0];thenecho"i" >> sucess.txt
else
echo “$i” >> error.txt
fi
done
免密ssh登陆后批量判断是否有指定目录的shell方法
上面查出来的sucess.txt中的ip可以通过免密ssh登陆后,在批量判断是否有对应目录:
-e :判断文件或目录是否存在
#!/bin/sh
for ssh_host in cat sucess.txt
do
export filePath=/xx/xx/xx
if ssh $ssh_host test -e $filePath;
then echo $ssh_host >> tomcat_exit.txt
else echo $ssh_host >> tomcat_noexit.txt
fi
done
exit()