使用python生成shell脚本
您好,我目前正在编写一个python脚本,该脚本生成shell脚本以在linux服务器上安装代理。python脚本输出的.sh文件总是以“语法错误:意外的文件结尾”结束,但当我在vi中手动键入确切的输出时,似乎没有问题。我是如何用python编写的,或者通过python编写是否可行 python脚本使用python生成shell脚本,python,linux,sh,silent-installer,Python,Linux,Sh,Silent Installer,您好,我目前正在编写一个python脚本,该脚本生成shell脚本以在linux服务器上安装代理。python脚本输出的.sh文件总是以“语法错误:意外的文件结尾”结束,但当我在vi中手动键入确切的输出时,似乎没有问题。我是如何用python编写的,或者通过python编写是否可行 python脚本 import csv def menu(): print("type of scripts") print("1. Install + Generation") print
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将换行符解释为。。。换行符,\r
是bash
的普通字符
修正:
当比较输出和手动键入的shell脚本时,
diff
会说什么?脚本很好,应该可以工作。输出什么语法错误:意外的文件结尾
?这是确切的错误消息吗?它前面有一个bash:
?尝试添加#/bin/sh
作为脚本的第一行。@Błotosmętek它说文件末尾没有换行符。@KamilCuk./agent1_install.sh:第18行:语法错误:意外的文件结尾这是我尝试运行生成的脚本时遇到的错误。当我将脚本复制粘贴到在线脚本检查中时,我也遇到了同样的错误。我注意到,与手动键入的脚本相比,在vi中比较这两个脚本时,生成的脚本底部有一个[noeol][dos]。有没有可能用python做点什么呢?啊,我明白了,非常感谢你的帮助!在不同的平台上工作时,我需要注意的是:x
sf = open(agentname+ "_install.sh", "w", newline='\n')