-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.py
55 lines (42 loc) · 1.32 KB
/
connection.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
import socket
import time
import subprocess
import logging
import settings
import getpass
import os
import datetime
class ConnectionFailedError(Exception):
pass
def is_address_accessible(address):
try:
s = socket.create_connection((address, settings.RDP_PORT), settings.CONNECTION_TEST_TIMEOUT)
s.close()
except socket.error, e:
logging.error("Connection to '%s:%s' has failed (%s)", address, settings.RDP_PORT, e)
return False
return True
def rdp_connect(address, user="", domain="", password="", dualmon=False):
if not is_address_accessible(address):
raise ConnectionFailedError()
cmdline = [settings.XFREERDP_BIN]
cmdline.extend(settings.XFREERDP_STATIC_PARAMS)
if user:
cmdline.extend(["/u:%s" %(user)])
if password:
cmdline.extend(["/p:%s" %(password)])
if domain:
cmdline.extend(["/d:%s" %(domain)])
if dualmon:
cmdline.extend(["/multimon"])
cmdline.extend(["/v:%s" %(address)])
proc = subprocess.Popen(cmdline)
print cmdline
# Check if process died too quickly
time.sleep(1)
rc = proc.poll()
logging.debug("poll() returned %s", rc)
if rc is not None:
logging.error("Process had died too quickly, rc=%d", rc)
raise ConnectionFailedError()
return proc.pid