Конвертировать скрипт установки Bash в Python - PullRequest
0 голосов
/ 06 января 2019

У меня есть следующий скрипт bash, приведенный ниже, и я хотел бы преобразовать его в Python и, в конечном итоге, добавить обработку ошибок.

Я пытался создавать массивы и читать их как в bash, но я не смог найти легкий путь в Python. Есть идеи, пожалуйста?

#!/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"

Благодаря огромной помощи пользователей ниже:

Мой обновленный код на Python ниже. Надеюсь, это кому-нибудь поможет

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))))

1 Ответ

0 голосов
/ 06 января 2019

Сначала мы конвертируем repos в список питонов. Итак:

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 в python. В цикле for мы запускаем git clone package. Вместо использования библиотеки мы можем просто запустить ее через os.system().

Следовательно, код цикла for:

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

Наконец, мы получаем количество репо в списке и распечатываем его, что мы делаем с print ("There are {} repos.".format(str(len(repos))))

Полный код:

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))))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...