Execute shell commands in Python

iZodiac

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_forward
iptables -t nat -A PREROUTING -p tcp --destination-port 80 -j REDIRECT --to-port 8080

If 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?

Kira

You can use os.system(), like this:

import os
os.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, more powerful and likely faster:

from subprocess import call
call('echo "I like potatos"', shell=True)

Or, without invoking shell:

call(['echo', 'I like potatos'])

If you want to capture the output, one way of doing it is like this:

import subprocess
cmd = ['echo', 'I like potatos']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

o, e = proc.communicate()

print('Output: ' + o.decode('ascii'))
print('Error: '  + e.decode('ascii'))
print('code: ' + str(proc.returncode))

I highly recommend setting a timeout in communicate, and also to capture the exceptions you can get when calling it. This is a very error-prone code, so you should expect errors to happen and handle them accordingly.

https://docs.python.org/3/library/subprocess.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to open adb shell and execute commands inside shell using python

From Dev

How to open adb shell and execute commands inside shell using python

From Dev

Can I use a shell or python script to execute commands in a chroot?

From Dev

Python Execute Shell commands without having a terminal open

From Dev

Jenkins - Execute shell commands with wildcards

From Dev

execute commands as parameters in a login shell

From Dev

Jenkins - can the "Execute Shell" execute SSH commands

From Dev

Shell commands with a pipe in Python

From Dev

Cannot execute shell commands in bash script

From Java

How to execute mongo commands through shell scripts?

From Dev

Clojure: how to execute shell commands with piping?

From Dev

golang: Execute shell commands on remote server

From Dev

How to execute the vim commands through shell script

From Dev

Execute shell commands and get output in a TextView

From Dev

Mount FTP so it will be possible to execute shell commands

From Dev

execute multiple commands on console through unix shell

From Dev

C++ execute many commands in shell

From Dev

How to execute shell commands as root in Java

From Dev

Mount FTP so it will be possible to execute shell commands

From Dev

Execute shell commands in same process of your app

From Dev

Rails + Passenger: Can't execute shell commands

From Dev

Make a shell script execute commands in telnet or programs

From Dev

How to execute commands in gnuplot using shell script?

From Dev

Start xterm with different shell and execute commands

From Dev

how to execute compound commands in shell script?

From Dev

C++ execute multiple commands with timer in shell

From Dev

How to execute library calls commands from shell?

From Dev

Python script to execute external commands

From Dev

Execute Bash commands Python way

Related Related

  1. 1

    How to open adb shell and execute commands inside shell using python

  2. 2

    How to open adb shell and execute commands inside shell using python

  3. 3

    Can I use a shell or python script to execute commands in a chroot?

  4. 4

    Python Execute Shell commands without having a terminal open

  5. 5

    Jenkins - Execute shell commands with wildcards

  6. 6

    execute commands as parameters in a login shell

  7. 7

    Jenkins - can the "Execute Shell" execute SSH commands

  8. 8

    Shell commands with a pipe in Python

  9. 9

    Cannot execute shell commands in bash script

  10. 10

    How to execute mongo commands through shell scripts?

  11. 11

    Clojure: how to execute shell commands with piping?

  12. 12

    golang: Execute shell commands on remote server

  13. 13

    How to execute the vim commands through shell script

  14. 14

    Execute shell commands and get output in a TextView

  15. 15

    Mount FTP so it will be possible to execute shell commands

  16. 16

    execute multiple commands on console through unix shell

  17. 17

    C++ execute many commands in shell

  18. 18

    How to execute shell commands as root in Java

  19. 19

    Mount FTP so it will be possible to execute shell commands

  20. 20

    Execute shell commands in same process of your app

  21. 21

    Rails + Passenger: Can't execute shell commands

  22. 22

    Make a shell script execute commands in telnet or programs

  23. 23

    How to execute commands in gnuplot using shell script?

  24. 24

    Start xterm with different shell and execute commands

  25. 25

    how to execute compound commands in shell script?

  26. 26

    C++ execute multiple commands with timer in shell

  27. 27

    How to execute library calls commands from shell?

  28. 28

    Python script to execute external commands

  29. 29

    Execute Bash commands Python way

HotTag

Archive