find and replace with variables - variables

I'm trying to find and replace with variables, but it doesn't work.
Here is the code. I need to append -C -w 10% -c 5% -p /u0 to append to the end of a matching line. I do not know how to suppress the (-) Any ideas? Thank you.
OLD=$(command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e)
NEW=$(command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e -C -w 10% -c 5% -p /u0)
sed -i "s/$OLD/$NEW/" /home/scripts/nrpe.cfg

Try this (assumes bash):
OLD='command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e'
NEW='command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p / -p /var -p /tmp -p /home -p /boot -p /usr -A -e -C -w 10% -c 5% -p /u0'
oldEscaped=$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$OLD")
newEscaped=$(sed 's/[\\&/]/\\&/g' <<<"$NEW")
sed -i "s/$oldEscaped/$newEscaped/" /home/scripts/nrpe.cfg
Your first problem was that you mistook $(...) for a string-quoting mechanism (it is not; it's used for command substitution (executing the enclosed command and replacing the construct with the command's output)).
To assign literal strings, simply use single quotes as above.
Your second problem was that you can't blindly pass strings to sed's s (string-substitution) command, because certain characters have special meaning to sed, so to use them literally they have to be escaped - the most obvious problem being the / instances in the strings, which get mistaken for the delimiters of the s/.../.../ command.
Therefore, 2 auxiliary sed commands are used to perform the requisite escaping:
sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$OLD" escapes the old string so that none of its characters can be mistaken for the regex delimiter or special regular-expression characters.
sed 's/[\\&/]/\\&/g' <<<"$NEW" escapes the new string so that none of its characters can be mistaken for the regex delimiter or backreferences (such as &, or \1).
Finally, note that it's better not to use all-uppercase shell variable names such as $OLD, so as to avoid conflicts with environment variables.

Related

Passing gitlab variables in sshpass script

Is there a way to add gitlab variables to the command ?
eg: variables: ARTIFACTORY_ADDRESS: "a.com"
script:
sshpass -p "password" ssh -o "StrictHostKeyChecking=no" user#SERVER 'echo $ARTIFACTORY_ADDRESS'
Currently its not taking the value from the variable and printing $ARTIFACTORY_ADDRESS in the console. I want the value to be printed in the console
Check first if using double-quotes would help enabling variable substitution:
sshpass -p "password" ssh -o "StrictHostKeyChecking=no" user#SERVER \
"echo $ARTIFACTORY_ADDRESS"
^^^ ^^^

Escaping karate.fork Commands

I am trying to run the following command in karate using karate.fork
ssh -o ProxyCommand="ssh -W %h:%p -i ~/.ssh/id_rsa root#myjumphost" -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no -o PasswordAuthentication=no root#finaldest echo test
I have broken this up into an array to pass to karate.fork like so:
[
ssh,
-o,
ProxyCommand="ssh -W %h:%p -i ~/.ssh/id_rsa root#myjumphost",
-i,
~/.ssh/id_rsa,
-o,
StrictHostKeyChecking=no,
-o,
PasswordAuthentication=no,
root#finaldest,
echo test
]
Then run the command like so:
* karate.fork(args) where args is the array mentioned above
The command works when I paste it into the terminal and run it manually, however when run with karate.fork I get
zsh:1: no such file or directory: ssh -W finaldest:22 -I ~/.ssh/id_rsa root#myjumphost
kex_exchange_identification: Connection closed by remote host
I have tried adding a few backslashes before the " in the ProxyCommand but no amount of back slashes fixes this issue. I think I am misunderstanding what karate.fork is doing to run the command, is there some internal parsing or manipulating of the given input? I was able to get this command to work when I used useShell: true however this option breaks other tests for me so I would really like to avoid it.
I had to remove the double quotes, seems like they didn't play well with karate.fork and the command still runs without them
[
ssh,
-o,
ProxyCommand=ssh -W %h:%p -i ~/.ssh/id_rsa root#myjumphost,
-i,
~/.ssh/id_rsa,
-o,
StrictHostKeyChecking=no,
-o,
PasswordAuthentication=no,
root#finaldest,
echo test
]

tcpdump with -w -C -G and -z options

I'm trying to take continuous traces which are written to files that are limited by both duration (-G option) and size (-C option). The files are automatically named with the -w option, and finally the files are compressed with the -z gzip option. Altogether what I have is:
tcpdump -i eth0 -w /home/me/pcaps/MyTrace_%Y-%m-%d_%H%M%S.pcap -s 0 -C 100 -G 3600 -Z root -z gzip &
The problem is that with the -C option, the current file count is appended onto the name, so I wind up with files ending in: .pcap2.gz .pcap3.gz .pcap4.gz, etc. I would much prefer to have them end as: _2.pcap.gz _3.pcap.gz _4.pcap.gz, etc.
But if I remove .pcap from the -w option, I wind up with 2.gz 3.gz 4.gz
This could work if I could include options in the "-z" command like -z "gzip -S .pcap.gz" so that gzip itself appends the .pcap or if I could use an alias like pcap_gzip="gzip -S .pcap.gz" and then -z pcap_gzip, but neither option seems to be working, the latter producing this error: compress_savefile:execlp(gzip -S pcap.gz, /home/me/pcaps/MyTrace_2018-08-07_105308_27): No such file or directory
I encountered the same problem today, In CentOS6. I found your problem, but the answer did not work to me.
In fact, it only needs to be adjusted slightly, that is, the absolute path of the saved file name and the name of the script to be executed is written, for example
tcpdump -i em1 ... -s 0 -G 10 -w '/home/Svr01_std_%Y%m%d_%H%M%S.pcap' -Z root -z /home/pcapup2arcive.sh
I found out that although the alias doesn't work, I was able to put the same commands in a script and invoke the script via tcpdump -z.
pcap_gzip.sh:
#!/bin/bash
gzip -S .pcap.gz "$#"
Then:
tcpdump -i eth0 -w /home/me/pcaps/MyTrace_%Y-%m-%d_%H%M%S -s 0 -C 100 -G 3600 -Z root -z pcap_gzip.sh &

remote ssh execution : Single Quotes inside double quotes

All,
I have not been able to escape single quotes while writing a file to a remote node.
ssh -i demo.pem -t ec2-user#10.10.10.10 'echo '\''{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}'\''| sudo tee /etc/key.json'
The output I get is as follows.
{"watches": [{"type": "key","key": "test","handler": "neon -e sudo /opt/watch_handler.sh"}]}
I would like the output to have single quotes around 'sudo /opt/watch_handler.sh'
{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}
'\' is not working.
Could you please help.
Thanks,
Getting multiple levels of quoting correct is troublesome and error-prone. Consider alternative solutions, such as:
cat <<EOF | ssh -i demo.pem -t ec2-user#10.10.10.10 sudo tee /etc/key.json
{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}
EOF
I like using cat because it does not require any escaping to work. However, you can also generate the string locally using echo instead of cat as long as you escape the double quotes in your JSON expression:
echo "{\"watches\": [{\"type\": \"key\",\"key\": \"test\",\"handler\": \"neon -e 'sudo /opt/watch_handler.sh'\"}]}" | sudo tee /etc/key.json

Using wget 1.12 centos 6 to batch download and rename output files

using file wget
wget -c --load-cookies cookies.txt http://www.example.com/file
works fine
wget -c --load-cookies cookies.txt http://www.example.com/file.mpg -O filename_to_save_as.mpg
when I use
wget -c --load-cookies cookies.txt -i /dir/inputfile.txt
to pass urls from a text file it wget it works as expected. Is there any way to pass a url from a text file and still rename the out put file as in example 2 above. I have tried passing the -O option with an argument but wget tell me "invalid URL http://site.com/file.mpg -O new_name.mpg: scheme missing"
also I have tried escaping after the url, quotes and formatting in such a way as
url = "http://foo.bar/file.mpg" -O new_name.mpg
is there any way to use an input file and still change the output file name using wget?
if not would a shell script be more appropriate? If so how should it be written?
I don't think that wget supports it, but it's possible to do with a small shell script.
First, create an input file like this (inputfile.txt):
http://www.example.com/file1.mpg filename_to_save_as1.mpg
http://www.example.com/file2.mpg filename_to_save_as2.mpg
http://www.example.com/file3.mpg filename_to_save_as3.mpg
The url and the filename are separated by a tab character.
Then use this bash script (wget2.sh):
#!/bin/bash
while read line
do
URL=$(echo "$line" | cut -f 1 )
FILENAME=$(echo "$line" | cut -f 2 )
echo wget -c --load-cookies cookies.txt "$URL" -O "$FILENAME"
done
with this command:
echo input.txt | wget2.sh
A more simple solution is to write a shell script which contains the wget command for every file:
#!/bin/bash
wget -c --load-cookies cookies.txt http://www.example.com/file.mpg1 -O filename_to_save_as1.mpg
wget -c --load-cookies cookies.txt http://www.example.com/file.mpg2 -O filename_to_save_as2.mpg
wget -c --load-cookies cookies.txt http://www.example.com/file.mpg3 -O filename_to_save_as3.mpg