# Run Python script in Bro

**URL:** <https://community.zeek.org/t/run-python-script-in-bro/4995>\
**Category:** Zeek\
**Created:** [August 21, 2017, 8:57pm UTC](https://community.zeek.org/t/run-python-script-in-bro/4995 "2017-08-21T20:57:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chen\_Xu](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@Chen\_Xu](https://community.zeek.org/u/Chen_Xu)\
**Post date:** [August 21, 2017, 8:57pm UTC](https://community.zeek.org/t/run-python-script-in-bro/4995/1 "2017-08-21T20:57:27Z")

</div>

Hello all,

I use the str\_shell\_escape() to run a Python script using command line. I am wondering how I can get the return or print value of Python script, because I will need the result in a if statement in Bro.

Thanks,

Chen Xu

---

<div class="post-metadata">

**Author:** ![Jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jan](https://community.zeek.org/u/Jan)\
**Post date:** [August 22, 2017, 7:44am UTC](https://community.zeek.org/t/run-python-script-in-bro/4995/2 "2017-08-22T07:44:25Z")

</div>

Hi Chen,

> I use the str\_shell\_escape() to run a Python script using command line. I  
> am wondering how I can get the return or print value of Python script,  
> because I will need the result in a if statement in Bro.

str\_shell\_escape() is used to escape strings in that context. I think system() is what you are looking for: [https://www.bro.org/sphinx/scripts/base/bif/bro.bif.bro.html#id-system](https://www.bro.org/sphinx/scripts/base/bif/bro.bif.bro.html#id-system)

Jan

---

<div class="post-metadata">

**Author:** ![seth](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/seth/32/642_2.png) [@seth](https://community.zeek.org/u/seth)\
**Post date:** [August 22, 2017, 5:55pm UTC](https://community.zeek.org/t/run-python-script-in-bro/4995/3 "2017-08-22T17:55:32Z")

</div>

The "Exec" library is what you want to use. You can use it like this...

&nbsp;&nbsp;when ( local ret = Exec::run([$cmd="ls"]) )  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;print ret$exit\_code;  
&nbsp;&nbsp;&nbsp;&nbsp;print ret$stdout;  
&nbsp;&nbsp;&nbsp;&nbsp;}

The only thing to keep in mind is that the body of "when" statements are only executed once the condition has completed. The body of the when statement will be "registered" and execution will immediately continue below the when statement so you many have to do something like have your if statement you want to do \*inside\* the when statement's body.

&nbsp;&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:45pm UTC](https://community.zeek.org/t/run-python-script-in-bro/4995/4 "2022-05-06T15:45:13Z")

</div>


