Call variable in CURL Command - shell

I am trying to define a variable for a curl command.
curl --location -k --request GET 'https://myprojt.test9.abc.com/api/part/config=8594&select=parts,Action,refernceNumber&SalesID=333&partNumber=789-635'
I want to call a variable for &partNumber=789-635
Tried defining $part='#option.partnumber#', this is the input parameter which takes a value.
curl --location -k --request GET 'https://myprojt.test9.abc.com/api/part/config=8594&select=parts,Action,refernceNumber&SalesID=333&partNumber=$part'
I even tried replacing single quotes (') by double (") but not working, kindly help.

You can assign directly like MYVAR=#option.myoption#
I leave a working example:
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='myoption' value='world' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>e781836f-e0f8-4ccb-b03f-a384be306860</id>
<loglevel>INFO</loglevel>
<name>JobInlineScript</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[# starting
MYVAR=#option.myoption#
# print
echo "hello $MYVAR"]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>e781836f-e0f8-4ccb-b03f-a384be306860</uuid>
</job>
</joblist>
And here the result.

Related

How to check if an attribute is present in an xml node using xmllint

I'm using bash and xmllint to check nodes in the following xml:
<?xml version="1.0" encoding="utf-8"?>
<output>
<document>
<sentence id="13">
<text>This is a test sentence.</text>
<entities>
<annotation id="3">
<grammar-form id="0" normal-form="THIS"/>
</annotation>
<annotation id="4">
<grammar-form id="0" normal-form="IS"/>
</annotation>
<annotation id="5">
<grammar-form id="0" normal-form="A"/>
</annotation>
<annotation id="6">
<grammar-form id="0" normal-form="TEST"/>
</annotation>
<annotation id="7">
<grammar-form id="0" normal-form="SENTENCE"/>
</annotation>
<annotation id="12">
<grammar-form id="0" normal-form="."/>
</annotation>
</entities>
</sentence>
</document>
</output>
How can I simply check that each grammar-form node has a normal-form attribute present? It doesn't matter what the attribute value is, I just need to check that it is present.
It's easier to select grammar-forms that don't have the attribute and see if you get any matches or not:
if xmllint --xpath '//grammar-form[not(#normal-form)]' input.xml 1>/dev/null 2>&1; then
echo "There are missing normal forms."
else
echo "There are no missing normal forms."
fi
In xpath mode, xmllint will print the matching paths, or if nothing matches, exit with a error code of 10 and print a message to that effect to standard error (the --noout option mentioned in the manpage to suppress output doesn't do anything in the version I'm testing with, unfortunately), hence the redirections.

How to redirect long string with single/double quotes var in Rundeck?

thank you for taking time reading this question.
I've a Rundeck job with multiple steps. Basically, step 1 and 2 is fetching a long string which is under ' '. Example:
'This is a long string.. and is also under "double quotes" '. -> This variable is stored as the following form: #option.mylongstring#
Third step of my Rundeck job is failing because I'm having issues with single and multiple quotes in my string. I want to extract specific values from that long string
My solution was to send the content of #option.mylongstring# in a temp file and apply sed to convert single quotes into double quotes (sed "s/'/\"/g") and from there, extract the information that I need.
Anyway, seems that the redirection is not happening in Rundeck: echo #option.mylongstring# &> $TEMPFILE is doing nothing, generating an empty file.
Anyone faced the same issue?
Using inline-script works without problems, let me share the job definition example:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>5e7123ce-c9b7-4bfa-a0e8-6484a9bd7c4f</id>
<loglevel>INFO</loglevel>
<name>LongStringExample</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[echo 'hello "world"' > myfile.txt]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>5e7123ce-c9b7-4bfa-a0e8-6484a9bd7c4f</uuid>
</job>
</joblist>
Using an option:
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='opt1' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>22d7286f-7be9-4aaf-92ae-8e5bf5277d67</id>
<loglevel>INFO</loglevel>
<name>AnotherLongStringExample</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[echo 'this is another "#option.opt1#"' > another_file.txt]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>22d7286f-7be9-4aaf-92ae-8e5bf5277d67</uuid>
</job>
</joblist>

Q: How to sanitise XML files with xmlstarlet?

I've got several xml files to sanitise via the command line tool xmlstarlet (1.6.1).
Sample 1
<?xml version="1.0" encoding="utf-8"?>
<!-- Some license comment
- with some link to http://example.com/foo/ -->
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Name Sample 1</ShortName>
<Description>Description Sample 1</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image height="16" width="16">data:...</Image>
<Url type="text/html" method="get" template="https://examplesearch.com/" rel="searchform">
<Param name="q" value="{searchTerms}"/>
<MozParam name="m1" condition="purpose" value="abc"/>
<MozParam name="m2" condition="purpose" value="cde"/>
</Url>
</OpenSearchDescription>
Sample 2
<!-- Some license comment
- with some link to http://example.com/foo/ -->
<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Name Sample 2</ShortName>
<Description>Description Sample 2</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:...</Image>
<Url type="application/x-suggestions+json" method="GET" template="https://www.examplesearch.com/search?client=firefox&q={searchTerms}"/>
<Url type="text/html" method="GET" template="https://examplesearch.com/search" rel="searchform">
<Param name="q" value="{searchTerms}"/>
</Url>
</SearchPlugin>
I try to clear the Description node first which works for sample 2:
xml ed -L -u "//_:SearchPlugin/_:Description" -v "" sample2.xml
The result is the node <Description/>, but the same logic does not work for sample 1:
xml ed -L -u "//_:OpenSearchDescription/_:Description" -v "" sample1.xml
I'd like to receive <Description><Description/> as results for both xml samples.
Update: The previous part has been solved.
Secondly, in sample 2 I'd like to remove client=firefox& out of the second Url template value:
"https://www.examplesearch.com/search?client=firefox&q={searchTerms}"
I've got no clue how to apply a regex/xslt operation on the value to achieve this. Any suggestions how this could be done?

OpenDayLight Oxygen Netconf mount data-missing error

I am using ODL oxygen release to connect a netopeer-server (NETCONF host) using the below curl command. I see the following error being returned. May I know what exactly is wrong? netopeer server supports netconf-monitoring.
{"errors":{"error":[{"error-type":"protocol","error-tag":"data-missing","error-message":"Mount point does not exist."}]}}
curl -H "Content-Type: application/xml" -u admin:admin -X POST -d "<?xml version=\"1.0\" encoding=\"UTF-8\"> <module xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">prefix:sal netconf-connector</type>
<name>netopeer</name>
<address xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">192.168.56.101</address>
<port xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">830</port>
<username xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">netopeer-server</username>
<password xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">pass</password>
<tcp-only xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">true</tcp-only>
<event-executor xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:netty">prefix:netty-event-executor</type>
<name>global-event-executor</name>
</event-executor>
<binding-registry xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">prefix:binding-broker-osgi-registry</type>
<name>binding-osgi-broker</name>
</binding-registry>
<dom-registry xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom">prefix:dom-broker-osgi-registry</type>
<name>dom-broker</name>
</dom-registry>
<client-dispatcher xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:netconf">prefix:netconf-client-dispatcher</type>
<name>global-netconf-dispatcher</name>
</client-dispatcher>
<processing-executor xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:threadpool">prefix:threadpool</type>
<name>global-netconf-processing-executor</name>
</processing-executor>
<keepalive-executor xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">
<type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:threadpool">prefix:scheduled-threadpool</type>
<name>global-netconf-ssh-scheduled-executor</name>
</keepalive-executor>
</module>" http://localhost:8181/restconf/config/network-topology:network-topology/topology/topology-netconf/node/controller-config/yang-ext:mount/config:modules
I did try the suggested changes. COuld you please tell me what is wrong with the input? I do not see anything obvious!
curl -H "Content-Type: application/xml" -u admin:admin -X PUT -d "<?xml version=\"1.0\" encoding=\"UTF-8\"><node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">
<node-id>netopeer</node-id>
<host xmlns=\"urn:opendaylight:netconf-node-topology\">192.168.56.101</host>
<port xmlns=\"urn:opendaylight:netconf-node-topology\">830</port>
<username xmlns=\"urn:opendaylight:netconf-node-topology\">netopeergui</username>
<password xmlns=\"urn:opendaylight:netconf-node-topology\">netopeergui</password>
<tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>
</node>" http://localhost:8181/restconf/config/network-topology:network-topology/topology/topology-netconf/node/netopeer
{"errors":{"error":[{"error-type":"protocol","error-tag":"malformed-message","error-message":"**Error parsing input: A pseudo attribute name is expected.** ","error-info":"A pseudo attribute name is expected. "}]}}shrikanth#mds:~/Applications/karaf-0.8.1/bin$
This worked after adding the ? at to the xml tag as below
curl -H "Content-Type: application/xml" -u admin:admin -X PUT -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\">
<node-id>netopeer</node-id>
<host xmlns=\"urn:opendaylight:netconf-node-topology\">192.168.56.101</host>
<port xmlns=\"urn:opendaylight:netconf-node-topology\">830</port>
<username xmlns=\"urn:opendaylight:netconf-node-topology\">netopeergui</username>
<password xmlns=\"urn:opendaylight:netconf-node-topology\">netopeergui</password>
<tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only>
</node>" http://localhost:8181/restconf/config/network-topology:network-topology/topology/topology-netconf/node/netopeer
This seems like old way of mounting devices through config sub-system, can you try with this more recent API: http://docs.opendaylight.org/en/stable-oxygen/user-guide/netconf-user-guide.html#netconf-connector-configuration-with-md-sal

diff -I (uppercase 'i') not working when options are passed as variable

I'm writing a script to find out the diff between files using the GNU version of the diff command. Here I need to ignore the html comment <!-- and any patterns (provided as input through a file) that is matched.
File wxy/a:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
some text here
<property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/>
<!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /-->
</beans>
File xyz/a:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
some text there
<property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/>
<!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /-->
</beans>
Pattern input file: input.conf:
[a]
http://.*[:0-9]*/ab/ssoLogin
[some other file]
....
....
My script would read the input.conf for the filename [a] and puts to a temp file lines_to_ignore, now I read the file lines_to_ignore and append the pattern to a variable like below
compare_file.sh
diff_ignore_options="-I \"\!--\"" # Ignore option for <!-- Comments
for iline in `cat lines_to_ignore`; do
diff_ignore_options=${diff_ignore_options}" -I \"$iline\""
echo "-----------------------------------------------------------"
diff -I "\!--" -I "$iline" wxy/a xyz/a
echo "-----------------------------------------------------------"
done
diff $diff_ignore_options wxy/a xyz/a
Now the output:
-----------------------------------------------------------
19c19
< some text here
---
> some text there
-----------------------------------------------------------
19,21c19,21
< some text here
< <property name="loginUrl" value="http://localhost:15040/ab/ssoLogin"/>
< <!--property name="cUrl" value="http://localhost:15040/ab/ssoLogin" /-->
---
> some text there
> <property name="loginUrl" value="http://localhost:15045/ab/ssoLogin"/>
> <!--property name="cUrl" value="http://localhost:15045/ab/ssoLogin" /-->
Why is the variable substitution in diff command not working?
diff $diff_ignore_options wxy/a xyz/a
I want to do it the variable way because I might have to match more than one pattern in some files.
The problem is the ! character, which the shell uses for history expansion. Furthermore, you're including escaped double-quote characters in your $diff_ignore_options variable; since the pattern you want to ignore doesn't include any " characters, you don't want that.
This should work (note the use of single quotes to avoid treating ! as a metacharacter):
diff_ignore_options='-I !--'
diff $diff_ignore_options this_file that_file
And you can then add more patterns like this:
diff_ignore_options="$diff_ignore_options -I foobar"

Resources