Python Forum
Pexpect - Python 3.7 - Unable to get my script past the login stage
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pexpect - Python 3.7 - Unable to get my script past the login stage
#1
For the life of me, I cannot get my script to run. Do I need to do something with the bytes? Script runs in python 2.x. I always get the output below. I have googled for hours and I'm sure it's probably something simple that I am missing. Any insight would be greatly appreciated.

<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('#')


The code:

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()
Reply
#2
What do you mean by cannot get the script to run?
If you get an error, post the full error traceback message in error tags.
Reply
#3
There is no error, per se. It just fails to login and return that output, posted above, which i ghess is some sort of response from the pexpect module.
Reply
#4
(Nov-01-2018, 09:51 AM)j.crater Wrote: What do you mean by cannot get the script to run?
If you get an error, post the full error traceback message in error tags.

Running it again, just now. Some way, I get the following:
The authenticity of host '0 (0.0.0.0)' can't be established.

I am entering a valid IP address in the script, but somehow it is lost and being replaced with 0.0.0.0.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Pexpect - SSH Timeout Nirmal 3 6,812 Oct-17-2018, 02:35 PM
Last Post: wavic
  Python script hangs after entering username to remote login using telnetlib auto 0 4,803 Sep-10-2018, 01:10 PM
Last Post: auto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020