Commit e4cd84cb authored by Ad Schellevis's avatar Ad Schellevis

(configd) make client tool more verbose and add -m option to execute multiple commands at once.

parent 9e04b808
......@@ -34,35 +34,76 @@
"""
import socket
import os.path
import traceback
import syslog
import sys
__author__ = 'Ad Schellevis'
configd_socket_name = '/var/run/configd.socket'
def exec_config_cmd(exec_command):
""" execute command using configd socket
:param exec_command: command string
:return: string
"""
# Create and open unix domain socket
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(configd_socket_name)
except socket.error:
syslog.syslog(syslog.LOG_ERR,'unable to connect to configd socket (@%s)'%configd_socket_name)
print('unable to connect to configd socket (@%s)'%configd_socket_name)
return None
try:
sock.send(exec_command)
data = ""
while True:
line = sock.recv(4096)
if line:
data = data + line
# end of stream marker found, exit
if data.find("%c%c%c"%(chr(0),chr(0),chr(0))) > -1:
break
return (data[:-3])
except:
print ('error in configd communication %s, see syslog for details')
syslog.syslog(syslog.LOG_ERR,'error in configd communication \n%s'%traceback.format_exc())
finally:
sock.close()
# set a timeout to the socket
socket.setdefaulttimeout(120)
# validate parameters
if len(sys.argv) <= 1:
print 'usage : %s <command>'%sys.argv[0]
print ('usage : %s [-m] <command>'%sys.argv[0])
sys.exit(0)
# check if configd socket exists
if not os.path.exists(configd_socket_name):
print ('configd socket missing (@%s)'%configd_socket_name)
sys.exit(-1)
if sys.argv[1] == '-m':
# execute multiple commands at once ( -m "action1 param .." "action2 param .." )
for exec_command in sys.argv[2:]:
result=exec_config_cmd(exec_command=exec_command)
if result is None:
sys.exit(-1)
print('%s'%(result))
else:
for exec_command in sys.argv[1:]:
# Create and open unix domain socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(configd_socket_name)
try:
sock.send(exec_command)
data = ""
while True:
line = sock.recv(4096)
if line:
data = data + line
# end of stream marker found, exit
if data.find("%c%c%c"%(chr(0),chr(0),chr(0))) > -1:
break
print (data[:-3])
finally:
sock.close()
# execute single command sequence
exec_command=' '.join(sys.argv[1:])
result=exec_config_cmd(exec_command=exec_command)
if result is None:
sys.exit(-1)
print('%s'%(result))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment