Skip to content

Instantly share code, notes, and snippets.

@bjoern-r
Last active March 5, 2023 06:16
Show Gist options
  • Save bjoern-r/c3afda16d45edad5f4a5 to your computer and use it in GitHub Desktop.
Save bjoern-r/c3afda16d45edad5f4a5 to your computer and use it in GitHub Desktop.
simple shell xmlrpc client that uses bash & curl
#!/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
@sleroy
Copy link

sleroy commented Apr 7, 2017

./xmlrpc-client.sh -t [arg1 arg2 ...]

@m99coder
Copy link

m99coder commented May 19, 2018

@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.

@ahumoon7421
Copy link

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()

@jayvynsong
Copy link

Super!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment