Hi all,

the following is the result of executing a command on cli of a machine 192.148.2.10:

PGK:1200>> show employee record
show employee record
EmIL   Desig     Street               Street2         Occupation     Avialability    
------  -----    -------------  -------------         ----------  ------------- -----------
   3222 HOD      MorganLayout          HB colony       Engineer       Yes   
   3321 Read    Banker's colony       James Yard       Engineer       Yes   
   3432 Lect   Michigen Compound      Morice Villa     BioTech        Yes

If I run a script that executes the command by doing remote ssh to this machine this is what happens:
First i do ssh and get the ssh handler : this handler is "child"
Then I send the command using the child as follows:

child.sendline("show employee record)

and I return:

return child.before

My problem is I am not getting the full out put.
I am getting only

show employee record
show employee record

as the out put.
What should be done to get the full out put, ie including all the records/
Any help would be valuable for me.

Regards,
prashanth

Recommended Answers

All 2 Replies

this is a little complicated but I think the first thing you need to do is give whatever you what to have printed out a variable like z. Then your code should be something like this you may have to play with it a little depending on what verision of python you are using.[
x = input
if x = 'return child.before':
print(z)]

if you play with that for a little bit you should get what you want
good luck

You could check up this code if it gives you any inspiration how to do ssh in Python. It is from old thread: Connecting to a Linux Machine via SSH but has no code tags so here it is with those:

# Login to ABC Machine via SSH
    child = pexpect.spawn('ssh -l %s %s'%(ABCusername, ABChostname))
    i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
    if i == 0: # Timeout
        print 'ERROR! could not login with SSH. Here is what SSH said:'
        print child.before, child.after
        print str(child)
        sys.exit (1)
    if i == 1: # In this case SSH does not have the public key cached.
        child.sendline ('yes')
        child.expect ('(?i)password')
    if i == 2:
        # This may happen if a public key was setup to automatically login.
        pass
    if i == 3:
        child.sendline(ABCpassword)
        # Now we are either at the command prompt or
        # the login process is asking for our terminal type.
        i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT])
        if i == 1:
            child.sendline (TERMINAL_TYPE)
            child.expect (COMMAND_PROMPT)

    # Set command prompt to something more unique.

    COMMAND_PROMPT = "\[PEXPECT\]\$ "
    child.sendline ("PS1='[PEXPECT]\$ '") # In case of sh-style
    i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
    if i == 0:
        print "# Couldn't set sh-style prompt -- trying csh-style."
        child.sendline ("set prompt='[PEXPECT]\$ '")
        i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
        if i == 0:
            print "Failed to set command prompt using sh or csh style."
            print "Response was:"
            print child.before
            sys.exit (1)

  # Now you can do scp as follows:

  child.sendline('scp ~/sample.txt %s@%s:/sample.txt'%(XYZusername, XYZhostname))
    i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
    if i == 0: # Timeout
        print 'ERROR! could not copy with SCP. Here is what SCP said:'
        print child.before, child.after
        print str(child)
        sys.exit (1)
    if i == 1: # In this case SSH does not have the public key cached.
        child.sendline ('yes')
        child.expect ('(?i)password')
    if i == 2:
        # This may happen if a public key was setup to automatically login.
        pass
    if i == 3:
        child.sendline(XYZpassword)
        child.expect(COMMAND_PROMPT)
        print child.before

### All u r doing is; connecting to a machine called ABC and from there, u r doing scp to any required machine XYZ.

### I hope it helps u.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.