免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7474 | 回复: 6

关于expect实现ssh登录的问题 [复制链接]

论坛徽章:
0
发表于 2011-07-13 17:04 |显示全部楼层
用expect实现自动ssh登录,不过有个问题目前没有搞明白,用ssh name@host 登录,
有两种情况
第一种:如果是第一次会有一个
The authenticity of host '192.168.10.23 (192.168.10.23)' can't be established.
RSA key fingerprint is 54:e6:ce:7a:aa:c0:95:31:58:0d:56:f6:ee:b5:8a:14.
Are you sure you want to continue connecting (yes/no)?
的提示,点YES后,然后输入密码.
第二种:ssh 主机直接出输入密码的提示
在expect中如何对这两种情况进行匹配呢,我试的是有提示的如果可以通过登录,但再用这脚本第二次再登录就不行了.或者直接匹配输入密码的,但是写脚本就是要两个情况都考虑到,我在网上搜了几天一直没找到答案,哪位高人能提点下我.谢谢

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:09
发表于 2011-07-13 17:19 |显示全部楼层
child = pexpect.spawn('/bin/bash',['-c',command],timeout=1200)
index = child.expect (['password: ','Are you sure you want to continue connecting',pexpect.EOF,pexpect.TIMEOUT])
if index == 0:
        print 'good'
        child.sendline(passwd)
        child.expect(pexpect.EOF)
        print child.before
        sys.exit(0)
elif index == 1:
        print 'first time connet'
        child.sendline('yes')
        child.expect('password: ')
        child.sendline(passwd)
        child.expect(pexpect.EOF)
        print child.before
        sys.exit(0)
elif index == 2:
        print 'exit'
        sys.exit(1)
elif index == 2:
        print 'connet time out'
        sys.exit(1)



这个是我python的脚本,你可以通过expect (['password: ','Are you sure you want to continue connecting',pexpect.EOF,pexpect.TIMEOUT])来判断多种情况。
shell里面的expect 没用过,但也是一样的,语法有点区别而已

论坛徽章:
0
发表于 2011-07-14 14:35 |显示全部楼层
谢谢.现在还不会PYTHON,只想知道shell下用expect怎么实现

论坛徽章:
0
发表于 2011-07-14 15:14 |显示全部楼层
最好的办法是修改ssh程序,使之接受一个密码参数,

论坛徽章:
0
发表于 2011-07-15 09:20 |显示全部楼层
这个是有点烦人,甚至有些是Pass有些又是pass,这些都要加判断!

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2011-07-15 09:37 |显示全部楼层
用expect实现自动ssh登录,不过有个问题目前没有搞明白,用ssh name@host 登录,
有两种情况
第一种:如果是第 ...
er_shi 发表于 2011-07-13 17:04



expect {
  "*yes/no*" {send "yes\r"; exp_continue}
  "*password*" {send "$pass\r";}
}

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
发表于 2011-07-15 09:38 |显示全部楼层
回复 1# er_shi


    又是这种月经问题
FYI,不懂自己看man expect,这个是我自己用的。
  1.     #!/usr/bin/expect
  2.     set timeout 60
  3.     set pwd "该机器的密码"
  4.     spawn ssh  10.10.10.1
  5.            expect {

  6.                     "not know" {send_user "[exec echo \"not know\"]";exit}

  7.                     "(yes/no)?" {send "yes\r";exp_continue}

  8.                     "password:" {send  "$pwd\r"}

  9.                     "Permission denied, please try again." {

  10.                         send_user "[exec echo \"Error:Password is wrong\"]"

  11.                         exit  }


  12. expect eof
  13.                     }


  14. expect "*#"
  15. send "ifconfig\r"
  16. send "exit\r"
  17. expect eof

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP