Compare commits
8 commits
Author | SHA1 | Date | |
---|---|---|---|
Sven Velt | baa916df81 | ||
092b3a1103 | |||
76065290c5 | |||
9862a7bade | |||
ba1cff4d61 | |||
f0a969d40f | |||
be2ff6a2cd | |||
f8934c59f9 |
|
@ -1,16 +1,40 @@
|
||||||
#!/usr/bin/env python
|
#!/bin/sh
|
||||||
|
'''':
|
||||||
|
for pyint in /usr/libexec/platform-python python3 python python2; do
|
||||||
|
command -v $pyint >/dev/null 2>&1 && exec $pyint "$0" "$@"
|
||||||
|
done
|
||||||
|
echo "$0: No python could be found" >&2
|
||||||
|
exit 1
|
||||||
|
# '''
|
||||||
|
|
||||||
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import syslog
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--verbose', '-v', action='count', default=0)
|
||||||
|
parser.add_argument('--silent', action='store_true', default=False)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.silent:
|
||||||
|
args.verbose = -1
|
||||||
|
|
||||||
|
if args.verbose >= 0:
|
||||||
|
syslog.openlog(
|
||||||
|
ident=sys.argv[0],
|
||||||
|
logoption=syslog.LOG_PID,
|
||||||
|
facility=syslog.LOG_LOCAL3 | syslog.LOG_ERR
|
||||||
|
)
|
||||||
|
|
||||||
allowed = [
|
allowed = [
|
||||||
##### System informations
|
##### System informations
|
||||||
r'^/usr/bin/lsb_release\s+-d$', # Linux
|
r'^/usr/bin/lsb_release\s+-d$', # Linux
|
||||||
r'^/(usr/)?bin/uname\s+-mrs$', # Linux, BSD & others
|
r'^/(usr/)?bin/uname\s+-mrs$', # Linux, BSD & others
|
||||||
r'''^/(usr/)?s?bin/awk -F'"' -e '/PRETTY_NAME/{ print \$2; }' /etc/os-release''', # Linux: /etc/os-release via awk for get_os.py
|
r'''^/(usr/)?s?bin/awk -F'"' (-e\s*)?'/PRETTY_NAME/{ print \$2; }' /etc/os-release''', # Linux: /etc/os-release via awk for get_os.py
|
||||||
|
|
||||||
##### Complete command lines (Monitoring-Plugins on Debian)
|
##### Complete command lines (Monitoring-Plugins on Debian)
|
||||||
r'^/usr/lib/nagios/plugins/check_disk -w \d+% -c \d+% -p /[/a-z]*$',
|
r'^/usr/lib/nagios/plugins/check_disk -w \d+% -c \d+% -p /[/a-z]*$',
|
||||||
|
@ -33,22 +57,37 @@ allowed = [
|
||||||
|
|
||||||
cmdline = os.getenv('SSH_ORIGINAL_COMMAND')
|
cmdline = os.getenv('SSH_ORIGINAL_COMMAND')
|
||||||
if not cmdline:
|
if not cmdline:
|
||||||
print 'This is just a wrapper, no command specified!'
|
print('This is just a wrapper, no command specified!')
|
||||||
|
if args.verbose >= 0:
|
||||||
|
syslog.syslog('Called without SSH_ORIGINAL_COMMAND')
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
|
||||||
for maybe in allowed:
|
for maybe in allowed:
|
||||||
if re.match(maybe, cmdline):
|
if re.match(maybe, cmdline):
|
||||||
cmdline = shlex.split(cmdline)
|
if args.verbose >= 2:
|
||||||
|
syslog.syslog(syslog.LOG_INFO, 'Found command line >%s< with regexp >%s<' % ( cmdline, maybe ) )
|
||||||
|
cmdlinelist = shlex.split(cmdline)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cmd = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
|
cmd = subprocess.Popen(cmdlinelist, stdout=subprocess.PIPE)
|
||||||
except Exception, exc:
|
except Exception as exc:
|
||||||
print 'Could not execute plugin ("%s"): %s' % (' '.join(cmdline), exc)
|
print('Could not execute plugin: %s' % exc)
|
||||||
|
if args.verbose >= 0:
|
||||||
|
syslog.syslog('Could not execute plugin >%s<' % cmdline)
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
else:
|
else:
|
||||||
print cmd.communicate()[0].rstrip()
|
(out, outerr) = cmd.communicate()
|
||||||
|
out = out.rstrip().decode('utf-8')
|
||||||
|
outerr = (outerr or b'').rstrip().decode('utf-8')
|
||||||
|
print(out)
|
||||||
|
if args.verbose >= 1:
|
||||||
|
syslog.syslog('Executed command line >%s<' % cmdline)
|
||||||
|
if args.verbose >= 3:
|
||||||
|
syslog.syslog('Output >%s<, Error >%s<' % (out, outerr))
|
||||||
sys.exit(cmd.returncode)
|
sys.exit(cmd.returncode)
|
||||||
|
|
||||||
print '%s: No allowed command found!' % sys.argv[0]
|
print('%s: No allowed command found!' % sys.argv[0])
|
||||||
|
if args.verbose >= 0:
|
||||||
|
syslog.syslog('No allowed command found for >%s<' % cmdline)
|
||||||
sys.exit(3)
|
sys.exit(3)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue