{"id":507,"date":"2018-02-25T21:06:45","date_gmt":"2018-02-25T10:06:45","guid":{"rendered":"http:\/\/www.scriptinglibrary.com\/?p=507"},"modified":"2020-05-18T16:15:31","modified_gmt":"2020-05-18T06:15:31","slug":"merge-all-apache-httpd-conf-with-python","status":"publish","type":"post","link":"https:\/\/www.scriptinglibrary.com\/languages\/python\/merge-all-apache-httpd-conf-with-python\/","title":{"rendered":"Merge All Apache Httpd.Conf with Python"},"content":{"rendered":"<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Apache_HTTP_Server\">Apache<\/a> is certainly the most popular web server around, it&#8217;s also open-source, multi-platform and serves more than 46% of the websites in the world.<\/p>\n<p>Its success goes back to the mid-90s when Apache was created and the internet started to be used more widely and is evolved to the 2.4 version that we use today. Apache has a lot of features that make the tools valuable and useful.<\/p>\n<p><!--more--><\/p>\n<p>The correct configuration of Apache is always required but can be not so straightforward when itis divided into conf files and include statements.<\/p>\n<p>This tool is inspired and refactored from another<a href=\"http:\/\/benincampus.blogspot.com.au\/2015\/10\/show-complete-apach\"> developer that had the same idea<\/a> <b>Merging\/Combine all httpd.conf files into one, <\/b>to have the visibility of the configuration at run-time.<\/p>\n<p>This is the end result, that you can also find on our GitHub repository:<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/python\r\n\"\"\" This script combines your APACHE httpd.conf with all included files\\\r\n   into one and redirects it to the standard output. \"\"\"\r\n__author__ = 'ben', 'PAOLO FRIGO, paolofrigo@gmail.com | www.scriptinglibrary.com'\r\n\r\nimport os\r\nimport os.path\r\nimport logging\r\nimport fnmatch\r\nimport re\r\nimport argparse\r\n\r\n\r\ndef ProcessMultipleFiles(inputfiles):\r\n    \"\"\" Process an expression with \/* with all the files included on that directory \"\"\"\r\n    if inputfiles.endswith('\/'):\r\n        inputfiles = inputfiles + \"*\"\r\n    content = ''\r\n    localfolder = os.path.dirname(inputfiles)\r\n    basenamepattern = os.path.basename(inputfiles)\r\n    for root, dirs, files in os.walk(localfolder):\r\n        for filename in fnmatch.filter(files, basenamepattern):\r\n            content += ProcessInput(os.path.join(root, filename))\r\n    return content\r\n\r\n\r\ndef RemoveExcessiveLinebreak(lineofcontent):\r\n    \"\"\" Remove Excessive Linebreaks form the line of content passed as argument \"\"\"\r\n    length = len(lineofcontent)\r\n    lineofcontent = lineofcontent.replace(\r\n        os.linesep + os.linesep + os.linesep, os.linesep + os.linesep)\r\n    newlength = len(lineofcontent)\r\n    if newlength &lt; length:\r\n        lineofcontent = RemoveExcessiveLinebreak(lineofcontent)\r\n    return lineofcontent\r\n\r\n\r\ndef ProcessInput(inputfile):\r\n    \"\"\" This function accepts an input path for the httpd conf file, searchs\r\n        for 'include' and replaces it with the matchin content of the included\r\n        file, add starts and end comments and removes all other comments or\r\n        spaces. \"\"\"\r\n    content = ''\r\n    if logging.root.isEnabledFor(logging.DEBUG):\r\n        content = '# Start of ' + inputfile + os.linesep\r\n    with open(inputfile, 'r') as infile:\r\n        for line in infile:\r\n            stripline = line.strip(' \\t')\r\n            if stripline.startswith('#'):\r\n                continue\r\n            # search for serverroot\r\n            searchroot = re.search(r'serverroot\\s+(\\S+)', stripline, re.I)\r\n            if searchroot:\r\n                serverroot = searchroot.group(1).strip('\"')\r\n                logging.info(\"serverroot: \" + serverroot)\r\n            if stripline.lower().startswith('include'):\r\n                match = stripline.split()\r\n                if len(match) == 2:\r\n                    includefiles = match[1]\r\n                    includefiles = includefiles.strip('\"')\r\n                    if not includefiles.startswith('\/'):\r\n                        includefiles = os.path.join(serverroot, includefiles)\r\n                    content += ProcessMultipleFiles(includefiles) + os.linesep\r\n                else:\r\n                    content += line\r\n            else:\r\n                content += line\r\n    content = RemoveExcessiveLinebreak(content)\r\n    if logging.root.isEnabledFor(logging.DEBUG):\r\n        content += '# End of ' + inputfile + os.linesep + os.linesep\r\n    return content\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    logging.basicConfig(level=logging.DEBUG,\r\n                        format='[%(asctime)s][%(levelname)s]:%(message)s')\r\n    PARSER = argparse.ArgumentParser(\r\n        description=\"\"\"DESCRIPTION: This script combines your APACHE httpd.conf with\r\n                    all included files into one and redirects it to the standard output.\"\"\",\r\n        usage=\".\/combineapacheconfig.py \/etc\/httpd\/conf\/httpd.conf \\\r\n              .\/combineapacheconfig.py -h  \",\r\n        epilog=\"\"\"Author :Ben, Paolo Frigo\"\"\")\r\n    PARSER.add_argument('apacheconf', default=\"\/etc\/httpd\/conf\/httpd.conf\",\r\n                        help='SPECIFY YOUR HTTPD\/APACHE CONF FILE PATH. \\\r\n                            i.e. \/etc\/httpd\/conf\/httpd.conf')\r\n    USERSARGS = PARSER.parse_args()\r\n    try:\r\n        serverroot = os.path.dirname(USERSARGS.apacheconf)\r\n        content = ProcessInput(USERSARGS.apacheconf)\r\n    except Exception as e:\r\n        logging.error(\"Failed to process \" +\r\n                      USERSARGS.apacheconf, exc_info=True)\r\n        exit(1)\r\n    print(content)<\/pre>\n<p>If you need to read or redirect the output you can threat this script as any other typical Linux command.<\/p>\n<pre class=\"lang:sh decode:true\">.\/combineapacheconfig.py \/etc\/httpd\/conf\/httpd.conf | less\r\n.\/combineapacheconfig.py \/etc\/httpd\/conf\/httpd.conf &gt; \/tmp\/my_combined_httpd.conf<\/pre>\n<p>This is the result of the help (-h option).<\/p>\n<pre class=\"lang:sh decode:true\">[root@centos-vm]# .\/combineapacheconfig.py -h\r\n\r\nusage: .\/combineapacheconfig.py \/etc\/httpd\/conf\/httpd.conf               .\/combineapacheconfig.py -h\r\n\r\n \r\n\r\nDESCRIPTION: This script combines your APACHE httpd.conf with all included\r\n\r\nfiles into one and redirects it to the standard output.\r\n\r\n \r\n\r\npositional arguments:\r\n\r\n  apacheconf  SPECIFY YOUR HTTPD\/APACHE CONF FILE PATH. i.e.\r\n\r\n              \/etc\/httpd\/conf\/httpd.conf\r\n\r\n \r\n\r\noptional arguments:\r\n\r\n  -h, --help  show this help message and exit\r\n\r\n \r\n\r\nAuthor :Ben, Paolo Frigo<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Apache is certainly the most popular web server around, it&#8217;s also open-source, multi-platform and serves more than 46% of the websites in the world. Its success goes back to the mid-90s when Apache was created and the internet started to be used more widely and is evolved to the 2.4 version that we use today. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.scriptinglibrary.com\/languages\/python\/merge-all-apache-httpd-conf-with-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Merge All Apache Httpd.Conf with Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":508,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[140,142,143,141,144],"class_list":["post-507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-apache","tag-conf","tag-httpd","tag-merge","tag-tool"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.scriptinglibrary.com\/wp-content\/uploads\/2018\/01\/merge-all-apache-httpd-conf-with-python.png?fit=441%2C190&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8Zjv3-8b","jetpack-related-posts":[{"id":1456,"url":"https:\/\/www.scriptinglibrary.com\/guide\/apache-is-a-swiss-army-knife\/","url_meta":{"origin":507,"position":0},"title":"Apache Is A Swiss Army Knife","author":"Paolo Frigo","date":"August 26, 2019","format":false,"excerpt":"There are tools that are extremely useful and once configured properly will last a long time with little or no maintenance required at all. Web servers are a common example of tools that come to my mind that can be a swiss-army knife and serve a lot of purposes. Whether\u2026","rel":"","context":"In &quot;DevOps&quot;","block_context":{"text":"DevOps","link":"https:\/\/www.scriptinglibrary.com\/category\/devops\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":731,"url":"https:\/\/www.scriptinglibrary.com\/languages\/bash\/youtube-dl-what-a-great-tool-written-in-python\/","url_meta":{"origin":507,"position":1},"title":"youtube-dl What A Great Tool Written In Python","author":"Paolo Frigo","date":"June 21, 2018","format":false,"excerpt":"I absolutely love music in every shape or form and YouTube is an endless source of new inspiration for me. Few months ago I wanted to transcribe some songs, but at that time I was travelling abroad and the internet connection wasn't always available. Searching for a solution, I found\u2026","rel":"","context":"In &quot;BaSH&quot;","block_context":{"text":"BaSH","link":"https:\/\/www.scriptinglibrary.com\/category\/languages\/bash\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":723,"url":"https:\/\/www.scriptinglibrary.com\/languages\/python\/check-if-a-mailbox-exists-with-python-and-smtplib\/","url_meta":{"origin":507,"position":2},"title":"Check if a mailbox exists with Python and smtplib","author":"Paolo Frigo","date":"June 8, 2018","format":false,"excerpt":"Every day we discover different tools that should make our life easier, enchanted by the promise of achieving the best result without any effort. If we can master that tool or technique we can solve many problems even without understanding or finding the root cause, right? So often I'm guilty\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/www.scriptinglibrary.com\/category\/languages\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1605,"url":"https:\/\/www.scriptinglibrary.com\/languages\/bash\/comparing-citrix-cve-verification-tool-to-a-one-liner-bash-script\/","url_meta":{"origin":507,"position":3},"title":"Comparing Citrix CVE Verification Tool to a one-liner bash script","author":"Paolo Frigo","date":"January 30, 2020","format":false,"excerpt":"Last December NIST announced this vulnerability CVE-2019-19781\u00a0 and soon after that Citrix published this page CVE-2019-19781 - Vulnerability in Citrix Application Delivery Controller, Citrix Gateway, and Citrix SD-WAN WANOP appliance and released a verification tool supporting clients for finding out if they were vulnerable or not: CVE-2019-19781 \u2013 Verification Tool\u2026","rel":"","context":"In &quot;BaSH&quot;","block_context":{"text":"BaSH","link":"https:\/\/www.scriptinglibrary.com\/category\/languages\/bash\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1844,"url":"https:\/\/www.scriptinglibrary.com\/languages\/python\/monitoring-an-internet-connection-using-python\/","url_meta":{"origin":507,"position":4},"title":"Monitoring an Internet Connection using Python","author":"Paolo Frigo","date":"June 29, 2021","format":false,"excerpt":"When RaspberryPi 3+ first came out after playing with sensors and an Arduino Uno board for a while, I used it for monitoring wireless and internet connections in different premises. Thanks to the compact size and the low power needed it was the best tool.\u00a0 I thought it would be\u2026","rel":"","context":"In &quot;Guides &amp; Tutorials&quot;","block_context":{"text":"Guides &amp; Tutorials","link":"https:\/\/www.scriptinglibrary.com\/category\/guide\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":334,"url":"https:\/\/www.scriptinglibrary.com\/languages\/python\/first-steps-with-python-setting-up-the-environment\/","url_meta":{"origin":507,"position":5},"title":"First Steps with Python : Setting Up The Environment","author":"Paolo Frigo","date":"December 26, 2017","format":false,"excerpt":"Python is a beautiful programming language and the main key for its success is because is so easy to learn.\u00a0 We strongly suggest to get familiar with Python choosing the approach that suit your pace of learning and programming such video-courses, books, tutorials, etc. Programming it's like any other technical\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/www.scriptinglibrary.com\/category\/languages\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":8,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"predecessor-version":[{"id":1733,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/posts\/507\/revisions\/1733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/media\/508"}],"wp:attachment":[{"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scriptinglibrary.com\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}