10 examples of 'python run shell command get output' in Python

Every line of 'python run shell command get output' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
24def run_shell(command):
25 logging.debug("Running "+command)
26 subprocess.check_call(command, shell=True)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

33def run_shell_cmd(cmd, wait=False):
34 p = subprocess.Popen(
35 "su",
36 executable=ANDROID_SHELL,
37 shell=True,
38 stdin=subprocess.PIPE,
39 stdout=subprocess.PIPE)
40 res, err = p.communicate(cmd + '\n')
41
42 if wait:
43 p.wait()
44 return res
45 else:
46 return res
23def shell(cmd, env=None):
24 """Dummy wrapper for running shell commands, checking the return value and
25 logging"""
26
27 if env is not None:
28 subp_env = env
29 else:
30 subp_env = os.environ
31
32 if type(cmd) is str:
33 _logger.warning(cmd)
34 else:
35 _logger.warning(' '.join(cmd))
36
37 for line in subp.check_output(cmd, shell=True, env=subp_env).split("\n"):
38 if re.match(r"^\s*$", line):
39 continue
40 _logger.warning(line)
11def _run_shell(env, args):
12 return env.run_shell()
5def run(cmd, shell=True, check=True, stdout=PIPE, stderr=PIPE):
6 return _run(cmd, shell=shell, check=check, stdout=stdout,
7 stderr=stderr).stdout.decode()
111def shell_execute(shell_cmd):
112 subprocess.run(shell_cmd, shell=True)
56def shellcmd(cmd, verbose=True):
57 """Call a shell command."""
58 if verbose:
59 print(cmd)
60 try:
61 subprocess.check_call(cmd, shell=True)
62 except subprocess.CalledProcessError, err:
63 msg = """
64 Error while executing a shell command.
65 %s
66 """ % str(err)
67 raise Exception(msg)
68def run_command(self, cmd):
69 orig = sys.stdout
70 try:
71 sys.stdout = six.StringIO()
72 if isinstance(cmd, list):
73 self.shell.main(cmd)
74 else:
75 self.shell.main(cmd.split())
76 except SystemExit:
77 exc_type, exc_value, exc_traceback = sys.exc_info()
78 self.assertEqual(exc_value.code, 0)
79 finally:
80 out = sys.stdout.getvalue()
81 sys.stdout.close()
82 sys.stdout = orig
83 return out
34def RunCommand(command, verbose=False, shell=False):
35 """Runs the command list, print the output, and propagate its result."""
36 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
37 stderr=subprocess.STDOUT, shell=shell)
38 if not shell:
39 output = proc.communicate()[0]
40 result = proc.returncode
41 if verbose:
42 print(output.decode("utf-8").strip())
43 if result != 0:
44 print ('Command "%s" exited with non-zero exit code %d'
45 % (' '.join(command), result))
46 sys.exit(result)
47 return output.decode("utf-8")
48 else:
49 return None
32def shell(command):
33 cmd = shell_interactive
34 if command:
35 cmd += ' -c "' + command + '"'
36 try:
37 p = pexpect.spawn(str(cmd))
38 except Exception:
39 return
40 while True:
41 c = console.get_char()
42 if c == '\b': # BACKSPACE
43 p.send('\x7f')
44 elif c != '':
45 p.send(c)
46 while True:
47 try:
48 c = p.read_nonblocking(1, timeout=0)
49 except:
50 c = ''
51 if c == '' or c == '\n':
52 break
53 elif c == '\r':
54 console.write_line()
55 elif c == '\b':
56 if console.col != 1:
57 console.col -= 1
58 else:
59 console.write(c)
60 if c == '' and not p.isalive():
61 return

Related snippets