簡體   English   中英

使用python生成shell腳本

[英]Generating shell scripts using python

嗨,我目前正在開發一個 python 腳本,該腳本生成 shell 腳本以在 linux 服務器上安裝代理。 python 腳本輸出的 .sh 文件一直以“語法錯誤:文件意外結束”結尾,但是當我在 vi 中手動輸入確切的輸出時,似乎沒有問題。 我在 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

Windows 上的換行約定\\r\\n Bash 將換行符解釋為...換行符,而\\rbash的普通字符。

使固定:

sf = open(agentname+ "_install.sh", "w", newline='\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM