Nov-01-2018, 09:16 AM
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:
<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()