Last active
March 5, 2023 06:16
-
-
Save bjoern-r/c3afda16d45edad5f4a5 to your computer and use it in GitHub Desktop.
simple shell xmlrpc client that uses bash & curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author [email protected] | |
TEMP=`getopt -o vt: --long target -n 'example.bash' -- "$@"` | |
if [ $? != 0 ] ; then echo "getopt fail, terminating..." >&2 ; exit 1 ; fi | |
# Note the quotes around `$TEMP': they are essential! | |
eval set -- "$TEMP" | |
Usage() { | |
echo "usage: $0 -t <rpcServUrl> <method> [arg1 arg2 ...]" | |
exit 1 | |
} | |
simpleArg() { | |
local arg="$1" | |
local type=${arg%%:*} val=${arg#*:} | |
echo -e "${indent}<param><value><$type>$val</$type></value></param>" | |
} | |
structArg() { | |
local arg="$1" | |
: parse wait complete ... | |
} | |
while true ; do | |
case "$1" in | |
-t|--target) servUrl=$2; shift 2;; | |
-v) verbose=--verbose; shift;; | |
--) shift; break;; | |
*) echo "Internal error!"; exit 1;; | |
esac | |
done | |
[ -z "$servUrl" ] && Usage | |
[ $# -lt 1 ] && Usage | |
#<method> <arg1> <arg2> ... <argx> | |
generateRequestXml() { | |
method=$1; shift | |
echo '<?xml version="1.0"?>' | |
echo "<methodCall>" | |
echo " <methodName>$method</methodName>" | |
echo " <params>" | |
indent=" " | |
for arg; do | |
indent="${indent} " | |
case $arg in | |
struct:*) structArg "$arg";; | |
*) simpleArg "$arg";; | |
esac | |
done | |
echo " </params>" | |
echo "</methodCall>" | |
} | |
rpcxml=rpc$$.xml | |
generateRequestXml "$@" > $rpcxml | |
[ "$verbose" = "--verbose" ] && cat "$rpcxml" | |
curl -k $verbose --data "@$rpcxml" "$servUrl" 2>/dev/null | xmllint --format - | |
\rm -f $rpcxml |
./xmlrpc-client.sh -t [arg1 arg2 ...]
@joel-depiltech: For simple types it is ./xmlrpc-client.sh --verbose --target "https://api.host.name" -- methodName key1:value1 key2:value2 ...
. I’m still struggling with the struct
type. I just don’t know what the line : parse wait complete ...
should do.
thanks a lot. This is my example.
I run server.py on linux, and use this shell script like this.
./xmlrpc_client.sh -t "xx.xx.xx.xx:8085" -- add int:1 int:2
the server.py is like this:
from xmlrpc.server import SimpleXMLRPCServer
# from SimpleXMLRPCServer import SimpleXMLRPCServer
def add(x, y):
return x + y
if __name__ == '__main__':
s = SimpleXMLRPCServer(('xx.xx.xx.xx', 8085))
s.register_function(add)
s.serve_forever()
Super!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to share this. Please do you have an example ? An example of one command line...