Advertisement
sergio_educacionit

apache_tools

Jun 1st, 2023 (edited)
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. # Importar libreria para capturar argumentos pasados al script.
  4.  
  5. import sys
  6.  
  7.  
  8. # Diccionario con mensajes de error
  9.  
  10. mensajes_error = {
  11.  
  12.     "err_no_command":"Debe ingresar un comando.",
  13.     "err_invalid_command":"Debe ingresar un comando valido.",
  14.     "err_no_servername":"Debe ingresar un nombre para el vhost."
  15.  
  16.  
  17. }
  18.  
  19.  
  20.  
  21. # Lista con los comandos que utiliza este programa
  22.  
  23.  
  24. comandos = [ 'newhost', 'modhost', 'delhost' ]
  25.  
  26. argumentos = sys.argv
  27.  
  28. # Validar que haya al menos un argumento pasado al script
  29.  
  30. if len(argumentos) < 2:
  31.  
  32.     print(mensajes_error["err_no_command"])
  33.     exit(1)
  34.  
  35.  
  36. if argumentos[1] == comandos[0]:
  37.    
  38.     if len(argumentos) < 3:
  39.  
  40.         print(mensajes_error["err_no_servername"])
  41.         exit(1)
  42.     # newhost
  43.  
  44.     #<VirtualHost *:80>
  45.  
  46.     #   DocumentRoot /ruta/al/document/root
  47.     #   ServerName <servername>
  48.  
  49.     #</VirtualHost> 
  50.  
  51.     # Paramentros predeterminados
  52.  
  53.     puerto = 80
  54.  
  55.     server_name = argumentos[-1]
  56.  
  57.     document_root = f"/var/www/{server_name}"
  58.  
  59.  
  60.     # Procesar parametros
  61.  
  62.     contador = 0
  63.  
  64.     for arg in argumentos:
  65.  
  66.         if arg == "-p":
  67.  
  68.             puerto = argumentos[contador + 1]
  69.  
  70.  
  71.         elif arg == "-d":
  72.  
  73.             document_root = argumentos[contador + 1]
  74.  
  75.         contador += 1
  76.  
  77.  
  78.     output = f"<VirtualHost *:{puerto}>\n\n\tDocumentRoot {document_root}\n\tServerName {server_name}\n\n</VirtualHost>"
  79.    
  80.     print(output)
  81.  
  82.  
  83.  
  84.  
  85. elif argumentos[1] == comandos[1]:
  86.  
  87.     print(argumentos[1])
  88.  
  89. elif argumentos[1] == comandos[2]:
  90.  
  91.     print(argumentos[1])
  92.  
  93. else:
  94.  
  95.     print(mensajes_error["err_invalid_command"])
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement