免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3017 | 回复: 9
打印 上一主题 下一主题

[其他] CHECK ssh 登陆远程ip 权限 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-20 11:05 |只看该作者 |倒序浏览
本帖最后由 wdong_2001 于 2014-06-20 11:14 编辑

hi,all
我现在需要写一个脚本,去check ssh remote ip权限。我的想法是这样的,如下:

#remotessh.sh
ssh -o StrictHostKeyChecking=no ${ip} "echo" >/dev/null     #第一次访问服务器时不用输入yes

#checkremote.sh

sh remotessh.sh $remoteip

if 不能直接登陆   
       KILL -9 PID(sh remotessh.sh $remoteip)
else
       echo "ssh $remoteip is ok"
if

但遇到了点问题,如下:
判断不能登陆时,kill -9 pid 后,有异常情况如下:
[oracle@Sys-25 bin]$ for ip in 192.168.9.{31,32,33};do echo "ip=$ip";sh checkRemote.sh $ip;done
ip=192.168.9.31
[2014-06-20-10:56:35] [INFO] 192.168.9.40 ssh to 192.168.9.31 success
192.168.9.40 ssh 192.168.9.31       [OK]
ip=192.168.9.32
[2014-06-20-10:56:36] [INFO] 192.168.9.40 ssh to 192.168.9.32 success
192.168.9.40 ssh 192.168.9.32       [OK]
ip=192.168.9.33
oracle@192.168.9.33's password:
[2014-06-20-10:56:37] [ERROR] 192.168.9.40 ssh to 192.168.9.33 false
192.168.9.40 ssh 192.168.9.33       [false]
checkRemote.sh: line 139: 29260 已杀死                  sh ${checkfile} $ip > /dev/null
[oracle@Sys-25 bin]$
Permission denied, please try again.
oracle@192.168.9.33's password:
Permission denied, please try again.
oracle@192.168.9.33's password:
Permission denied (publickey,gssapi-with-mic,password).

[oracle@Sys-25 bin]$
[oracle@Sys-25 bin]$  #这行开始再输入就不能正常显示了
###回车后如下
[oracle@Sys-25 bin]$ [oracle@Sys-25 bin]$ [oracle@Sys-25 bin]$ [oracle@Sys-25 bin]$

论坛徽章:
0
2 [报告]
发表于 2014-06-20 11:23 |只看该作者
高手的帮忙看看,在线等

论坛徽章:
0
3 [报告]
发表于 2014-06-20 12:18 |只看该作者
KILL -9 PID(sh remotessh.sh $remoteip)
这行的问题,不知道原文是什么。

论坛徽章:
0
4 [报告]
发表于 2014-06-20 14:59 |只看该作者
pid=$(ps -ef|grep "sh ${checkfile} ${ip}"|grep -v "grep"|awk '{print $2}')
echo "pid=$pid"
#sleep 30
if [ -n $pid ]
then
         eval ps -ef|grep "sh ${checkfile} ${ip}"|grep -v "grep"|awk '{print $2}'|xargs kill -9
         echo "[${Time}] [ERROR] ${local_ip} ssh to ${ip} false"

fi       

论坛徽章:
0
5 [报告]
发表于 2014-06-20 15:08 |只看该作者
关键是执行完后,还会有些提示,可能是SSH 设置偿试能几次登陆,测试结果如下:如何能把后面的异常过滤掉呢
[oracle@Sys-25 bin]$ for ip in 192.168.9.{31,32,33};do echo "ip=$ip";sh checkRemote.sh $ip;done
ip=192.168.9.31
[2014-06-20-15:05:03] [INFO] 192.168.9.40 ssh to 192.168.9.31 success
192.168.9.40 ssh 192.168.9.31       [OK]
ip=192.168.9.32
[2014-06-20-15:05:05] [INFO] 192.168.9.40 ssh to 192.168.9.32 success
192.168.9.40 ssh 192.168.9.32       [OK]
ip=192.168.9.33
oracle@192.168.9.33's password: pid=2631
[2014-06-20-15:05:06] [ERROR] 192.168.9.40 ssh to 192.168.9.33 false
192.168.9.40 ssh 192.168.9.33       [false]
checkRemote.sh: line 140:  2631 已杀死                  sh ${checkfile} $ip > /dev/null
[oracle@Sys-25 bin]$
Permission denied, please try again.
oracle@192.168.9.33's password:
Permission denied, please try again.
oracle@192.168.9.33's password:
Permission denied (publickey,gssapi-with-mic,password).
[oracle@Sys-25 bin]$ [oracle@Sys-25 bin]$ [oracle@Sys-25 bin]$

论坛徽章:
0
6 [报告]
发表于 2014-06-20 16:18 |只看该作者
回复 4# wdong_2001

看不懂,eval什么作用?

论坛徽章:
7
天秤座
日期:2014-08-07 13:56:30丑牛
日期:2014-08-27 20:34:21双鱼座
日期:2014-08-27 22:02:21天秤座
日期:2014-08-30 10:39:11双鱼座
日期:2014-09-21 20:07:532015年亚洲杯之日本
日期:2015-02-06 14:00:282015亚冠之大阪钢巴
日期:2015-11-02 14:50:19
7 [报告]
发表于 2014-06-20 17:14 |只看该作者
新人弱弱问下,不用ssh-keygen的情况下,调用第一个脚本不是应该卡在输密码的那步吗,是怎么跑if分支去kill的

论坛徽章:
0
8 [报告]
发表于 2014-06-20 17:38 |只看该作者
本帖最后由 wdong_2001 于 2014-06-20 17:41 编辑

调用第一个脚本不是应该卡在输密码的那步吗?是会卡在输密码处,同时会返回一个状态,第二个脚本IF条件处会判断这个返回值,

论坛徽章:
0
9 [报告]
发表于 2014-06-23 07:09 |只看该作者
给你介绍个工具polysh

论坛徽章:
0
10 [报告]
发表于 2014-06-23 14:08 |只看该作者
谢谢大家,已经找到一个好的方法  
ssh 192.168.9.32 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no "echo a" > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "Trust not exist."
fi
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP