-2

Can any one please tell me, what is the prompt we have to give for expecting a blank space for a python script.I need to execute a command which will execute only after it sees the expect prompt.On doing manually i got that the prompt is a blank space,and has to expect the same prompt via script also.

import pexpect import pxssh import time import os,re,sys

def ssh(ipaddr,password): try:

    ss = pexpect.spawn(ipaddr)
    print ipaddr    
        ss.logfile = open("/tmp/mynewlog", "a")
        print "SSH connecting"
        print 
except:
    print "connection refused"
    print
    #sys.exit()

    try:
        print password
        ss.expect (':')

            ss.sendline (password +"\n")
            print "connected"
            time.sleep(30)
            ss.expect (">")
            print "connection established"
            print

    except:
            print "Permission denied, please try again."
                print
                sys.exit()
    try:
        ss.sendline ('taskkill /F /IM iperf.exe\n')
        time.sleep(30)
        ss.expect ('>')
            ss.sendline ('cd /D D:\iperf-2.0.5-2-win32\n')
            ss.expect ('>')
            ss.sendline ('iperf -s\n')#########This command i need to execute and i'm  expecting a blank space as prompt
            ss.sendline (pexpect.EOF)
            print "END"
            ssh()  

except: print "Failed to execute the command"
print sys.exit()

abhi
  • 368
  • 1
  • 4
  • 14
  • Welcome to stackoverflow. I think your question is a little obscure for most of us. It looks like you're using the `pexpect` package, but most people who are looking at this question have probably never used it. It might help if you could edit your question to give us an example of what you've tried, and explain more clearly what you expect to happen. – Mike Apr 25 '13 at 12:45
  • Thanks mike..i will update my question – abhi Apr 25 '13 at 13:01
  • 1
    @abhi I would now also update your question with answers defining what Mike's answer below offers. You will help others in the future and also get an answer, that way. – TryTryAgain Apr 25 '13 at 16:52
  • Please edit your question with properly indented code. Try to convert the tabs into spaces before pasting, and after you have pasted, press Ctrl+K to format the block of code. – nhahtdh Apr 26 '13 at 01:08

1 Answers1

1

Edit:

ss.expect takes any string as a regular expression, and uses it to search the output from a program. In your case, if you want to match the line before all the dashes, you can do

ss.expect(r'TCP window size: .*yte \(default\)')

More broadly, I'm not sure what you're trying to accomplish with this, but it might be helpful to look at this page.


Old:

I'm not sure what you mean when you say that you expect a blank space as prompt.

1) Is it really a space? If so ss.expect(' ') seems like it should work.

2) Is there an end of line? If so ss.expect('\n') seems like it should work.

3) Does iperf really give you a prompt? Is it really waiting for input? And you're sending an EOF? Does this kill the program?

Mike
  • 19,114
  • 12
  • 59
  • 91
  • Thanks mike for the reply i tried with ('\n'),but not working.Actually on executing iperf -s command, the prompt will be like mentioned below, ------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 64.0 KByte (default) ------------------------------------------------------------ After this, the cursor is blinking in blank space,so what i have to expect from here. – abhi Apr 26 '13 at 05:04