简体   繁体   English

将 Bash 安装脚本转换为 Python

[英]Convert Bash Install script to Python

I have the following bash script below and would like to convert it over to Python and eventually add error handling.我在下面有以下 bash 脚本,并希望将其转换为 Python 并最终添加错误处理。

I tried to do arrays and read them in like in bash but i could not find an easy way in Python.我试图做数组并像在 bash 中一样读取它们,但我在 Python 中找不到简单的方法。 Any ideas please?请问有什么想法吗?

#!/bin/bash
repos=("BloodHoundAD/BloodHound.git" "GhostPack/Seatbelt.git" "GhostPack/SharpUp.git" "yeyintminthuhtut/Awesome-Red-Teaming.git"
"byt3bl33d3r/DeathStar.git" "byt3bl33d3r/CrackMapExec.git" "Cn33liz/p0wnedShell.git" "EmpireProject/Empire.git"
"danielmiessler/SecLists.git" "laramies/theHarvester.git")
for i in "${repos[@]}"; do
  git clone http://github.com/$i
done
echo "There are ${#repos[@]} repos here"

Thanks to the great help by the users below:感谢以下用户的大力帮助:

My updated code in Python is below.我在 Python 中的更新代码如下。 Hope it helps someone希望它可以帮助某人

import os
import subprocess

repos=["BloodHoundAD/BloodHound.git", "GhostPack/Seatbelt.git", "GhostPack/SharpUp.git", "yeyintminthuhtut/Awesome-Red-Teaming.git",
"byt3bl33d3r/DeathStar.git", "byt3bl33d3r/CrackMapExec.git", "Cn33liz/p0wnedShell.git", "EmpireProject/Empire.git",
"danielmiessler/SecLists.git", "laramies/theHarvester.git"]

for repo in repos:
    subprocess.Popen("git clone https://github.com/{}".format(repo) , shell=True).wait()

print ("There are {} repos in the array.".format(str(len(repos))))

First, we covert repos into a python list.首先,我们将repos转换为 python 列表。 So:所以:

repos=["BloodHoundAD/BloodHound.git", "GhostPack/Seatbelt.git", "GhostPack/SharpUp.git", "yeyintminthuhtut/Awesome-Red-Teaming.git",
"byt3bl33d3r/DeathStar.git", "byt3bl33d3r/CrackMapExec.git", "Cn33liz/p0wnedShell.git", "EmpireProject/Empire.git",
"danielmiessler/SecLists.git", "laramies/theHarvester.git"]

Then, we create a for loop in python.然后,我们在python中创建一个for循环。 In that for loop, we run git clone package .在那个 for 循环中,我们运行git clone package Instead of using a library, we can just run it through os.system() .我们可以通过os.system()运行它,而不是使用库。

Therefore, the for loop code is:因此,for循环代码为:

for repo in repos:
    os.system("git clone http://github.com/{}".format(repo))

Finally, we get the amount of repos in the list and print it out, which we do with print ("There are {} repos.".format(str(len(repos))))最后,我们获取列表中的 repos 数量并将其打印出来,我们使用print ("There are {} repos.".format(str(len(repos))))

The full code is:完整代码是:

import os

repos=["BloodHoundAD/BloodHound.git", "GhostPack/Seatbelt.git", "GhostPack/SharpUp.git", "yeyintminthuhtut/Awesome-Red-Teaming.git",
"byt3bl33d3r/DeathStar.git", "byt3bl33d3r/CrackMapExec.git", "Cn33liz/p0wnedShell.git", "EmpireProject/Empire.git",
"danielmiessler/SecLists.git", "laramies/theHarvester.git"]

for repo in repos:
    os.system("git clone http://github.com/{}".format(repo))


print ("There are {} repos.".format(str(len(repos))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM