Skip to content

Instantly share code, notes, and snippets.

@kwirk
Last active December 16, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwirk/5484915 to your computer and use it in GitHub Desktop.
Save kwirk/5484915 to your computer and use it in GitHub Desktop.
fail2ban/fail2ban issue #190
diff --git a/fail2ban/server/action.py b/fail2ban/server/action.py
index 6c36bbc..2b8acd5 100644
--- a/fail2ban/server/action.py
+++ b/fail2ban/server/action.py
@@ -27,7 +27,7 @@ __date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
-import logging, os, subprocess, time, signal
+import logging, os, subprocess, time, signal, tempfile
import threading
#from subprocess import call
@@ -353,8 +353,11 @@ class Action:
logSys.debug(realCmd)
_cmd_lock.acquire()
try: # Try wrapped within another try needed for python version < 2.5
+ stdout = tempfile.TemporaryFile(suffix=".stdout", prefix="fai2ban_")
+ stderr = tempfile.TemporaryFile(suffix=".stderr", prefix="fai2ban_")
try:
- popen = subprocess.Popen(realCmd, shell=True)
+ popen = subprocess.Popen(
+ realCmd, stdout=stdout, stderr=stderr, shell=True)
stime = time.time()
retcode = popen.poll()
while time.time() - stime <= timeout and retcode is None:
@@ -376,6 +379,14 @@ class Action:
finally:
_cmd_lock.release()
+ std_level = retcode == 0 and logging.DEBUG or logging.ERROR
+ stdout.seek(0)
+ logSys.log(std_level, "%s stdout: %s" % (realCmd, `stdout.read()`))
+ stdout.close()
+ stderr.seek(0)
+ logSys.log(std_level, "%s stderr: %s" % (realCmd, `stderr.read()`))
+ stderr.close()
+
if retcode == 0:
logSys.debug("%s returned successfully" % realCmd)
return True
diff --git a/fail2ban/tests/actiontestcase.py b/fail2ban/tests/actiontestcase.py
index 6cac0c1..abeab7d 100644
--- a/fail2ban/tests/actiontestcase.py
+++ b/fail2ban/tests/actiontestcase.py
@@ -103,3 +103,11 @@ class ExecuteAction(unittest.TestCase):
self.assertAlmostEqual(time.time() - stime, 2.1, places=1)
self.assertTrue(self._is_logged('sleep 60 timed out after 2 seconds'))
self.assertTrue(self._is_logged('sleep 60 killed with SIGTERM'))
+
+ def testCaptureStdOutErr(self):
+ Action.executeCmd('echo "How now brown cow"')
+ self.assertTrue(self._is_logged("'How now brown cow\\n'"))
+ Action.executeCmd(
+ 'echo "The rain in Spain stays mainly in the plain" 1>&2')
+ self.assertTrue(self._is_logged(
+ "'The rain in Spain stays mainly in the plain\\n'"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment