-
Notifications
You must be signed in to change notification settings - Fork 1
/
sap.py
146 lines (123 loc) · 4.22 KB
/
sap.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import win32com.client as __win32
import win32clipboard as __clip
## Requirements: pip install pywin32
## Documentation: https://help.sap.com/viewer/b47d018c3b9b45e897faf66a6c0885a8/760.00/en-US
def create(profile: str, inplace: bool = False) -> list:
try:
## Instantiate SAP GUI application (creating the object)
app = __win32.Dispatch("Sapgui.ScriptingCtrl.1") # GuiApplication Object
except Exception as err:
print(f"[!] Unable to create SAP GUI instance for scripting: {err.args[1]}")
return []
return __create_connection(profile, [app], inplace)
def attach(profile: str, inplace: bool = False) -> list:
try:
## Attach to a running instance of SAP GUI (getting the object)
sap = __win32.GetObject("SAPGUI")
except Exception as err:
print(f"[!] SAP Logon instance was not found: {err.args[1]}")
return []
## Getting the scripting application
app = sap.GetScriptingEngine # GuiApplication Object
if not isinstance(app, __win32.CDispatch):
sap = None
return []
return __create_connection(profile, [app], inplace)
def __create_connection(profile: str, lapp: list, inplace: bool) -> list:
## To create a new SAP GUI instance placed within your application
profile += "/INPLACE" if inplace else ""
# Public Function OpenConnection( _
# ByVal Description As String, _
# Optional ByVal Sync As Variant, _
# Optional ByVal Raise As Variant _
# ) As GuiConnection
con = lapp[0].OpenConnection(profile, True, False) # GuiConnection Object
# In this case we're opening a new connection
# however once we are getting a instance of SAP
# it's possible to get the a connecion that already exists
# like this con.Children(0)
if con is None:
sap, lapp[0] = None, None
print("Open Connection fail")
return []
print(f"{con.Description} {con.name}")
# This property is another name for the Children property
session = con.Sessions(0) # GuiSession Object
__multiple_logon(session)
print(f"{session.Info.User} {session.Info.SystemName} {session.Info.Client} {session.name}")
return [lapp[0], con, session] # SAP Connection Data
def __multiple_logon(session: object) -> None:
while session.children.count > 1:
try:
session.FindById("wnd[1]/usr/radMULTI_LOGON_OPT2").select()
session.FindById("wnd[1]/tbar[0]/btn[0]").press()
except:
session.ActiveWindow.sendVKey(vKeys["Enter"])
def close(sap_connection_data: list) -> bool:
try:
sap_connection_data[1].CloseSession(sap_connection_data[2].id)
sap_connection_data[1].CloseConnection()
sap_connection_data[0] = None
sap_connection_data[1] = None
sap_connection_data[2] = None
return True
except Exception as err:
print(f"[!] SAP Connection was not closed: {err.args[1]}")
return False
def list_to_clipboard(data: list) -> None:
text = "\r\n".join(str(e) for e in data)
__clip.OpenClipboard()
__clip.EmptyClipboard()
__clip.SetClipboardText(text)
__clip.CloseClipboard()
vKeys = {
"Enter": 0,
"F1": 1,
"F2": 2,
"F3": 3,
"F4": 4,
"F5": 5,
"F6": 6,
"F7": 7,
"F8": 8,
"F9": 9,
"F10": 10,
"Ctrl+S": 11,
"F12": 12,
"Shift+F1": 13,
"Shift+F2": 14,
"Shift+F3": 15,
"Shift+F4": 16,
"Shift+F5": 17,
"Shift+F6": 18,
"Shift+F7": 19,
"Shift+F8": 20,
"Shift+F9": 21,
"Shift+Ctrl+0": 22,
"Shift+F11": 23,
"Shift+F12": 24,
"Ctrl+F1": 25,
"Ctrl+F2": 26,
"Ctrl+F3": 27,
"Ctrl+F4": 28,
"Ctrl+F5": 29,
"Ctrl+F6": 30,
"Ctrl+F7": 31,
"Ctrl+F8": 32,
"Ctrl+F9": 33,
"Ctrl+F10": 34,
"Ctrl+F11": 35,
"Ctrl+F12": 36,
"Ctrl+Shift+F1": 37,
"Ctrl+Shift+F2": 38,
"Ctrl+Shift+F3": 39,
"Ctrl+Shift+F4": 40,
"Ctrl+Shift+F5": 41,
"Ctrl+Shift+F6": 42,
"Ctrl+Shift+F7": 43,
"Ctrl+Shift+F8": 44,
"Ctrl+Shift+F9": 45,
"Ctrl+Shift+F10": 46,
"Ctrl+Shift+F11": 47,
"Ctrl+Shift+F12": 48
}