Привет! В настоящее время я работаю над сценарием python, который генерирует сценарии оболочки для установки агентов на сервере linux. Файлы. sh, которые выводятся сценариями python, заканчиваются "синтаксической ошибкой: неожиданный конец файла", но когда я вручную набираю точный вывод в vi, кажется, нет никаких проблем. Есть ли проблема с тем, как я пишу это в python, или это возможно сделать через python?
python скрипт
import csv
def menu():
print("type of scripts")
print("1. Install + Generation")
print("2. Unregister + Reregister")
print("3. Unregister + Uninstall")
#Converts numeral choice into type for script naming
def choicename(choice):
choice = int(choice)
if choice==1:
return "install"
elif choice == 2 :
return "rereg"
else:
return "uninstall"
#Generates the install agent scripts
def installScript(agentname,agentfile,mgrfile,prigw,secgw,ostype):
#Generates the script for Linux agents (.sh)
if ostype=="Linux":
agentpath = 'agent="/opt/test/ragent/bin/racli"'
installerpath = '\ninstaller="/opt/test/installer/bin/racli"'
checkAgent = '\nif [ ! -e "$agent" ]; then' +"\n" + "./" + agentfile + " -n -d /opt/test" + '\nelse\necho "Agent is already installed"\nfi'
checkInstaller = '\nif [ ! -e "$installer" ]; then' + "\n" +"./" + mgrfile + " -n -d /opt/test"+ '\nelse\necho "Manager is already installed"\nfi'
regAgent = "\n/opt/test/ragent/bin/cli registration advanced-register registration-type=Primary ragent-name="+ agentname+ " gw-ip="+ prigw+ " gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1"
if secgw!="":
regAgent+="\n/opt/test/ragent/bin/cli registration advanced-register registration-type=Secondary ragent-name="+ agentname+ " gw-ip="+ secgw+ " gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1"
startAgent="\n/opt/test/ragent/bin/rainit start"
regInstaller="\n/opt/test/installer/bin/cliinstaller registration register-use-existing package-folder-path=\".\" package-folder-size=1024"
startInstaller="\n/opt/test/installer/bin/rainstallerinit start"
sf = open(agentname+ "_install.sh","w")
sf.write(agentpath+installerpath+checkAgent+checkInstaller+regAgent+startAgent+regInstaller+startInstaller)
sf.close()
def scriptSplit(option,agentname,agentfile,mgrfile,prigw,secgw,ostype):
if option=="install":
installScript(agentname,agentfile,mgrfile,prigw,secgw,ostype)
elif option =="rereg":
reregScript(agentname,agentfile,mgrfile,prigw,secgw,ostype)
elif option =="uninstall":
uninstallScript()
#Collects user input for function type
def main():
menu()
choice = input("Please choose the type of script you would like to generate: ")
option = choicename(choice)
filename = input("Please enter the name of the csv file: ")
with open(filename) as csv_file:
creader = csv.reader(csv_file, delimiter=",")
line_count = 0
for row in creader:
if line_count!=0:
agentname=row[0]
agentfile=row[1]
mgrfile=row[2]
prigw=row[3]
secgw=row[4]
installtype=row[5]
scriptSplit(option,agentname,agentfile,mgrfile,prigw,secgw,installtype)
line_count += 1
#### END OF FUNCTIONS ###
main()
вывод из вышеупомянутого скрипта
agent="/opt/test/ragent/bin/racli"
installer="/opt/test/installer/bin/racli"
if [ ! -e "$agent" ]; then
./agent1.bsx -n -d /opt/test
else
echo "Agent is already installed"
fi
if [ ! -e "$installer" ]; then
./installer1.bsx -n -d /opt/test
else
echo "Manager is already installed"
fi
/opt/test/ragent/bin/cli registration advanced-register registration-type=Primary ragent-name=agent1 gw-ip=10.255.0.80 gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1
/opt/test/ragent/bin/cli registration advanced-register registration-type=Secondary ragent-name=agent1 gw-ip=10.255.0.81 gw-port=443 manual-settings-activation=Automatic monitor-networkchannels=Both password=$1
/opt/test/ragent/bin/rainit start
/opt/test/installer/bin/cliinstaller registration register-use-existing package-folder-path="." package-folder-size=1024
/opt/test/installer/bin/rainstallerinit start
CSV-файл, который он читает с
Agent Name,Agent File,Installer File,Primary IP,Secondary IP,Type
agent1,agent1.bsx,installer1.bsx,10.255.0.80,10.255.0.81,Linux
agent2,agent2.bsx,installer2.bsx,10.255.0.81,,Linux