sh 是一个非常强大的 Python 库,用来 像写 Shell 一样在 Python 中执行命令。 它让运维自动化脚本既保留 Shell 的直观,又能享受 Python 的逻辑控制。
sh 仅支持类 Unix 系统(Linux/macOS),不支持 Windows。
for line in sh.tail("-f", "/var/log/syslog", _iter=True):
print(line.strip())
def handler(line):
print(f"[OUT]: {line.strip()}")
sh.ping("-c", "4", "baidu.com", _out=handler)
result = sh.grep(sh.ls("-l"), "py")
print(result)