Command and Rule Over Your Shell
Sultan¶
- Installing Sultan
- Frequently Asked Questions
- Sultan Examples
- WARNING * WARNING * WARNING
- Example 1: Getting Started
- Example 2: Sultan with Context Management
- Example 3: Compounding with And (&&) and Or (||)
- Example 4: Redirecting with Pipes (|)
- Example 5: Redirecting Output to File
- Example 6: Read from Standard Input
- Example 7: Running as Another User
- Example 8: Running as Root
- Example 9: Disable Logging
- Example 10: Commands with Hyphones (i.e.: apt-get)
- Example 11: Sourcing a File before Running a Command
- Example 12: Results from a Command
- Example 13: Streaming Results from a Command
- Example 14: Custom Executable
- Sultan SSH Examples
What is Sultan?¶
Sultan is a Python package for interfacing with command-line utilities, like yum, apt-get, or ls, in a Pythonic manner. It lets you run command-line utilities using simple function calls.
The simplest way to use Sultan is to just call it:
from sultan.api import Sultan
s = Sultan()
s.sudo("yum install -y tree").run()
Runs:
sudo yum install -y tree;
The recommended way of using Sultan is to use it in Context Management mode. Here is how to use Sultan with Context Management:
from sultan.api import Sultan
with Sultan.load(sudo=True) as s:
s.yum("install -y tree").run()
Runs:
sudo su - root -c 'yum install -y tree;'
What if we want to install this command on a remote machine? You can easily achieve this using context management:
from sultan.api import Sultan
with Sultan.load(sudo=True, hostname="myserver.com") as sultan:
sultan.yum("install -y tree").run()
Runs:
ssh root@myserver.com 'sudo su - root -c 'yum install -y tree;''
If you enter a wrong command, Sultan will print out details you need to debug and find the problem quickly.
Here, the same command was run on a Mac:
from sultan.api import Sultan
with Sultan.load(sudo=True, hostname="myserver.com") as sultan:
sultan.yum("install -y tree").run()
Yields:
[sultan]: sudo su - root -c 'yum install -y tree;'
Password:
[sultan]: --{ STDERR }-------------------------------------------------------------------------------------------------------
[sultan]: | -sh: yum: command not found
[sultan]: -------------------------------------------------------------------------------------------------------------------
Want to get started? Simply install Sultan, and start writing your clean code:
pip install --upgrade sultan
If you have more questions, check the rest of the docs, or reach out at Github: https://github.com/aeroxis/sultan
WARNING * WARNING * WARNING¶
When you’re using Sultan, you are running commands directly on your local shell, so please, do not run untested and untrusted code. You are taking the risk if you are running untrusted code.
Sultan runs POpen with shell=True, and according to Python documentation, this can be a security hazard if combined with untrusted input. More information can be found here: