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

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>

Related

How to solve NO_ROUTE_DESTINATION when connecting from two FreeSWITCH

I am new to FreeSWITCH and I am trying to bridge a call from two different FreeSWITCH (SwitchA -> SwitchB ).
But when I am trying to make a call, It says NO_ROUTE_DESTINATION .
Here is my current configuration for FreeSWITCH-A (the call originates)
My sip_profile/external
<include>
<gateway name="fs-test2">
<param name="proxy" value="ipOfSwitch-B:5080"/>
<param name="register" value="false"/>
<param name="called-id-in-from" value="true"/>
<variables>
<variable name="verbose_sdb" value="true"/>
<variable name="absolute_codec_string" value="PCMU, PCMA" direction="oubound"/>
</variables>
</gateway>
</include>
My DialPlan
dialplan/default
<include>
<extension name="outbound_call">
<condition field="destination_number" expression="^(18881)$">
<action application="bridge" data="sofia/gateway/fs-test2/$1"/>
</condition>
</extension>
</include>
dialplan/public
<include>
<extension name="public-did">
<condition field="destination_number" expression="^(19991)$">
<action application="set" data="domain_name=$${domain}"/>
<action application="transfer" data="1000 XML default"/>
<action application="answer"/>
<action application="sleep" data="1000"/>
</condition>
</extension>
</include>
And here is my current configuration for FreeSWITCH-B
My sip_profile/external
<include>
<gateway name="fs-test1">
<param name="proxy" value="ipOfSwitch-A:5080"/>
<param name="register" value="false"/>
<param name="caller-id-in-from" value="true"/>
</gateway>
</include>
My dialplan
dialplan/default
<include>
<extension name="outbound_call">
<condition field="destination_number" expression="^(18881)$">
<action application="bridge" data="sofia/gateway/fs-test1/$1"/>
</condition>
</extension>
</include>
dialplan/public
<include>
<extension name="public-did">
<condition field="destination_number" expression="^(18881)$">
<action application="set" data="domain_name=$${domain}"/>
<action application="transfer" data="1000 XML default"/>
</condition>
</extension>
</include>
This is my sofia status profile looks like in SWITCH A-
-----------------------------------------------------------------
Profile::Gateway-Name| Data | Status |
-----------------------------------------------------------------
external::fs-test2 | sip:FreeSWITCH#ipOfSwitchB:5080 | NOREG |
-----------------------------------------------------------------
This is my sofia status profile looks like in SWITCH B-
-----------------------------------------------------------------
Profile::Gateway-Name| Data | Status |
-----------------------------------------------------------------
external::fs-test1 | sip:FreeSWITCH#ipOfSwitchA:5080 | NOREG |
-----------------------------------------------------------------
and when I type the command for sofia status gateway fs-test2 in SWITCH A , the result looks like this:
Name | fs-test2
Profile | external
Scheme | Digest
Realm | ipOfSwitchB:5080
username | FreeSWITCH
Password | no
From | <sip:FreeSWITCH#ipOfSwitchB:5080>
Contact | <sip:gw+fs-test2#103.62.152.227:5080;transport=udp;gw=fs-test2>
Exten | FreeSWITCH
To | sip:FreeSWITCH#ipOfSwitchB:5080
Proxy | sip:FreeSWITCH#ipOfSwitchB:5080
Status | UP
State | NOREG
After I've done all the changes , I run "reloadxml", "reload mod_sofia"
When I trigger a call(18881) from SWITCH A, there is a logs showing on SWITCH B, and it says :
sofia.c:10362 sofia/external/FreeSWITCH#ipOfSWITCHB:5080 receiving invite from ipOfSWITCHB:5080 version: 1.10.6 -release-18-1ff9d0a60e 64bit call-id: fd37ba89-54d1-123a-db86-080027337ad5,
and the moment the call ended from SWITCHA, the logs shows on SWITCHA as:
switch_core_state_machine.c:276 Dialplan [default] not found, skipping
switch_core_state_machine.c:312 No Route, Aborting
switch_core_state_machine.c:313 Hangup sofia/external/1000#ipOfSWITCHA [CS_ROUTING] [NO_ROUTE_DESTINATION]
there is also a log showing on SwitchB:
sofia.c:8641 Hangup sofia/external/18881 [CS_CONSUME_MEDIA] [NO_ROUTE_DESTINATION]
mod_dptools.c:3643 Originate Failed. Cause: NO_ROUTE_DESTINATION
switch_channel.c:4942 Hangup sofia/external/FreeSWITCH#ipOfSwitchB:5080 [CS_EXECUTE] [NO_ROUTE_DESTINATION]
Am I missing something?
Thanks in advance.
I think this is what happened when SIP INVITE send into your Switch-B:
reach extension "public-did", and transfer to extension "1000" of default context.
since you don't have extension "1000" in default context, so you got error "No Route"

Call variable in CURL Command

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.

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?

Escape quote character in ant file

I'm trying to retrieve a String defined in the class Core.java as follows:
public static final String PLATFORM_VERSION = "3.0.1_170518";
So I have created a test ant file (because I want to store it as an ant property to be used in compilation process).
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="get-core-version" name="test">
<target name="get-core-version">
<exec executable="bash"
outputproperty="coreVersionTemp"
failonerror="true">
<arg value="-c"/>
<arg value="cat ./Core.java | grep VERSION"/>
</exec>
<echo message=""ResultadoIntermedio": ${coreVersionTemp}"/>
<exec executable="bash"
outputproperty="coreVersion"
failonerror="true">
<arg value="-c"/>
<arg value="echo ${coreVersionTemp} | cut -d'\"' -f2"/>
</exec>
<echo message="Resultado: ${coreVersion}"/>
</target>
The ant code is split to locate exactly where is failing. The ant returns:
Buildfile: E:\git\test.xml
get-core-version:
[echo] "ResultadoIntermedio": public static final String PLATFORM_VERSION = "3.0.1_170518";
BUILD FAILED
E:\git\test.xml:14: exec returned: 1
Total time: 0 seconds
As you can see, the error is on 'cut' command. ResultadoIntermedio is correct.
If we execute the full command on bash, we have the expected result as well:
$ cat ./Core.java | grep VERSION | cut -d '"' -f2
3.0.1_170518
The problem is, I think, in the escape characters after the -d option of cut.
I have tried:
'"'
'\"'
\'\"\'
'"'
\'"\'
&apos;"&apos;
And some other combinations... how can I do this correctly?
Thank you very much.
Taking a workaround replacing '"' to ':' in the property with Javascript works fine, but is a little nasty solution.
<target name="get-core-version">
<exec executable="bash"
outputproperty="coreVersionTemp"
failonerror="true">
<arg value="-c"/>
<arg value="cat ./Core.java | grep VERSION"/>
</exec>
<echo message=""ResultadoIntermedio": ${coreVersionTemp}"/>
<!-- <propertyregex property="coreVersionTemp2" input="coreVersionTemp" regexp=""" replace=":" global="true"/> -->
<script language="javascript">
var temp = project.getProperty("coreVersionTemp");
project.setProperty("coreVersionTemp", temp.replaceAll("\"", ":"));
</script>
<echo message=""ResultadoIntermedio2": ${coreVersionTemp}"/>
<exec executable="bash"
outputproperty="coreVersion"
failonerror="true">
<arg value="-c"/>
<arg value="echo '${coreVersionTemp}' | cut -d: -f2"/>
</exec>
<echo message="Resultado: ${coreVersion}"/>
</target>
</project>

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