-1

I am trying to automate the CLI menu on Linux system where at many points i have to provide based the condition.

i have prompt with ': ', I am trying to match (111.222.333.444)the string (printed in child.before) below,

 111.222.333.444
:

if the string matches then i need can send another command (sendlind). however I am not sure how to achieve this with python pexpect. if any body can explain or provide an example it would be good help.

tgcloud
  • 857
  • 6
  • 18
  • 37

1 Answers1

1

You can use regexp (import "re" library) for compact code that checks the string against expected format. This method is not sufficient to verify an IP is valid. The easiest way to check that IP is achievable is to use ping.

import re;
# fmt1 accepts only IPs with 3-digit groups: 123.123.123.123
fmt1='^([0-9]{3}\.){3}[0-9]{3}$';

# fmt2 accepts IPs with 1-3 digit groups e.g. 13.123.1.1
fmt2='^([0-9]{1,3}\.){3}[0-9]{1,3}$';

exp=re.compile(fmt1);

def chk(s):
    x=exp.match(s);
    if x:
        print(s, ' = match');
        return 1;
    else:
        print(s, ' = mismatch');
        return 0;
usv
  • 11
  • 2