OpenSecurity/bin/cygwin.py
changeset 167 1e1811fa44bc
parent 165 a1b7a5a48a1e
child 169 a133c8d03ef8
     1.1 --- a/OpenSecurity/bin/cygwin.py	Tue May 20 15:26:03 2014 +0200
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Thu May 22 11:00:33 2014 +0200
     1.3 @@ -1,290 +1,301 @@
     1.4 -#!/bin/env python
     1.5 -# -*- coding: utf-8 -*-
     1.6 -
     1.7 -# ------------------------------------------------------------
     1.8 -# cygwin command
     1.9 -# 
    1.10 -# executes a cygwin command inside the opensecurity project
    1.11 -#
    1.12 -# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
    1.13 -#        Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.14 -#
    1.15 -# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.16 -# AIT Austrian Institute of Technology GmbH
    1.17 -# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.18 -# http://www.ait.ac.at
    1.19 -#
    1.20 -# This program is free software; you can redistribute it and/or
    1.21 -# modify it under the terms of the GNU General Public License
    1.22 -# as published by the Free Software Foundation version 2.
    1.23 -# 
    1.24 -# This program is distributed in the hope that it will be useful,
    1.25 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.26 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.27 -# GNU General Public License for more details.
    1.28 -# 
    1.29 -# You should have received a copy of the GNU General Public License
    1.30 -# along with this program; if not, write to the Free Software
    1.31 -# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.32 -# Boston, MA  02110-1301, USA.
    1.33 -# ------------------------------------------------------------
    1.34 -
    1.35 -
    1.36 -# ------------------------------------------------------------
    1.37 -# imports
    1.38 -
    1.39 -import os
    1.40 -import subprocess
    1.41 -import sys
    1.42 -import _winreg
    1.43 -from subprocess import Popen, PIPE, STARTUPINFO, _subprocess
    1.44 -import threading
    1.45 -
    1.46 -# local
    1.47 -from environment import Environment
    1.48 -from opensecurity_util import logger, setupLogger, OpenSecurityException
    1.49 -import time
    1.50 -
    1.51 -
    1.52 -# ------------------------------------------------------------
    1.53 -# code
    1.54 -
    1.55 -def once(theClass):
    1.56 -    """get the path to our local cygwin installment"""
    1.57 -    home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
    1.58 -    path_hint = [ 
    1.59 -        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin')), 
    1.60 -        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin64')), 
    1.61 -        os.path.abspath(os.path.join(home_drive, 'cygwin')),
    1.62 -        os.path.abspath(os.path.join(home_drive, 'cygwin64'))
    1.63 -    ]
    1.64 -    path_valid = [ p for p in path_hint if os.path.exists(p) ]
    1.65 -        
    1.66 -    theClass.cygwin_root = path_valid[0]
    1.67 -    theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
    1.68 -    theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
    1.69 -    theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
    1.70 -    theClass.cygwin_scp = os.path.join(theClass.cygwin_bin, 'scp.exe')
    1.71 -    theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
    1.72 -    theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe") 
    1.73 -    """get the path to the VirtualBox installation on this system"""
    1.74 -    theClass.vbox_root = theClass.getRegEntry('SOFTWARE\Oracle\VirtualBox', 'InstallDir')[0]  
    1.75 -    theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
    1.76 -    #theClass.user_home = os.path.expanduser("~")
    1.77 -    theClass.user_home = os.environ['APPDATA']#os.path.expandvars("%APPDATA%")
    1.78 -    return theClass
    1.79 -
    1.80 -            
    1.81 -@once
    1.82 -class Cygwin(object):
    1.83 -    cygwin_root = ''
    1.84 -    cygwin_bin = ''
    1.85 -    cygwin_bash = ''
    1.86 -    cygwin_ssh = ''
    1.87 -    cygwin_x11 = ''
    1.88 -    cygwin_scp = ''
    1.89 -    vbox_root = ''
    1.90 -    vbox_man = ''
    1.91 -    win_cmd = ''
    1.92 -    user_home = ''
    1.93 -    """Some nifty methods working with Cygwin"""
    1.94 -    
    1.95 -    def __call__(self, command, arguments, wait_return=True, window = False):
    1.96 -        """make an instance of this object act as a function"""
    1.97 -        return self.execute(command, arguments, wait_return, window)
    1.98 -
    1.99 -    @staticmethod
   1.100 -    def getRegEntry(key, value):
   1.101 -        try:
   1.102 -            k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
   1.103 -            value = _winreg.QueryValueEx(k, value)
   1.104 -            _winreg.CloseKey(k)
   1.105 -            return value
   1.106 -        except:
   1.107 -            pass
   1.108 -    
   1.109 -            
   1.110 -    @staticmethod
   1.111 -    def root():
   1.112 -        return Cygwin.cygwin_root
   1.113 -
   1.114 -    @staticmethod
   1.115 -    def bin():
   1.116 -        return Cygwin.cygwin_bin
   1.117 -    
   1.118 -    @staticmethod
   1.119 -    def bash():
   1.120 -        return Cygwin.cygwin_bash
   1.121 -    
   1.122 -    @staticmethod    
   1.123 -    def ssh():
   1.124 -        return Cygwin.cygwin_ssh
   1.125 -    
   1.126 -    @staticmethod    
   1.127 -    def scp():
   1.128 -        return Cygwin.cygwin_scp
   1.129 -
   1.130 -    @staticmethod    
   1.131 -    def x11():
   1.132 -        return Cygwin.cygwin_x11
   1.133 -    
   1.134 -    @staticmethod
   1.135 -    def vboxman():
   1.136 -        return Cygwin.vbox_man
   1.137 -    
   1.138 -    @staticmethod
   1.139 -    def cmd():
   1.140 -        return Cygwin.win_cmd
   1.141 -    
   1.142 -    @staticmethod
   1.143 -    def home():
   1.144 -        return Cygwin.user_home
   1.145 -    
   1.146 -    executeLock = threading.Lock()
   1.147 -    #executes command on host system
   1.148 -    @staticmethod
   1.149 -    def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE):
   1.150 -        _startupinfo = STARTUPINFO()
   1.151 -        if not window:
   1.152 -            _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
   1.153 -            _startupinfo.wShowWindow = _subprocess.SW_HIDE
   1.154 -
   1.155 -            #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
   1.156 -        res_stderr = None
   1.157 -        try:
   1.158 -            # quote the executable otherwise we run into troubles
   1.159 -            # when the path contains spaces and additonal arguments
   1.160 -            # are presented as well.
   1.161 -            # special: invoking bash as login shell here with
   1.162 -            # an unquoted command does not execute /etc/profile
   1.163 -            args = '"' + program + '" ' + arguments
   1.164 -            process = Popen(args, startupinfo = _startupinfo, stdin = stdin, stdout = stdout, stderr = stderr, shell = False)
   1.165 -            logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
   1.166 -            if not wait_return:
   1.167 -                return [0, 'working in background', '']
   1.168 -            result = process.wait()
   1.169 -            res_stdout = process.stdout.read();
   1.170 -            res_stderr = process.stderr.read();
   1.171 -
   1.172 -        except Exception as ex:
   1.173 -            res_stderr = ''.join(str(ex.args))
   1.174 -            result = 1 
   1.175 -            
   1.176 -        return result, res_stdout, res_stderr
   1.177 -    
   1.178 -    @staticmethod
   1.179 -    def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
   1.180 -        retry = 0
   1.181 -        result = None
   1.182 -        while retry < 3:
   1.183 -            if Cygwin.executeLock.acquire(True):
   1.184 -                result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
   1.185 -                Cygwin.executeLock.release()
   1.186 -                if result[0] == 0:
   1.187 -                    return result
   1.188 -                retry+=1
   1.189 -        return result
   1.190 -
   1.191 -
   1.192 -    @staticmethod
   1.193 -    def bashExecute(command, wait_return=True, window = False, bash_opts='', stdin = PIPE, stdout = PIPE, stderr = PIPE):
   1.194 -        # for some reason, the '-l' is ignored when started via python
   1.195 -        # so the same behavior is triggered by calling /etc/profile 
   1.196 -        # directly
   1.197 -        command = bash_opts + ' -l -c "'  + command + '"'
   1.198 -        return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window, stdin = stdin, stdout = stdout, stderr = stderr)
   1.199 -    
   1.200 -    @staticmethod
   1.201 -    def cmdExecute(command, wait_return=True, window = False):
   1.202 -        command = ' /c ' + command 
   1.203 -        return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
   1.204 -
   1.205 -    # executes command over ssh on guest vm
   1.206 -    @staticmethod
   1.207 -    def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
   1.208 -        command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
   1.209 -        return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
   1.210 -    
   1.211 -    #machineFolder + '/' + vm_name + '/dvm_key
   1.212 -    #address = self.getHostOnlyIP(vm_name)
   1.213 -    #machineFolder = self.getDefaultMachineFolder()
   1.214 -    #machineFolder = Cygwin.cygwinPath(machineFolder)
   1.215 -    
   1.216 -    # executes command over ssh on guest vm with X forwarding
   1.217 -    @staticmethod
   1.218 -    def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
   1.219 -        return Cygwin.bashExecute('DISPLAY=:0.0 ssh -Y -o StrictHostKeyChecking=no -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '')
   1.220 -
   1.221 -    @staticmethod
   1.222 -    def is_X11_running():
   1.223 -        """check if we can connect to a X11 running instance"""
   1.224 -        p = Cygwin.bashExecute('xset -display :0 q', wait_return = True, window = False) 
   1.225 -        return p[0] == 0
   1.226 -        
   1.227 -    @staticmethod
   1.228 -    def start_X11():
   1.229 -        """start X11 in the background (if not already running) on DISPLAY=:0
   1.230 -        
   1.231 -        If there is already a X11 running then exit silently, calling this
   1.232 -        method as often as needed.
   1.233 -        """
   1.234 -        Popen('"' + Cygwin.cygwin_x11 + '" :0 -multiwindow -resize -silent-dup-error')
   1.235 -        return (0, None, None)
   1.236 -    
   1.237 -    @staticmethod    
   1.238 -    def cygPath(path):
   1.239 -        cmd = 'cygpath -u \'' + path + '\''
   1.240 -        return Cygwin.bashExecute(cmd)[1].rstrip('\n')
   1.241 -    
   1.242 -    @staticmethod
   1.243 -    def checkResult(result):
   1.244 -        if result[0] != 0:
   1.245 -            logger.error('Command failed:' + ''.join(result[2]))
   1.246 -            raise OpenSecurityException('Command failed:' + ''.join(result[2]))
   1.247 -        return result
   1.248 -                
   1.249 -# start
   1.250 -import os
   1.251 -import win32api
   1.252 -import win32con
   1.253 -import win32security
   1.254 -
   1.255 -if __name__ == "__main__":
   1.256 -    logger = setupLogger('Cygwin')
   1.257 -    c = Cygwin()
   1.258 -    #logger.info(c.root())
   1.259 -    #logger.info(c.bin())
   1.260 -    #logger.info(c.bash())
   1.261 -    #logger.info(c.ssh())
   1.262 -    #logger.info(c.x11())
   1.263 -    #logger.info(c.home())   
   1.264 -    
   1.265 -    #PSEXEC -i -s -d CMD
   1.266 -    #tasklist /v /fo list /fi "IMAGENAME eq explorer.exe"
   1.267 -    
   1.268 -    #runner = XRunner()
   1.269 -    #runner.start()
   1.270 -    
   1.271 -    #Cygwin.start_X11()
   1.272 -    
   1.273 -    
   1.274 -            
   1.275 -    #time.sleep(500)
   1.276 -    
   1.277 -    #Cygwin.start_X11()
   1.278 -    #print (Cygwin.is_X11_running())
   1.279 -    #print (Cygwin.is_X11_running())
   1.280 -    #new_sdvm = 'SecurityDVM0'
   1.281 -    #new_ip = Cygwin.vboxExecute('guestproperty get ' + new_sdvm + ' /VirtualBox/GuestInfo/Net/0/V4/IP')[1]
   1.282 -    #new_ip = new_ip[new_ip.index(':')+1:].strip()
   1.283 -    #new_ip = '+'
   1.284 -    #result = Cygwin.bashExecute('DISPLAY=:0.0 xhost '+new_ip)
   1.285 -    #browser = '/usr/bin/midori '
   1.286 -    #print(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', '/cygdrive/c/Users/BarthaM/VirtualBox VMs' + '/' + new_sdvm + '/dvm_key'))
   1.287 -            
   1.288 -    #print(Cygwin.bashExecute('echo $PATH')[1])
   1.289 -    #print(Cygwin.cygPath('C:'))
   1.290 -    #print('C:\\Program Files\\OpenSecurity: ' + c.cygPath('C:\\Program Files\\OpenSecurity'))
   1.291 -    
   1.292 -    sys.exit(0)
   1.293 -    
   1.294 +#!/bin/env python
   1.295 +# -*- coding: utf-8 -*-
   1.296 +
   1.297 +# ------------------------------------------------------------
   1.298 +# cygwin command
   1.299 +# 
   1.300 +# executes a cygwin command inside the opensecurity project
   1.301 +#
   1.302 +# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
   1.303 +#        Oliver Maurhart, <oliver.maurhart@ait.ac.at>
   1.304 +#
   1.305 +# Copyright (C) 2013 AIT Austrian Institute of Technology
   1.306 +# AIT Austrian Institute of Technology GmbH
   1.307 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
   1.308 +# http://www.ait.ac.at
   1.309 +#
   1.310 +# This program is free software; you can redistribute it and/or
   1.311 +# modify it under the terms of the GNU General Public License
   1.312 +# as published by the Free Software Foundation version 2.
   1.313 +# 
   1.314 +# This program is distributed in the hope that it will be useful,
   1.315 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
   1.316 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   1.317 +# GNU General Public License for more details.
   1.318 +# 
   1.319 +# You should have received a copy of the GNU General Public License
   1.320 +# along with this program; if not, write to the Free Software
   1.321 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
   1.322 +# Boston, MA  02110-1301, USA.
   1.323 +# ------------------------------------------------------------
   1.324 +
   1.325 +
   1.326 +# ------------------------------------------------------------
   1.327 +# imports
   1.328 +
   1.329 +import os
   1.330 +import subprocess
   1.331 +import sys
   1.332 +import _winreg
   1.333 +from subprocess import Popen, PIPE, STARTUPINFO, _subprocess
   1.334 +import threading
   1.335 +
   1.336 +# local
   1.337 +from environment import Environment
   1.338 +from opensecurity_util import logger, setupLogger, OpenSecurityException
   1.339 +import time
   1.340 +
   1.341 +
   1.342 +# ------------------------------------------------------------
   1.343 +# code
   1.344 +
   1.345 +def once(theClass):
   1.346 +    """get the path to our local cygwin installment"""
   1.347 +    home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
   1.348 +    path_hint = [ 
   1.349 +        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin')), 
   1.350 +        os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, 'cygwin64')), 
   1.351 +        os.path.abspath(os.path.join(home_drive, 'cygwin')),
   1.352 +        os.path.abspath(os.path.join(home_drive, 'cygwin64'))
   1.353 +    ]
   1.354 +    path_valid = [ p for p in path_hint if os.path.exists(p) ]
   1.355 +        
   1.356 +    theClass.cygwin_root = path_valid[0]
   1.357 +    theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
   1.358 +    theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
   1.359 +    theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
   1.360 +    theClass.cygwin_scp = os.path.join(theClass.cygwin_bin, 'scp.exe')
   1.361 +    theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
   1.362 +    theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe") 
   1.363 +    """get the path to the VirtualBox installation on this system"""
   1.364 +    theClass.vbox_root = theClass.getRegEntry('SOFTWARE\Oracle\VirtualBox', 'InstallDir')[0]  
   1.365 +    theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
   1.366 +    #theClass.user_home = os.path.expanduser("~")
   1.367 +    theClass.user_home = os.environ['APPDATA']#os.path.expandvars("%APPDATA%")
   1.368 +    return theClass
   1.369 +
   1.370 +            
   1.371 +@once
   1.372 +class Cygwin(object):
   1.373 +    cygwin_root = ''
   1.374 +    cygwin_bin = ''
   1.375 +    cygwin_bash = ''
   1.376 +    cygwin_ssh = ''
   1.377 +    cygwin_x11 = ''
   1.378 +    cygwin_scp = ''
   1.379 +    vbox_root = ''
   1.380 +    vbox_man = ''
   1.381 +    win_cmd = ''
   1.382 +    user_home = ''
   1.383 +    """Some nifty methods working with Cygwin"""
   1.384 +    
   1.385 +    def __call__(self, command, arguments, wait_return=True, window = False):
   1.386 +        """make an instance of this object act as a function"""
   1.387 +        return self.execute(command, arguments, wait_return, window)
   1.388 +
   1.389 +    @staticmethod
   1.390 +    def getRegEntry(key, value):
   1.391 +        try:
   1.392 +            k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
   1.393 +            value = _winreg.QueryValueEx(k, value)
   1.394 +            _winreg.CloseKey(k)
   1.395 +            return value
   1.396 +        except:
   1.397 +            pass
   1.398 +    
   1.399 +            
   1.400 +    @staticmethod
   1.401 +    def root():
   1.402 +        return Cygwin.cygwin_root
   1.403 +
   1.404 +    @staticmethod
   1.405 +    def bin():
   1.406 +        return Cygwin.cygwin_bin
   1.407 +    
   1.408 +    @staticmethod
   1.409 +    def bash():
   1.410 +        return Cygwin.cygwin_bash
   1.411 +    
   1.412 +    @staticmethod    
   1.413 +    def ssh():
   1.414 +        return Cygwin.cygwin_ssh
   1.415 +    
   1.416 +    @staticmethod    
   1.417 +    def scp():
   1.418 +        return Cygwin.cygwin_scp
   1.419 +
   1.420 +    @staticmethod    
   1.421 +    def x11():
   1.422 +        return Cygwin.cygwin_x11
   1.423 +    
   1.424 +    @staticmethod
   1.425 +    def vboxman():
   1.426 +        return Cygwin.vbox_man
   1.427 +    
   1.428 +    @staticmethod
   1.429 +    def cmd():
   1.430 +        return Cygwin.win_cmd
   1.431 +    
   1.432 +    @staticmethod
   1.433 +    def home():
   1.434 +        return Cygwin.user_home
   1.435 +    
   1.436 +    executeLock = threading.Lock()
   1.437 +    #executes command on host system
   1.438 +    @staticmethod
   1.439 +    def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE):
   1.440 +        _startupinfo = STARTUPINFO()
   1.441 +        if not window:
   1.442 +            _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
   1.443 +            _startupinfo.wShowWindow = _subprocess.SW_HIDE
   1.444 +            #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
   1.445 +        
   1.446 +	    result, res_stdout, res_stderr = None, None, None
   1.447 +		
   1.448 +        try:
   1.449 +            # quote the executable otherwise we run into troubles
   1.450 +            # when the path contains spaces and additonal arguments
   1.451 +            # are presented as well.
   1.452 +            # special: invoking bash as login shell here with
   1.453 +            # an unquoted command does not execute /etc/profile
   1.454 +            args = '"' + program + '" ' + arguments
   1.455 +            process = Popen(args, startupinfo = _startupinfo, stdin = stdin, stdout = stdout, stderr = stderr, shell = False)
   1.456 +            logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
   1.457 +            if not wait_return:
   1.458 +                return [0, 'working in background', '']
   1.459 +				
   1.460 +            res_stdout, res_stderr = process.communicate()
   1.461 +            result = process.returncode
   1.462 +			
   1.463 +			#result = process.wait()
   1.464 +            #res_stdout = process.stdout.read();
   1.465 +            #res_stderr = process.stderr.read();
   1.466 +
   1.467 +        except Exception as ex:
   1.468 +            res_stderr = ''.join(str(ex.args))
   1.469 +            result = 1 
   1.470 +            
   1.471 +        return result, res_stdout, res_stderr
   1.472 +    
   1.473 +    @staticmethod
   1.474 +    def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
   1.475 +        retry = 0
   1.476 +        result = None
   1.477 +        while retry < 3:
   1.478 +            if Cygwin.executeLock.acquire(True):
   1.479 +                result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
   1.480 +                Cygwin.executeLock.release()
   1.481 +                if result[0] == 0:
   1.482 +                    return result
   1.483 +                retry+=1
   1.484 +        return result
   1.485 +
   1.486 +
   1.487 +    @staticmethod
   1.488 +    def bashExecute(command, wait_return=True, window = False, bash_opts='', stdin = PIPE, stdout = PIPE, stderr = PIPE):
   1.489 +        # for some reason, the '-l' is ignored when started via python
   1.490 +        # so the same behavior is triggered by calling /etc/profile 
   1.491 +        # directly
   1.492 +        command = bash_opts + ' -l -c "'  + command + '"'
   1.493 +        return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window, stdin = stdin, stdout = stdout, stderr = stderr)
   1.494 +    
   1.495 +    @staticmethod
   1.496 +    def cmdExecute(command, wait_return=True, window = False):
   1.497 +        command = ' /c ' + command 
   1.498 +        return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
   1.499 +
   1.500 +    # executes command over ssh on guest vm
   1.501 +    @staticmethod
   1.502 +    def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
   1.503 +        command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
   1.504 +        return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
   1.505 +	
   1.506 +	# executes command over ssh on guest vm
   1.507 +    @staticmethod
   1.508 +    def sshBackgroundExecute(command, address, user_name, certificate, wait_return=True, window = False):
   1.509 +        command = ' -f -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
   1.510 +        return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
   1.511 +    
   1.512 +    #machineFolder + '/' + vm_name + '/dvm_key
   1.513 +    #address = self.getHostOnlyIP(vm_name)
   1.514 +    #machineFolder = self.getDefaultMachineFolder()
   1.515 +    #machineFolder = Cygwin.cygwinPath(machineFolder)
   1.516 +    
   1.517 +    # executes command over ssh on guest vm with X forwarding
   1.518 +    @staticmethod
   1.519 +    def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
   1.520 +        return Cygwin.bashExecute('DISPLAY=:0.0 ssh -Y -o StrictHostKeyChecking=no -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '')
   1.521 +
   1.522 +    @staticmethod
   1.523 +    def is_X11_running():
   1.524 +        """check if we can connect to a X11 running instance"""
   1.525 +        p = Cygwin.bashExecute('xset -display :0 q', wait_return = True, window = False) 
   1.526 +        return p[0] == 0
   1.527 +        
   1.528 +    @staticmethod
   1.529 +    def start_X11():
   1.530 +        """start X11 in the background (if not already running) on DISPLAY=:0
   1.531 +        
   1.532 +        If there is already a X11 running then exit silently, calling this
   1.533 +        method as often as needed.
   1.534 +        """
   1.535 +        Popen('"' + Cygwin.cygwin_x11 + '" :0 -multiwindow -resize -silent-dup-error')
   1.536 +        return (0, None, None)
   1.537 +    
   1.538 +    @staticmethod    
   1.539 +    def cygPath(path):
   1.540 +        cmd = 'cygpath -u \'' + path + '\''
   1.541 +        return Cygwin.bashExecute(cmd)[1].rstrip('\n')
   1.542 +    
   1.543 +    @staticmethod
   1.544 +    def checkResult(result):
   1.545 +        if result[0] != 0:
   1.546 +            logger.error('Command failed:' + ''.join(result[2]))
   1.547 +            raise OpenSecurityException('Command failed:' + ''.join(result[2]))
   1.548 +        return result
   1.549 +                
   1.550 +# start
   1.551 +import os
   1.552 +import win32api
   1.553 +import win32con
   1.554 +import win32security
   1.555 +
   1.556 +if __name__ == "__main__":
   1.557 +    logger = setupLogger('Cygwin')
   1.558 +    c = Cygwin()
   1.559 +    #logger.info(c.root())
   1.560 +    #logger.info(c.bin())
   1.561 +    #logger.info(c.bash())
   1.562 +    #logger.info(c.ssh())
   1.563 +    #logger.info(c.x11())
   1.564 +    #logger.info(c.home())   
   1.565 +    
   1.566 +    #PSEXEC -i -s -d CMD
   1.567 +    #tasklist /v /fo list /fi "IMAGENAME eq explorer.exe"
   1.568 +    
   1.569 +    #runner = XRunner()
   1.570 +    #runner.start()
   1.571 +    
   1.572 +    #Cygwin.start_X11()
   1.573 +    
   1.574 +    
   1.575 +            
   1.576 +    #time.sleep(500)
   1.577 +    
   1.578 +    #Cygwin.start_X11()
   1.579 +    #print (Cygwin.is_X11_running())
   1.580 +    #print (Cygwin.is_X11_running())
   1.581 +    #new_sdvm = 'SecurityDVM0'
   1.582 +    #new_ip = Cygwin.vboxExecute('guestproperty get ' + new_sdvm + ' /VirtualBox/GuestInfo/Net/0/V4/IP')[1]
   1.583 +    #new_ip = new_ip[new_ip.index(':')+1:].strip()
   1.584 +    #new_ip = '+'
   1.585 +    #result = Cygwin.bashExecute('DISPLAY=:0.0 xhost '+new_ip)
   1.586 +    #browser = '/usr/bin/midori '
   1.587 +    #print(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', '/cygdrive/c/Users/BarthaM/VirtualBox VMs' + '/' + new_sdvm + '/dvm_key'))
   1.588 +            
   1.589 +    #print(Cygwin.bashExecute('echo $PATH')[1])
   1.590 +    #print(Cygwin.cygPath('C:'))
   1.591 +    #print('C:\\Program Files\\OpenSecurity: ' + c.cygPath('C:\\Program Files\\OpenSecurity'))
   1.592 +    
   1.593 +    sys.exit(0)
   1.594 +