Advertisement
Guest User

python

a guest
Sep 7th, 2017
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. rdpy.core.error.RDPSecurityNegoFail: negotiation failure code 5
  2. -------------------------------------------------------------------------------
  3. -> from rdpy.core.error import RDPSecurityNegoFail
  4.  
  5. 38 class RDPScreenShotFactory(rdp.ClientFactory):
  6. 39 """
  7. 40 @summary: Factory for screenshot exemple
  8. 41 """
  9. 42 __INSTANCE__ = 0
  10. 43 __STATE__ = []
  11. 44 def __init__(self, reactor, app, width, height, path, timeout):
  12. 45 """
  13. 46 @param reactor: twisted reactor
  14. 47 @param width: {integer} width of screen
  15. 48 @param height: {integer} height of screen
  16. (Pdb)
  17. 49 @param path: {str} path of output screenshot
  18. 50 @param timeout: {float} close connection after timeout s without any updating
  19. 51 """
  20. 52 RDPScreenShotFactory.__INSTANCE__ += 1
  21. 53 self._reactor = reactor
  22. 54 self._app = app
  23. 55 self._width = width
  24. 56 self._height = height
  25. 57 self._path = path
  26. 58 self._timeout = timeout
  27. 59 #NLA server can't be screenshooting
  28. (Pdb)
  29. 60 self._security = rdp.SecurityLevel.RDP_LEVEL_SSL
  30. 61
  31. 62 def clientConnectionLost(self, connector, reason):
  32. 63 """
  33. 64 @summary: Connection lost event
  34. 65 @param connector: twisted connector use for rdp connection (use reconnect to restart connection)
  35. 66 @param reason: str use to advertise reason of lost connection
  36. 67 """
  37. 68 if reason.type == RDPSecurityNegoFail and self._security != "rdp":
  38. 69 log.info("due to RDPSecurityNegoFail try standard security layer")
  39. 70 self._security = rdp.SecurityLevel.RDP_LEVEL_RDP
  40. 71 connector.connect()
  41. 72 return
  42. 73
  43. 74 log.info("connection lost : %s"%reason)
  44. 75 RDPScreenShotFactory.__STATE__.append((connector.host, connector.port, reason))
  45. 76 RDPScreenShotFactory.__INSTANCE__ -= 1
  46. 77 if(RDPScreenShotFactory.__INSTANCE__ == 0):
  47. 78 self._reactor.stop()
  48. 79 self._app.exit()
  49. 80
  50. 81 def clientConnectionFailed(self, connector, reason):
  51. (Pdb)
  52. 82 """
  53. 83 @summary: Connection failed event
  54. 84 @param connector: twisted connector use for rdp connection (use reconnect to restart connection)
  55. 85 @param reason: str use to advertise reason of lost connection
  56. 86 """
  57. 87 log.info("connection failed : %s"%reason)
  58. 88 RDPScreenShotFactory.__STATE__.append((connector.host, connector.port, reason))
  59. 89 RDPScreenShotFactory.__INSTANCE__ -= 1
  60. 90 if(RDPScreenShotFactory.__INSTANCE__ == 0):
  61. 91 self._reactor.stop()
  62. 92 self._app.exit()
  63. (Pdb)
  64. 93
  65. 94
  66. 95 def buildObserver(self, controller, addr):
  67. 96 """
  68. 97 @summary: build ScreenShot observer
  69. 98 @param controller: RDPClientController
  70. 99 @param addr: address of target
  71. 100 """
  72. 101 class ScreenShotObserver(rdp.RDPClientObserver):
  73. 102 """
  74. 103 @summary: observer that connect, cache every image received and save at deconnection
  75. (Pdb)
  76. 104 """
  77. 105 def __init__(self, controller, width, height, path, timeout, reactor):
  78. 106 """
  79. 107 @param controller: {RDPClientController}
  80. 108 @param width: {integer} width of screen
  81. 109 @param height: {integer} height of screen
  82. 110 @param path: {str} path of output screenshot
  83. 111 @param timeout: {float} close connection after timeout s without any updating
  84. 112 @param reactor: twisted reactor
  85. 113 """
  86. 114 rdp.RDPClientObserver.__init__(self, controller)
  87. (Pdb)
  88. 115 self._buffer = QtGui.QImage(width, height, QtGui.QImage.Format_RGB32)
  89. 116 self._path = path
  90. 117 self._timeout = timeout
  91. 118 self._startTimeout = False
  92. 119 self._reactor = reactor
  93. 120
  94. 121 def onUpdate(self, destLeft, destTop, destRight, destBottom, width, height, bitsPerPixel, isCompress,
  95. data):
  96. 122 """
  97. 123 @summary: callback use when bitmap is received
  98. 124 """
  99. 125 image = RDPBitmapToQtImage(width, height, bitsPerPixel, isCompress, data);
  100. (Pdb)
  101. 126 with QtGui.QPainter(self._buffer) as qp:
  102. 127 #draw image
  103. 128 qp.drawImage(destLeft, destTop, image, 0, 0, destRight - destLeft + 1, destBottom - destTop +
  104. 1)
  105. 129 if not self._startTimeout:
  106. 130 self._startTimeout = False
  107. 131 self._reactor.callLater(self._timeout, self.checkUpdate)
  108. 132
  109. 133 def onReady(self):
  110. 134 """
  111. 135 @summary: callback use when RDP stack is connected (just before received bitmap)
  112. 136 """
  113. (Pdb)
  114. 137 log.info("connected %s"%addr)
  115. 138
  116. 139 def onSessionReady(self):
  117. 140 """
  118. 141 @summary: Windows session is ready
  119. 142 @see: rdp.RDPClientObserver.onSessionReady
  120. 143 """
  121. 144 pass
  122. 145
  123. 146 def onClose(self):
  124. 147 """
  125. (Pdb)
  126. 148 @summary: callback use when RDP stack is closed
  127. 149 """
  128. 150 log.info("save screenshot into %s"%self._path)
  129. 151 self._buffer.save(self._path)
  130. 152
  131. 153 def checkUpdate(self):
  132. 154 self._controller.close();
  133. 155
  134. 156 controller.setScreen(width, height);
  135. 157 controller.setSecurityLevel(self._security)
  136. 158 return ScreenShotObserver(controller, self._width, self._height, self._path, self._timeout, self._reactor
  137.  
  138. (Pdb)
  139. 159
  140. 160 def main(width, height, path, timeout, hosts):
  141. 161 """
  142. 162 @summary: main algorithm
  143. 163 @param height: {integer} height of screenshot
  144. 164 @param width: {integer} width of screenshot
  145. 165 @param timeout: {float} in sec
  146. 166 @param hosts: {list(str(ip[:port]))}
  147. 167 @return: {list(tuple(ip, port, Failure instance)} list of connection state
  148. 168 """
  149. 169 #create application
  150. (Pdb)
  151. 170 app = QtGui.QApplication(sys.argv)
  152. 171
  153. 172 #add qt4 reactor
  154. 173 import qt4reactor
  155. 174 qt4reactor.install()
  156. 175
  157. 176 from twisted.internet import reactor
  158. 177
  159. 178 for host in hosts:
  160. 179 if ':' in host:
  161. 180 ip, port = host.split(':')
  162. (Pdb)
  163. 181 else:
  164. 182 ip, port = host, "3389"
  165. 183
  166. 184 reactor.connectTCP(ip, int(port), RDPScreenShotFactory(reactor, app, width, height, path + "%s.jpg"%ip, t
  167. meout))
  168. 185
  169. 186 reactor.runReturn()
  170. 187 app.exec_()
  171. 188 return RDPScreenShotFactory.__STATE__
  172. 189
  173. 190 def help():
  174. 191 print "Usage: rdpy-rdpscreenshot [options] ip[:port]"
  175. (Pdb)
  176. 192 print "\t-w: width of screen default value is 1024"
  177. 193 print "\t-l: height of screen default value is 800"
  178. 194 print "\t-o: file path of screenshot default(/tmp/rdpy-rdpscreenshot.jpg)"
  179. 195 print "\t-t: timeout of connection without any updating order (default is 2s)"
  180. 196
  181. 197 if __name__ == '__main__':
  182. 198 #default script argument
  183. 199 width = 1024
  184. 200 height = 800
  185. 201 path = "/tmp/"
  186. 202 timeout = 5.0
  187. (Pdb)
  188. 203
  189. 204 try:
  190. 205 opts, args = getopt.getopt(sys.argv[1:], "hw:l:o:t:")
  191. 206 except getopt.GetoptError:
  192. 207 help()
  193. 208 for opt, arg in opts:
  194. 209 if opt == "-h":
  195. 210 help()
  196. 211 sys.exit()
  197. 212 elif opt == "-w":
  198. 213 width = int(arg)
  199. (Pdb)
  200. 214 elif opt == "-l":
  201. 215 height = int(arg)
  202. 216 elif opt == "-o":
  203. 217 path = arg
  204. 218 elif opt == "-t":
  205. 219 timeout = float(arg)
  206. 220
  207. 221 main(width, height, path, timeout, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement