wexpect-venv/examples/cmd.py

37 lines
751 B
Python
Raw Normal View History

# 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-05-03 11:09:36 +00:00
2019-04-24 13:55:26 +00:00
import wexpect
# Path of cmd executable:
2019-05-13 11:06:56 +00:00
cmdPathes = ['C:\Windows\System32\cmd.exe', 'cmd.exe', 'cmd']
2019-04-24 13:55:26 +00:00
cmdPrompt = '>'
for cmdPath in cmdPathes:
# Start the child process
p = wexpect.spawn(cmdPath)
2019-04-24 13:55:26 +00:00
# Wait for prompt
p.expect(cmdPrompt)
2019-04-24 13:55:26 +00:00
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
2019-04-24 13:55:26 +00:00
# Send a command
p.sendline('ls')
p.expect(cmdPrompt)
2019-04-24 13:55:26 +00:00
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
2019-04-24 13:55:26 +00:00