Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run OS shell commands in IronPython/Mono?

I would like to give IronPython and Mono a try. Specifically doing sysadmin tasks. Which often means running OS commands. In CPython I use the subprocess module for such tasks. But in IronPython (v2.0.1, Mono 2.4, Linux) there is no subprocess module. It seems there is not even an 'os' module. So I can't use os.system(). What would be the IronPython way of doing tasks you would normally use 'subprocess' or 'os.system()' for in CPython?

like image 467
Cyberdrow Avatar asked May 01 '09 07:05

Cyberdrow


People also ask

How do I run a shell command in Python?

If you need to execute a shell command with Python, there are two ways. You can either use the subprocess module or the RunShellCommand() function. The first option is easier to run one line of code and then exit, but it isn't as flexible when using arguments or producing text output.

How do I run a shell command in Python using subprocess?

Python Subprocess Run Function The subprocess. run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.

How do I run a Python command using subprocess in Linux?

The subprocess library has a class called Popen() that allows us to execute shell commands and get the output of the command. Create a Python file and add the following code. We also need to create a file called “servers. txt”, where we can add a list of all the servers we need to ping.

What shell does Python OS system use?

1 Answer. By default it will run in the Bourne shell (that would be /bin/sh ).


1 Answers

I have found an answer. Thanks to the "IronPython Cookbook". One can find more information on this subject there: http://www.ironpython.info/index.php/Launching_Sub-Processes

>>> from System.Diagnostics import Process
>>> p = Process()
>>> p.StartInfo.UseShellExecute = False
>>> p.StartInfo.RedirectStandardOutput = True
>>> p.StartInfo.FileName = 'uname'
>>> p.StartInfo.Arguments = '-m -r'
>>> p.Start()
True
>>> p.WaitForExit()
>>> p.StandardOutput.ReadToEnd()
'9.6.0 i386\n'
>>> p.ExitCode
0
>>> 
like image 150
Cyberdrow Avatar answered Nov 03 '22 13:11

Cyberdrow