Pexpect-Python 3.7-无法使我的脚本超出登录阶段

时间:2018-11-01 09:00:47

标签: python python-3.x ssh pexpect switching

在我生命中,我无法运行脚本。我需要对字节做些事情吗?脚本在python 2.x中运行。我总是得到下面的输出。我已经搜索了几个小时,我敢肯定我可能想念的只是简单的事情。任何见识将不胜感激。

<pexpect.pty_spawn.spawn object at 0x7f73ee897470>
command: /usr/bin/ssh
args: [b'/usr/bin/ssh', b'<username>', b'-q', b'-oStrictHostKeyChecking=no', b'-oUserKnownHostsFile=/dev/null', b'-oPubkeyAuthentication=no']
buffer (last 100 chars): ''

after: <class 'pexpect.exceptions.EOF'>
match: None
match_index: None
exitstatus: 255
flag_eof: True
pid: 18756
child_fd: 6
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: re.compile('password:')
1: re.compile('#')

代码:

在我学习时,请原谅不是最佳做法的任何事情。

import ipaddress
import pexpect
import sys
import re
import time
import getpass


class RemoteValidations(object):
    def __init__(self):
        self.PING_ARRAY = []

    #Function takes the IP address of MDF1 and increments it by one for every other switch on the network.
    def get_IPs(self):
        mdf1 = input("IP: ")

        first_ip = ipaddress.ip_address(mdf1)

        switch_count = int(input("Total number of switches?: "))

        if switch_count == 3:
            mdf2 = first_ip + 1
            self.PING_ARRAY.append(str(mdf2))

            idfb = first_ip + 2
            self.PING_ARRAY.append(str(idfb))

            idfc = first_ip + 2
            self.PING_ARRAY.append(str(idfc))

        if switch_count == 4:
            mdf2 = first_ip + 1
            self.PING_ARRAY.append(str(mdf2))

            idfb = first_ip + 2
            self.PING_ARRAY.append(str(idfb))

            idfc = first_ip + 2
            self.PING_ARRAY.append(str(idfc))

            idfd = first_ip + 2
            self.PING_ARRAY.append(str(idfd))

    #Function takes the array of addresses above and attempts to ping each host.
    def ping_switches(self):
        un = input("Username: ")
        options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'
        pw = getpass.getpass("Password: ")
        prompt = '#'

        try:
            for s, i in enumerate(self.PING_ARRAY):
                connection_string = ("ssh %s@%s %s" % (un, s, options))
                child = pexpect.spawnu(connection_string, timeout=30)
                child.logfile = sys.stdout
                i = child.expect([re.escape('password:'), prompt])

                if i == 0:
                    time.sleep(2)
                    child.sendline(pw)
                    time.sleep(2)
                elif i == 1:
                    child.sendline('ping {}'.format(i))
                    time.sleep(2)
        except Exception as e:
            print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)


def main():
    r = RemoteValidations()
    r.get_IPs()
    r.ping_switches()


if __name__ == "__main__":
    main()

0 个答案:

没有答案