<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>article Automating the Palo Alto NGFW's Process/Deamon Restarts with Python PEXPECT in General Articles</title>
    <link>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/ta-p/1253717</link>
    <description>&lt;DIV class="lia-message-template-content-zone"&gt;
&lt;P&gt;In my previous article, &lt;I data-index-in-node="24" data-path-to-node="6"&gt;“&lt;A href="https://live.paloaltonetworks.com/t5/community-blogs/automating-the-palo-alto-ngfw-s-process-deamon-restarts/ba-p/531963" target="_blank" rel="noopener"&gt;Automating the Palo Alto NGFW's Process/Deamon Restarts&lt;/A&gt;,”&lt;/I&gt; I detailed how to orchestrate process restarts using TCL Expect paired with Bash scripts. While TCL Expect remains a classic approach for SSH-based network automation, modern engineering workflows are far better served by utilizing Python alongside the &lt;CODE data-index-in-node="336" data-path-to-node="6"&gt;pexpect&lt;/CODE&gt; module. &lt;I data-index-in-node="352" data-path-to-node="6"&gt;(I highly recommend reviewing that original article for foundational context).&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also previously demonstrated how to achieve this scale using Ansible AWX inside LIVEcommunity (&lt;I data-index-in-node="97" data-path-to-node="7"&gt;“&lt;A href="https://live.paloaltonetworks.com/t5/general-articles/use-ansible-awx-to-automate-the-palo-alto-ngfw-s-management-and/ta-p/1252412" target="_blank" rel="noopener"&gt;Use Ansible AWX to automate the Palo Alto NGFW's management and even Process/Daemon Restarts!&lt;/A&gt;”&lt;/I&gt;). &lt;SPAN class="citation-511"&gt;However, if your environment already runs &lt;/SPAN&gt;&lt;STRONG data-index-in-node="237" data-path-to-node="7"&gt;&lt;SPAN class="citation-511"&gt;Cortex XSOAR&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN class="citation-511"&gt; or &lt;/SPAN&gt;&lt;STRONG data-index-in-node="253" data-path-to-node="7"&gt;&lt;SPAN class="citation-511"&gt;Cortex XSIAM&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN class="citation-511 citation-end-511"&gt;, spinning up an entire multi-container Ansible AWX infrastructure—potentially managed on top of Kubernetes (K8s)—just to trigger a process restart is likely excessive.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="citation-511 citation-end-511"&gt;Because Cortex XSOAR already features robust, native content packs for standard PAN-OS commands (&lt;A href="https://cortex.marketplace.pan.dev/marketplace/details/PANOS/" target="_blank" rel="noopener"&gt;available via the Palo Alto Networks Marketplace&lt;/A&gt;), we can streamline the architecture. By embedding a custom &lt;CODE data-index-in-node="206" data-path-to-node="8"&gt;pexpect&lt;/CODE&gt; script directly into XSOAR, the process restart logic integrates cleanly into a broader, automated CI/CD pipeline without the overhead of external container orchestrators.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="nikoolayy1_0-1778420526161.png" style="width: 999px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/71379iD9B52E9967853F4E/image-size/large?v=v2&amp;amp;px=999" role="button" title="nikoolayy1_0-1778420526161.png" alt="nikoolayy1_0-1778420526161.png" /&gt;&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get started, you will need an environment running Ubuntu with Python 3 (installed by default in recent Ubuntu distributions). We recommend using a Python virtual environment (&lt;CODE data-index-in-node="178" data-path-to-node="9"&gt;venv&lt;/CODE&gt;) to isolate your &lt;CODE data-index-in-node="200" data-path-to-node="9"&gt;pip&lt;/CODE&gt; packages. Think of a virtual environment as one of the original forms of virtualization pre-dating Docker containers—though, as demonstrated at the end of this guide, building a dedicated container remains a highly effective option as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;python3 --version
Python 3.12.3

apt install -y python3-venv
python3 -m venv /home/niki/panos-venv
source /home/niki/panos-venv/bin/activate
pip install pexpect packaging
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The script below demonstrates how to programmatically restart a specific daemon process across multiple Palo Alto Networks firewalls sequentially. The script reads targeted firewall management IP addresses or hostnames line-by-line from a local file named &lt;CODE data-index-in-node="256" data-path-to-node="4"&gt;firewalls.txt&lt;/CODE&gt; located in the same directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;#!/usr/bin/env python3

import pexpect
import sys
import getpass

USERNAME = input("Username: ")
PASSWORD = getpass.getpass("Password: ")

#
# Process variable
#
PROCESS_NAME = "web-server"

PROMPT = r"\S+@\S+&amp;gt;"

with open("firewalls.txt") as f:
    FW_LIST = [line.strip() for line in f if line.strip()]

for FW_IP in FW_LIST:

    print(f"\n===== CONNECTING TO {FW_IP} =====\n")

    child = pexpect.spawn(
        f"ssh -tt -o StrictHostKeyChecking=no {USERNAME}@{FW_IP}",
        encoding="utf-8",
        timeout=120
    )

    child.logfile = sys.stdout

    #
    # Login
    #
    child.expect("Password:")
    child.sendline(PASSWORD)

    child.expect(PROMPT)

    #
    # CLI settings
    #
    child.sendline("set cli scripting-mode on")
    child.expect(PROMPT)

    child.sendline("set cli pager off")
    child.expect(PROMPT)

    child.sendline("set cli terminal width 500")
    child.expect(PROMPT)

    #
    # Restart process
    #
    COMMAND = f"debug software restart process {PROCESS_NAME}"

    print(f"\nRunning command: {COMMAND}\n")

    child.sendline(COMMAND)

    #
    # Wait for command echo
    #
    child.expect(COMMAND)

    #
    # Wait for prompt
    #
    child.expect(PROMPT)

    output = child.before

    print("\n========== COMMAND OUTPUT ==========\n")
    print(output)
    print("\n====================================\n")

    #
    # Exit
    #
    child.sendline("exit")
    child.expect(pexpect.EOF)

    print(f"\nSession closed for {FW_IP}\n")&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="3"&gt;Below is an example execution trace running the automation script. The username and password credentials are securely collected at runtime via prompt inputs, and the target firewall parameters are parsed sequentially out of the local &lt;CODE data-index-in-node="234" data-path-to-node="3"&gt;firewalls.txt&lt;/CODE&gt; file.&lt;/P&gt;
&lt;P data-path-to-node="3"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="4"&gt;In this execution log, the script successfully authenticates to the firewall, configures the terminal environment variables, and executes the target daemon restart command:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt; python restart-script.py sslmgr
Username: admin
Password:

===== CONNECTING TO 192.168.1.108 =====

(admin@192.168.1.108) Password: xxxx

Last login: Sun May 10 09:12:46 2026 from 192.168.1.106



Number of failed attempts since last successful login: 0



admin@PA-VM&amp;gt; set cli scripting-mode on
admin@PA-VM&amp;gt; set cli scripting-mode on
set cli pager off
set cli terminal width 500

Running command: debug software restart process sslmgr

debug software restart process sslmgr
set cli pager off
admin@PA-VM&amp;gt; set cli terminal width 500
admin@PA-VM&amp;gt; debug software restart process sslmgr
admin@PA-VM&amp;gt;

========== COMMAND OUTPUT ==========




====================================

exit
exit
Process sslmgr was restarted by user admin
admin@PA-VM&amp;gt; Connection to 192.168.1.108 closed.

Session closed for 192.168.1.108
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The source code for this automation script is available on my GitHub repository:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="https://github.com/Nikoolayy1/palo-alto-python-scripts/tree/main" target="_blank" rel="noopener"&gt;Nikoolayy1/palo-alto-python-scripts&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For almost all other operational and configuration tasks, I highly recommend using the&amp;nbsp;&lt;A href="https://pan-os-python.readthedocs.io/en/latest/getting-started.html" target="_blank" rel="noopener"&gt;Palo Alto Networks PAN-OS SDK for Python 1.12.1 documentation&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you try to run a standard operational command like &lt;CODE data-index-in-node="54" data-path-to-node="6"&gt;show system info&lt;/CODE&gt; via raw SSH and extract specific values (such as the software version) using Python's &lt;CODE data-index-in-node="157" data-path-to-node="6"&gt;re&lt;/CODE&gt; (regex) or &lt;CODE data-index-in-node="171" data-path-to-node="6"&gt;xml&lt;/CODE&gt; modules, you will quickly find that parsing raw text or complex XML schemas is incredibly difficult and prone to breaking. Just stick with the&amp;nbsp; Python SDK that uses the API and only use the pexpect module for process restarts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the python SDK you will need the below python packages:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;pip install pan-os-python
pip install setuptools&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;    fw = Firewall(
        hostname=FW_IP,
        api_username=USERNAME,
        api_password=PASSWORD,
    )

    result = fw.op("show system info")

    sw_version = result.find(".//sw-version").text
    hostname = result.find(".//hostname").text
    logdb_version = result.find(".//logdb-version").text

    print("hostname:", hostname)
    print("sw-version:", sw_version)
    print("logdb-version:", logdb_version&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="1"&gt;Emphasizing the power of combining the two tools (the Python SDK for conditional checking and &lt;CODE data-index-in-node="139" data-path-to-node="1"&gt;pexpect&lt;/CODE&gt; for execution) to build smart automation.&lt;/P&gt;
&lt;P data-path-to-node="3"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="3"&gt;You can find a complete, end-to-end example on my GitHub repository demonstrating how to combine these two methodologies effectively.&lt;/P&gt;
&lt;P data-path-to-node="4"&gt;The complete script uses the PAN-OS Python SDK to cleanly query the firewall's current software version. If the SDK detects a specific target baseline—for instance, PAN-OS &lt;CODE data-index-in-node="172" data-path-to-node="4"&gt;10.1.4&lt;/CODE&gt;—the script then conditionally triggers the fallback &lt;CODE data-index-in-node="231" data-path-to-node="4"&gt;pexpect&lt;/CODE&gt; SSH routine to perform the daemon process restart.&lt;/P&gt;
&lt;P data-path-to-node="5"&gt;Attempting to build this type of conditional logic using legacy TCL Expect would be extraordinarily difficult. Fetching raw SSH output, passing it across tools, and attempting to parse the string variables manually using regex or Bash filters is simply too fragile for modern production workflows. Combining the native SDK with &lt;CODE data-index-in-node="328" data-path-to-node="5"&gt;pexpect&lt;/CODE&gt; provides a highly robust, enterprise-ready automation wrapper.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/Nikoolayy1/palo-alto-python-scripts/blob/main/restart-if-version.py" target="_blank" rel="noopener"&gt;palo-alto-python-scripts/restart-if-version.py at main · Nikoolayy1/palo-alto-python-scripts&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="3"&gt;To package your automation dependencies seamlessly and ensure your scripts run consistently across your entire team, you can wrap your environment inside a Docker container.&lt;/P&gt;
&lt;P data-path-to-node="4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-path-to-node="4"&gt;The configuration below establishes a persistent volume mount between your host operating system's directory (&lt;CODE data-index-in-node="110" data-path-to-node="4"&gt;&amp;lt;home directory&amp;gt;/scripts/py&lt;/CODE&gt;) and the container workspace. This allows you to easily share and modify Python scripts on your local system while the container handles the secure execution baseline.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;cat Dockerfile
FROM python:3.11-slim

RUN apt-get update &amp;amp;&amp;amp; apt-get install -y \
    openssh-client \
    sshpass \
    iputils-ping \
    curl \
    &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir \
    pexpect \
    packaging \
    pan-os-python

WORKDIR /app

COPY . /app

CMD ["/bin/bash"]


docker build -t panos-python:1.0 .

docker run -it --rm   -v /home/niki/scripts/py:/app   panos-python:1.0&lt;/LI-CODE&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 09 Jun 2026 13:30:44 GMT</pubDate>
    <dc:creator>nikoolayy1</dc:creator>
    <dc:date>2026-06-09T13:30:44Z</dc:date>
    <item>
      <title>Automating the Palo Alto NGFW's Process/Deamon Restarts with Python PEXPECT</title>
      <link>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/ta-p/1253717</link>
      <description>&lt;P&gt;Discover how to streamline your Palo Alto Networks NGFW operations by leveraging the power of Python, &lt;CODE data-index-in-node="276" data-path-to-node="2"&gt;pexpect&lt;/CODE&gt;, and the PAN-OS Python SDK to conditionally trigger daemon process restarts. Learn how to integrate this lean logic straight into your Cortex XSOAR/XSIAM CI/CD pipelines or package it seamlessly across your team using Docker containers.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2026 13:30:44 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/ta-p/1253717</guid>
      <dc:creator>nikoolayy1</dc:creator>
      <dc:date>2026-06-09T13:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: Automating the Palo Alto NGFW's Process/Deamon Restarts with Python PEXPECT</title>
      <link>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/tac-p/1255770#M862</link>
      <description>&lt;P&gt;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/153031"&gt;@nikoolayy1&lt;/a&gt;&amp;nbsp;Terrific breakdown! I really appreciate the context you provided around Cortex XSOAR!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2026 21:12:35 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/tac-p/1255770#M862</guid>
      <dc:creator>crasmussen</dc:creator>
      <dc:date>2026-06-09T21:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: Automating the Palo Alto NGFW's Process/Deamon Restarts with Python PEXPECT</title>
      <link>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/tac-p/1255899#M864</link>
      <description>&lt;P&gt;Another great contribution &lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/153031"&gt;@nikoolayy1&lt;/a&gt;,&amp;nbsp;thank you for sharing! The depth of knowledge and practical experience you bring to your content is always evident, and it is a real benefit to the community. Appreciate you taking the time to share your expertise.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2026 16:00:29 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/general-articles/automating-the-palo-alto-ngfw-s-process-deamon-restarts-with/tac-p/1255899#M864</guid>
      <dc:creator>Masharad</dc:creator>
      <dc:date>2026-06-10T16:00:29Z</dc:date>
    </item>
  </channel>
</rss>

