forked from raczben/wexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_wrapper.py
47 lines (33 loc) · 965 Bytes
/
cmd_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# A simple example code for wexpect
from __future__ import print_function
import sys
import os
import re
here = os.path.dirname(os.path.abspath(__file__))
wexpectPath = os.path.dirname(here)
import wexpect
# Path of cmd executable:
cmd_exe = 'cmd'
# The prompt should be more sophisticated than just a '>'.
cmdPrompt = re.compile('[A-Z]\:.+>')
# Start the child process
p = wexpect.spawn(cmd_exe)
# Wait for prompt
p.expect(cmdPrompt, timeout = 5)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
while True:
# Wait and run a command.
command = input()
p.sendline(command)
try:
# Wait for prompt
p.expect(cmdPrompt)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
except wexpect.EOF:
# The program has exited
print('The program has exied... BY!')
break