-
Notifications
You must be signed in to change notification settings - Fork 1
/
setbackground
executable file
·172 lines (124 loc) · 5.23 KB
/
setbackground
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
#
# Copyright (c) 2020 Gareth Palmer <gareth.palmer3@gmail.com>
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
import sys
import os.path
import re
import getopt
import traceback
from html import escape
import requests
import requests.auth
import requests.adapters
class ProgramError(Exception):
pass
class HTTPSAdapter(requests.adapters.HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
kwargs['assert_hostname'] = False
super().init_poolmanager(*args, **kwargs)
def set_background(hostname, port, timeout, username, password, certificate_file, image, icon):
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<setBackground>' \
'<background>' \
'<image>' + escape(image) + '</image>' \
'<icon>' + escape(icon) + '</icon>' \
'</background>' \
'</setBackground>'
if username != '':
auth = requests.auth.HTTPBasicAuth(username, password)
else:
auth = None
scheme = 'https' if certificate_file else 'http'
try:
session = requests.Session()
session.mount('https://', HTTPSAdapter())
session.post(f'{scheme}://{hostname}:{port}/CGI/Execute',
timeout = timeout, auth = auth, verify = certificate_file, data = {'XML': xml})
except requests.RequestException as error:
raise ProgramError(error)
print(f'Set background to {image}')
def main():
try:
short_options = 'h:t:u:p:c:H'
long_options = ['host=', 'timeout=', 'username=', 'password=', 'certificate=', 'help']
try:
options, arguments = getopt.gnu_getopt(sys.argv[1:], short_options, long_options)
except getopt.GetoptError as error:
raise ProgramError(error.msg[0].upper() + error.msg[1:] +
'. Try \'' + os.path.basename(sys.argv[0]) + ' --help\' for more information')
hostname = None
port = None
timeout = 10
username = ''
password = ''
certificate_file = None
help = False
for option, argument in options:
if option in ('-h', '--host'):
hostname = argument
if ':' in hostname:
hostname, port = hostname.rsplit(':', maxsplit = 1)
try:
port = int(port)
if port < 1 or port > 65535:
raise ValueError
except ValueError:
raise ProgramError(f'Invalid port: {port}')
if not re.search(r'(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', hostname):
raise ProgramError(f'Invalid host: {hostname}')
elif option in ('-t', '--timeout'):
timeout = argument
try:
timeout = int(timeout)
if timeout < 1:
raise ValueError
except ValueError:
raise ProgramError(f'Invalid timeout: {timeout}')
elif option in ('-u', '--username'):
username = argument
elif option in ('-p', '--password'):
password = argument
elif option in ('-c', '--certificate'):
certificate_file = argument
elif option in ('-H', '--help'):
help = True
if help:
print('Usage: ' + os.path.basename(sys.argv[0]) + ' [OPTIONS] IMAGE ICON\n'
'Set the background image on a Cisco IP Phone.\n'
'\n'
' -h, --host HOST[:PORT] host name or IP address and port of the phone\n'
' -t, --timeout TIMEOUT connection timeout in seconds (default 10)\n'
' -u, --username USERNAME authentication username\n'
' -p, --password PASSWORD authentication password\n'
' -c, --certificate CERT-FILE connect using SSL and verify using certificate\n'
' -H, --help print this help and exit\n'
'\n'
'IMAGE and ICON are either an http: or https: URL.\n'
'ICON is the thumbnail preview of the image.\n')
return
if len(arguments) < 1:
raise ProgramError('No image provided')
image = arguments[0]
if not re.search(r'(?x) ^ https?:// ', image):
raise ProgramError(f'Invalid image: {image}')
if len(arguments) < 2:
raise ProgramError('No icon provided')
icon = arguments[1]
if not re.search(r'(?x) ^ https?:// ', icon):
raise ProgramError(f'Invalid icon: {icon}')
if hostname is None:
raise ProgramError('No host specified')
if port is None:
port = 443 if certificate_file else 80
set_background(hostname, port, timeout, username, password, certificate_file, image, icon)
except ProgramError as error:
print(str(error), file = sys.stderr)
exit(1)
except Exception:
traceback.print_exc(file = sys.stderr)
exit(1)
exit(0)
if __name__ == '__main__':
main()