forked from openSUSE/twopence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·96 lines (83 loc) · 2.78 KB
/
example.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import twopence
import os
try:
#######################################################################
# Adapt the following line to your setup
# target = twopence::Target("virtio:/var/run/twopence/test.sock")
# target = twopence::Target("ssh:192.168.123.45")
# target = twopence::Target("serial:/dev/ttyS0")
target = twopence.Target( YOUR_TARGET_HERE )
#######################################################################
except NameError:
print '''
This Python script is meant as an example from which you could
copy and paste your own test scripts.
1 - copy this script to your home directory;
2 - modify it to declare $TARGET variable;
3 - run it to see how it works;
4 - get your inspiration from the source code.
'''
exit(1)
# We can send a command to the system under tests
print "target.run('ls -l')"
status = target.run('ls -l')
print "status=", status.code
print
# We can avoid displaying the results
print "cmd = twopence.Command('ping -c1 8.8.8.8'); ..."
cmd = twopence.Command('ping -c1 8.8.8.8')
cmd.suppressOutput()
cmd.stderr = None
status = target.run(cmd)
print "status=", status.code
print
# We don't need to process the status code
print "target.run('uname -a')"
target.run('uname -a')
print
# We should be able to pipe a local command
# to another command on the remote system
# TODO: how to do exec("ls -l") | twopence_commad("cat") ?
print "TODO: pipe a local command to a remote command"
print
# We should be able to work interactively with the remote system
# TODO: does not work
print "TODO: be able to work interactively"
print
#try:
# print "target.run('cat', timeout = 15)"
# print "(type Ctrl-D to exit, Ctrl-C to end)\n"
# status = target.run('cat', timeout = 15)
# print "status=", status.code
# print
#except SystemError:
# print "timeout"
# print
# We can redirect remote standard output and error to the same variable
print "cmd = twopence.Command('ls -l . /ooops'); ..."
cmd = twopence.Command('ls -l . /oops')
cmd.suppressOutput()
status = target.run(cmd)
print "output='", status.stdout, "'"
print "status=", status.code
# We can redirect remote standard output and error to separate variables
print "cmd = twopence.Command('find /tmp -type f'); ..."
cmd = twopence.Command('find /tmp -type f')
cmd.suppressOutput()
cmd.user = 'nobody'
cmd.stdout = bytearray()
cmd.stderr = bytearray()
status = target.run(cmd)
print "stdout='", status.stdout, "'"
print "stderr='", status.stderr, "'"
# We can inject a local file into the remote system
print "target.inject('/etc/services', 'test.txt')"
target.inject('/etc/services', 'test.txt')
print
# We can extract a file from the remote system
# and store it locally
print "target.extract('test.txt', 'etc_services')"
target.extract('test.txt', 'etc_services')
print
os.remove("etc_services")