matlab system command with variable - matlab

i would like to do some process, that will block some IP that stores in variables. The syntax that i wrote:
[status4,cmdeks2] = system("sudo tail -1 /var/log/apache2/access.log | cut -d ' ' -f 1");
lm = system(['sudo iptables -A INPUT -s' cmdeks ' -j DROP '])
the cmdeks 2 itself is some IP:
192.168.88.10
But it return some error, that matlab output:
/bin/bash : line 1: -j: command not found
How do i put that cmdeks in the system syntax?

I guess it is because cmdeks2 contains a line feed code and the iptables command breaks by the line feed code.
Extracting only IP address via sscanf will work.
[status4,cmdeks2] = system("tail -1 ./access.log | cut -d ' ' -f 1");
cmdeks2 = sscanf(cmdeks2, '%s\n'); % cmdeks2 contains only IP address
lm = system(['sudo iptables -A INPUT -s ' cmdeks2 ' -j DROP '])
Also, you might have extra iptables rules by your previous trials, so please delete those rules by "iptables -D" command.

Related

Check number of active meetings in Big Blue Button from command line

I want to check how many active meetings there are on the BBB server at any one time from the command line. I have tried
$ bbb-conf --network
but not getting anywhere. I have also checked the number of active connections to port 80 and 443
$ netstat -anp | grep :443 | grep ESTABLISHED | wc -l
but I'm not sure if I can trust that figure.
I know I can use the isMeetingRunning call from the API but I'm just looking for command line.
Any ideas would be appreciated
The following bash script, which can be run from command line on the same machine as the BigBlueButton server, will process the response to the BBB API getMeetings call.
#!/bin/bash
APICallName="getMeetings"
APIQueryString=""
X=$( bbb-conf --secret | fgrep URL: )
APIEndPoint=${X##* }
Y=$( bbb-conf --secret | fgrep Secret: )
Secret=${Y##* }
S=$APICallName$APIQueryString$Secret
Checksum=$( echo -n $S | sha1sum | cut -f 1 -d ' ' )
if [[ "$APIQueryString" == "" ]]
then
URL="${APIEndPoint}api/$APICallName?checksum=$Checksum"
else
URL="${APIEndPoint}api/$APICallName?$APIQueryString&checksum=$Checksum"
fi
wget -q -O - "$URL" | grep -o '<meetingID>' | wc -w
Tested on a live BBB machine.
Note:
The APICallName and APIQueryString can be modified to provide interface to other BBB API calls. See https://docs.bigbluebutton.org/dev/api.html
The command-line sha1sum will output a different result if a newline is appended to its input. This is the reason echo -n is used instead of echo.
In the last line, the script processes the XML output from the API call in a very naïve way, simply counting the number of occurences of the <meetingID> tag. More elaborate processing would probably require parsing the XML.

Find and replace in UNIX

I'm having the following string in a file called test.txt,
test.log test1.log test2.log
I want to replace it with
test.log -A test1.log -A test2.log
I tried:
sed -i 's/.log/.log -A/g' test.txt
But the output is
test.log -A test1.log -A test2.log -A
I don't want that to be appended in the last file. Can someone help me on this?
If the arguments are separated by space and final argument in the line doesn't have spaces after it, you could use this:
$ cat ip.txt
test.log test1.log test2.log
$ sed 's/\.log /&-A /g' ip.txt
test.log -A test1.log -A test2.log
since . is a metacharacter, you have to use \. to match it literally
& in replacement section represents entire matched portion in search section
You could also use awk here, better suited for field processing and added advantage of stripping away whitespaces at start/end of line
$ awk -v OFS=' -A ' '/\.log/{$1=$1} 1' ip.txt
test.log -A test1.log -A test2.log
default input field separator(FS) is one or more contiguous whitespace, so no need to set that
-v OFS=' -A ' set space followed by -A and space as output field separator(OFS)
/\.log/ if line contains .log
$1=$1 re-build input record, so that input FS will be replaced by OFS
1 idiomatic way to print input record
note that this solution won't change a line if it doesn't contain .log

Empty output in crontab on CentOS

I wrote a simple script to get IPv4 addresses, as follows:
#!/bin/bash
ip -4 addr | grep inet | awk -F '[ \t]+|/' '{print $3}' | grep -v ^127.0.0 | tr '\n' ' '
It works well when I run it from the bash. Now I created a crontab entry and am hoping to output the ip address to a tmp file:
* * * * * /root/ipv4.sh >> /tmp/tmp.txt
There is nothing written to /tmp/tmp.txt, though the /tmp/tmp.txt is touched every minute. It happens only on Cent OS, while on Ubuntu it works as expected.
Problem solved by changing ip to /sbin/ip in the script.

Ctrl+Z Character and EOF Issues With Pipes

I have a huge file provided by a third party, which appears to have been generated in a Windows/DOS-like environment. The last line of the file contains a ^Z character. I noticed this when I looked at the processed file and the last line contained a ^Z. I added some logic to skip this line from the input and it was working fine until I changed my code to take the input from stdin as opposed to a file.
Here is a simpler illustration of this issue. When I do a line count on a single file stream with and without ^Z skipping, it reports the correct values:
unzip -j -p -qq file1.zip | perl -nle 'print' | wc -l
3451
unzip -j -p -qq file2.zip | perl -nle 'print' | wc -l
3451
unzip -j -p -qq file1.zip | perl -nle 'next if /^\cZ/; print' | wc -l
3450
unzip -j -p -qq file2.zip | perl -nle 'next if /^\cZ/; print' | wc -l
3450
Now when I try to process both files at once, I lose one record. I am guessing this is something to do with the ^Z character but I cannot figure out what I can do about it:
unzip -j -p -qq '*.zip' | perl -nle 'print' | wc -l
6901 ## this should have been 6902
unzip -j -p -qq '*.zip' | perl -nle 'next if /^\cZ/; print' | wc -l
6899 ## this should have been 6900
These files are huge (each 20+GB) and they are to be read in groups of 3-6 files so I wanted to avoid processing them one by one and then concatenate later. Any thoughts on how to avoid the ^Z character without running into the above issue?
I am on a Linux machine. Btw, opening the file in vim does not display the last record (i.e., ^Z) and setting set ff=unix did not change this either. So vim reports 3450 lines for the single unzipped file and 6900 for the combined unzipped files.
Thanks!
Since the ^Z isn't followed by a line ending, unzip is producing
file1:1
file1:2
file1:3
^Zfile2:1
file2:2
file2:3
^Z
so you delete the first line of the second file. You could simply remove the ^Z instead of the entire line.
perl -pe's/^\cZ//'
That said, unzip -a is designed for exactly this situation. Not only will it strip the ^Z for you, it will also fix the line endings if necessary.
$ unzip -j -p -qq z.zip a.txt | od -c
0000000 a b c \r \n d e f \r \n 032
0000013
$ unzip -j -p -qq z.zip b.txt | od -c
0000000 g h i \r \n j k l \r \n 032
0000013
$ unzip -j -p -qq z.zip | od -c
0000000 a b c \r \n d e f \r \n 032 g h i \r \n
0000020 j k l \r \n 032
0000026
$ unzip -j -p -qq -a z.zip | od -c
0000000 a b c \n d e f \n g h i \n j k l \n
0000020

Running tshark in perl script

I am trying to run tshark in a perl script, simply by doing the following -
my $filter = "port 68 or 67";
my $capture = "tshark -i eth0 -f $filter -a duration:120 -w pcapture.pcap&";
system($capture);
This code is not starting the tshark process. Any changes recommended?
Assuming port 68 or 67 is a legal value for -f, you need quotes to treat it as one entity (a value which contains whitespace):
my $filter = "port 68 or 67";
my $capture = "tshark -i eth0 -f '$filter' -a duration:120 -w pcapture.pcap&";
system($capture);