2019-05-04 16:58:46 +00:00
|
|
|
# A simple example code for wexpect
|
2019-04-24 13:55:26 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
2019-05-03 11:09:36 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
wexpectPath = os.path.dirname(here)
|
|
|
|
sys.path.insert(0, wexpectPath)
|
|
|
|
|
2019-04-24 13:55:26 +00:00
|
|
|
import wexpect
|
|
|
|
|
|
|
|
# Path of cmd executable:
|
2019-05-05 21:05:00 +00:00
|
|
|
cmdPathes = [r'C:\\Windows\\System32\\cmd.exe', 'cmd.exe', 'cmd']
|
2019-04-24 13:55:26 +00:00
|
|
|
cmdPrompt = '>'
|
|
|
|
|
2019-05-04 16:58:46 +00:00
|
|
|
for cmdPath in cmdPathes:
|
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(cmdPath)
|
2019-04-24 13:55:26 +00:00
|
|
|
|
2019-05-04 16:58:46 +00:00
|
|
|
# Wait for prompt
|
|
|
|
p.expect(cmdPrompt)
|
2019-04-24 13:55:26 +00:00
|
|
|
|
2019-05-04 16:58:46 +00:00
|
|
|
# print the texts
|
|
|
|
print(p.before, end='')
|
|
|
|
print(p.match.group(0), end='')
|
2019-04-24 13:55:26 +00:00
|
|
|
|
2019-05-04 16:58:46 +00:00
|
|
|
# Send a command
|
|
|
|
p.sendline('ls')
|
|
|
|
p.expect(cmdPrompt)
|
2019-04-24 13:55:26 +00:00
|
|
|
|
2019-05-04 16:58:46 +00:00
|
|
|
# print the texts
|
|
|
|
print(p.before, end='')
|
|
|
|
print(p.match.group(0), end='')
|
2019-04-24 13:55:26 +00:00
|
|
|
|