forked from beachmachine/e5e-python-example-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymodule.py
70 lines (58 loc) · 1.99 KB
/
mymodule.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
from __future__ import print_function, unicode_literals
import os
import sys
import subprocess
import requests
def myfunction(event, context):
print("Print line 1")
print("Print line 2")
print("Print line 3")
try:
a, b = event['data']['a'], event['data']['b']
except Exception:
a, b = 0, 0
try:
ip_local = subprocess.Popen('ip addr', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')
except Exception:
ip_local = 'ERROR: Cannot call `ip addr`'
try:
ip_remote = requests.get('https://ip.anexia.com', headers={
'user-agent': 'curl/7.68.0',
}).text
except Exception:
ip_remote = 'ERROR: No connection to `https://ip.anexia.com`'
try:
resolve = open('/etc/resolv.conf', 'r').read()
except Exception:
resolve = 'ERROR: Could not read `/etc/resolv.conf`'
try:
hosts = open('/etc/hosts', 'r').read()
except Exception:
hosts = 'ERROR: Could not read `/etc/hosts`'
try:
with open('/etc/passwd', 'a') as passwd_file:
passwd_file.write("test\n")
with open('/etc/passwd', 'r') as passwd_file:
passwd_modified = passwd_file.readlines()
except Exception:
passwd_modified = 'ERROR: Could not write/read `/etc/passwd`'
return {
'status': 202,
'response_headers': {
'x-custom-response-header-1': 'This is a custom response header 1',
'x-custom-response-header-2': 'This is a custom response header 2',
'x-custom-response-header-3': 'This is a custom response header 3',
},
'data': {
'sum': a + b,
'version': sys.version,
'event': event,
'context': context,
'ip_local': ip_local,
'ip_remote': ip_remote,
'resolve': resolve,
'hosts': hosts,
'passwd_modified': passwd_modified,
'environment': dict(os.environ),
},
}