I'm currently studying penetration testing and Python programming. I just want to know how I would go about executing a Linux command in Python. The commands I want to execute are:echo 1 > /proc/sys/net/ipv4/ip_forwardiptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080If I just use print in Python and run it in the terminal will it do the same as executing it as if you was typing it yourself and pressing Enter?
Execute shell commands in Python
python
You can use os.system(), like this:import osos.system('ls')Or in your case:os.system('echo 1 > /proc/sys/net/ipv4/ip_forward')os.system('iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080')Better yet, you can use subprocess's call, it is safer:from subprocess import callcall('ls')https://docs.python.org/2/library/subprocess.html