免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 6840 | 回复: 24

[其他] expect使用问题,跪求指导!!!! [复制链接]

论坛徽章:
0
发表于 2013-04-26 09:23 |显示全部楼层
15可用积分

以下是我写的一个测试用的脚本:
#!/usr/bin/expect -f
set passwd [lindex $argv 1]
set ipaddress [lindex $argv 0]
set timeout 10

spawn ssh -q alne@$ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}
expect "\$"
send "uptime \r"
send "exit \r"
expect eof
exit

脚本执行到登陆远程机器以后就会停住,

和set timeout 设置的时间有关系,我要是设置10秒,他就会等10秒才会执行 send后的命令,设置100秒他就等100秒才执行send命令。

没用过啊,痛苦啊,这是怎么回事,跪求回复啊

最佳答案

查看完整内容

#!/usr/bin/env python"""This runs a command on a remote host using SSH. At the prompts enter hostname,user, password and the command."""import pexpectimport getpass, os#user: ssh 主机的用户名#host:ssh 主机的域名#password:ssh 主机的密码#command:即将在远端 ssh 主机上运行的命令def ssh_command (user, host, password, command): """ This runs a command on the remote host. This could also be done ...

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
发表于 2013-04-26 09:23 |显示全部楼层
#!/usr/bin/env python

"""
This runs a command on a remote host using SSH. At the prompts enter hostname,
user, password and the command.
"""

import pexpect
import getpass, os

#user: ssh 主机的用户名
#host:ssh 主机的域名
#password:ssh 主机的密码
#command:即将在远端 ssh 主机上运行的命令
def ssh_command (user, host, password, command):
    """
    This runs a command on the remote host. This could also be done with the
    pxssh class, but this demonstrates what that class does at a simpler level.
    This returns a pexpect.spawn object. This handles the case when you try to
    connect to a new host and ssh asks you if you want to accept the public key
    fingerprint and continue connecting.
    """
    ssh_newkey = 'Are you sure you want to continue connecting'
    # 为 ssh 命令生成一个 spawn 类的子程序对象.
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
    i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
    # 如果登录超时,打印出错信息,并退出.
    if i == 0: # Timeout
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    # 如果 ssh 没有 public key,接受它.
    if i == 1: # SSH does not have the public key. Just accept it.
        child.sendline ('yes')
        child.expect ('password: ')
        i = child.expect([pexpect.TIMEOUT, 'password: '])
        if i == 0: # Timeout
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    # 输入密码.
    child.sendline(password)
    return child

def main ():
    # 获得用户指定 ssh 主机域名.
    host = raw_input('Hostname: ')
    # 获得用户指定 ssh 主机用户名.
    user = raw_input('User: ')
    # 获得用户指定 ssh 主机密码.
    password = getpass.getpass()
    # 获得用户指定 ssh 主机上即将运行的命令.
    command = raw_input('Enter the command: ')
    child = ssh_command (user, host, password, command)
    # 匹配 pexpect.EOF
    child.expect(pexpect.EOF)
    # 输出命令结果.
    print child.before

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print str(e)
        traceback.print_exc()
        os._exit(1)


看下pexpect也能实现。
参考URL http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/

论坛徽章:
0
发表于 2013-04-26 13:03 |显示全部楼层
#!/usr/bin/expect -f
set passwd [lindex $argv 1]
set ipaddress [lindex $argv 0]
set timeout 10

spawn ssh -q alne@$ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}

set timeout 1 # 这里重新定义timeout 时间就可以了,笨办法
expect "\$"
send "uptime \r"
send "exit \r"
expect eof
exit

论坛徽章:
0
发表于 2013-04-27 22:52 |显示全部楼层

论坛徽章:
0
发表于 2013-04-28 10:26 |显示全部楼层
回复 2# steveneast


    可是下面在执行命令的时候等待时间是按上面的10秒还是下面的1秒呢?

论坛徽章:
0
发表于 2013-04-28 11:47 |显示全部楼层
set  timeout  -1     只有在成功expect ,就会send 。

论坛徽章:
0
发表于 2013-04-28 16:45 |显示全部楼层
回复 5# hongjie7456


    还是不行

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
发表于 2013-05-02 10:53 |显示全部楼层
#!/usr/local/bin/expect -f
set passwd [lindex $argv 1]
set ipaddress [lindex $argv 0]
set timeout 10

spawn ssh -l bizcard $ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}
expect "*\$"
send "uptime\r"
expect "*\$"
send "exit\r"
expect eof
exit
试试这个,应该是没问题了。

论坛徽章:
4
CU大牛徽章
日期:2013-04-17 11:50:25CU大牛徽章
日期:2013-04-17 11:50:33CU大牛徽章
日期:2013-04-17 11:50:39CU大牛徽章
日期:2013-04-17 11:50:44
发表于 2013-05-04 13:46 |显示全部楼层
超时时间设置短一点不可以么?

论坛徽章:
0
发表于 2013-05-06 11:17 |显示全部楼层
回复 9# xiaodylan


    那样的话如果执行大点的脚本或是安装软件的话就会因为超时自动断开
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP