Search code examples
pythonlinuxshsilent-installer

Generating shell scripts using python


Hi I'm currently working on a python script that generates shell scripts to install agents on a linux server. The .sh files that the python scripts output keeps ending up with a "syntax error: unexpected end of file" but when i manually type in the exact output in vi, there seems to be no issue. Is there any issue with how I'm writing it in python or is it feasible to do it through python?

python script

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

output from above script

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 file it reads from

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

Solution

  • The newline convention is \r\n on windows. Bash interprets newline character as... newline character, and the \r is a normal character for bash.

    Fix:

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