Python - Run Linux Bash Commands

by matt392 in Circuits > Software

97 Views, 1 Favorites, 0 Comments

Python - Run Linux Bash Commands

commandline.jpeg
# Execute Linux bash commands and write to text files
# Created using Thonny IDE
# Run from the command line or Thonny IDE

import subprocess

# tee command sends output to screen and writes to text file

subprocess.Popen('ip -c a | tee internetinfo.text', shell=True)

# ping to Google's public DNS server
subprocess.Popen('ping 8.8.8.8 -c 3 | tee pingdns.text', shell=True)

subprocess.Popen('df -m | tee diskfree.text', shell=True)

subprocess.Popen('uname -a | tee uname.text', shell=True)

subprocess.Popen('hostname | tee hostname.text', shell=True)

subprocess.Popen('lshw | tee lshw.text', shell=True)

subprocess.Popen('lscpu | tee lscpu.text', shell=True)