找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4541|回复: 2

[求助] 新学python,帮忙看下问题。谢谢

1

主题

2

帖子

15

积分

贫民

积分
15
wangohyes 发表于 2015-8-14 17:21:43 | 显示全部楼层 |阅读模式
Python版本3,执行后输入账号,密码和执行命令后会报错。报错就后面两个图片
#!/usr/local/python3

import pexpect
import getpass
import os
import traceback

def ssh_command(user,host,password,command):
        ssh_newkey = 'Are you sure you want to continue connecting'
        child = pexpect.spawn('ssh %s@%s "%s"'%(user,host,command),timeout=None)
        i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password:'])
        if i == 0:
                print('error')
                return None
        if i == 1:
                child.sendline('yes')
                child.expect('password:')
                i = child.expect([pexpect.TIMEOUT, 'password: '])
                if i == 0:
                                        print(child.before,child.after)
                return None
        child.sendline(password)

def main():
        user = input('User:')
        host = input('Hostip:')
        password = getpass.getpass()
        command = input('Enter the command:')
        child = ssh_command (user, host, password, command)
        child.expect(pexpect.EOF)
        print(child.before)

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

2.png 3.png



回复

使用道具 举报

1

主题

2

帖子

15

积分

贫民

积分
15
wangohyes  楼主| 发表于 2015-8-18 13:56:03 | 显示全部楼层
~~~求解答~~~
回复

使用道具 举报

4

主题

77

帖子

77

积分

版主

Rank: 7Rank: 7Rank: 7

积分
77

活跃会员热心会员默默耕耘优秀版主论坛元老

北极 发表于 2015-8-30 20:13:56 | 显示全部楼层
输入格式问题,input应该写成raw_input...
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表