I ran the below-mentioned jq command and my putty session became inactive. however, I can still see the process running using the "top" command.
Does jq --stream run in background by default?
jq -cn --stream '
fromstream(1|truncate_stream(inputs | select(.[0][0] == "userActivities") | del(.[0][0])))
| select(.localDate[0:7] == "2018-10")
' 2018-10-01T21_45_56Z_triplem-baas_data.json > October_2018_triplem_events.json
Does jq --stream run in background by default?
No.
The --stream option is usually only used for very large JSON texts, so if that is the case here, then it might take a long while for the job to finish. If you want to verify that progress is being made, consider adding one or more debug statements: each debug is like . but copies its input value to STDERR before passing the value along.
Sometimes it pays to be a bit devious with debug, as illustrated in this variant of your program:
jq -cn --stream '
fromstream(1|truncate_stream(inputs | select(.[0][0] == "userActivities") | del(.[0][0])))
| (.localDate|debug) as $debug
| select(.localDate[0:7] == "2018-10")
' 2018-10-01T21_45_56Z_triplem-baas_data.json > October_2018_triplem_events.json
Related
So, something so simple, how much ram is installed in the current machine? I run a pxe image built in buildroot to grab system specifications from systems on a network boot. But, one thing seems to stick out to me. How do you effectively and reliably count the ram on every possible system.
I give you the worst code ever made, it's 6 years old and I am absolutely embarrassed by it.
ramtotal=0
ramsize=1
while test $ramsize -le 10000; do
ramcount=`dmidecode --type memory | grep -v Enabled | grep -v Installed | grep -v Maximum | grep "Size:" | grep "MB" | grep -c " $ramsize "`
ramup=$(( ramsize * ramcount ))
ramtotal=$(( ramtotal + ramup ))
ramsize=$(( ramsize * 2 ))
done
Well, may my code live long enough to be capable of counting ram chips with a size of 2^10000. Future proof ftw. And that's the thing, the code literally just worked, and so there was never any reason to make it disappear.
Today, I am trying a new code which worked fine on my Ubuntu Server, but not with busybox.
ramtotal=`dmidecode --type memory | grep -v Enabled | grep -v Installed | grep -v Maximum | grep "Size:" | grep "MB" | grep -o -P '(?<=\:\ ).*(?=\ MB)' | awk '{s+=$1} END {print s}'`
ramtotal=`dmidecode -t memory | grep "Size:" | awk '/Size: ([0-9]+) bytes|([kKMGTPEZ]B)/ {if($3 ~ /GB/) { size+=$2*1024 } else if($3 ~ /MB/) { size+=$2 } } END { print size }'`
So, it's been a long time since I originally posted. And I guess just to be consistent, I wanted to come back and update this, seeing as a change in the source code of dmidecode essentially breaks what I had previously added. Essentially for some reason dmidecode decided that this field could be MB or GB. (and perhaps something even bigger, though I didn't bother to research how forward thinking they decided to be).
I have an nmap output looking like this
Nmap scan report for 10.90.108.82
Host is up (0.16s latency).
PORT STATE SERVICE
80/tcp open http
|_http-title: Did not follow redirect to https://10.90.108.82/view/login.html
I would like the output to be like
10.90.108.82 http-title: Did not follow redirect to https://10.90.108.82/view/login.html
How can it be done using grep or any other means?
You can use the following nmap.sh script like that:
<nmap_command> | ./nmap.sh
nmap.sh:
#!/usr/bin/env sh
var="$(cat /dev/stdin)"
file=$(mktemp)
echo "$var" > "$file"
ip_address=$(head -1 "$file" | rev | cut -d ' ' -f1 | rev)
last_line=$(tail -1 "$file" | sed -E "s,^\|_, ,")
printf "%s%s\n" "$ip_address" "$last_line"
rm "$file"
If you do not mind using a programming language, check out this code snippet with Python:
import nmapthon as nm
scanner = nm.NmapScanner('10.90.108.82', ports=[80], arguments='-sS -sV --script http-title')
scanner.run()
if '10.90.108.82' in scanner.scanned_hosts(): # Check if host responded
serv = scanner.service('10.90.108.82', 'tcp', 80)
if serv is not None: # Check if service was identified
print(serv['http-title'])
Do not forget to execute pip3 install nmapthon.
I am the author of the library, feel free to have a look here
Looks like you want an [nmap scan] output to be edited and displayed as you wish. Try bash scripting, code a bash script and run it.
Here's an link to a video where you might find an answer to your problem:
https://youtu.be/lZAoFs75_cs
Watch the video from the Time Stamp 1:27:17 where the creator briefly describes how to cut-short an output and display it as we wish.
If you require, I could code an bash script to execute an cut-shorted version of the output given by an nmap scan.
This question already has an answer here:
why is a double-quoted awk command substitution failing in csh
(1 answer)
Closed 4 years ago.
I get an error Illegal variable name when I use this line of code:
set users = "` last | sort | tr -s '\t' ' ' | grep '[0,2][0-4]:[0-5][0-9] -' | grep -v '^$' | grep -v '[2][0-1]:[0-5][0-9] -' `"
But it works fine when I use this code:
set users = "` last | sort | tr -s '\t' ' ' | grep '[0,2][0-4]:[0-5][0-9] -' | grep -v '[2][0-1]:[0-5][0-9] -' `"
The code should store people who logged in between 22:00 and 05:00 (excluding 05:00) into a variable named users. It should also remove any empty lines which are in the output. This is what I'm trying to do in the first code, but it gives me the aforementioned error.
I don't know how to explain it, but it is one of these typical CSH pitfalls.
A <dollar> ($) between <double-quotes> (") (independently if they are in between <back-ticks> (`) and <single-quotes> (') are always concidered to be variable names. So if the word following the <dollar> is not a valid variable name, the thing starts to complain. Example:
$ grep "foo$" file.txt
Illegal variable name.
This is exactly what your problem is. You wrote something similar too
$ set var = "`grep -v '^$' file.txt`"
and even though the <dollar> is between <single-quotes> which are in-between <back-ticks> for command substitution which is again between <double-quotes> to retain the blanks and tabs of the command substitution, it just does not matter! There is no hope! You used <double-quotes> with all good intentions, but it just blew up in your face! Resistance is futile, your <dollar> will be assimilated to resemble a variable, even when it does not! CSH just does not care! You just want to cry! You cannot even escape it!
If you make use of last from util-linux, you might be interested in the flags --since and --until (see here and here). Otherwise you might use the following command line:
set users="`last | awk '/(2[2-3]|0[0-4]):.. [-s]/'`"
This will match all lines where the user logged in between 22 en 05 (excl) and is potentially still logged in.
As a general note, I would suggest switching from CSH to BASH for many reasons. Some of them are mentioned here and here.
I'm new with Geneos and would like to know how to show the output of our existing script which is previously used in Nagios. We're planning to use the toolkit plugin and not sure what will be the commands to use to be able to see result in active console.
Requirement - check the log if there is session timeout and it will alert OK if grep is equal 20 then Warning alert if less or greater than 20.
Output in Geneos:
column_title - TIMEOUT CHECK, STATUS
row_result - THE_FILE, OK: Session Timeout is 20
Here's our sample script:
#!/bin/ksh
OK=0
WARNING=1
CRITICAL=2
THE_FILE=/target/directory/web.txt
TIMEOUT=`grep "<session-timeout>" $THE_FILE | awk -F'>' '{print $2}' | awk -F'>' '{print $1}'
if [$TIMEOUT -eq 20 ]; then
echo "OK: Session Timeout is $TIMEOUT"
exit $OK
else
echo "WARNING: Session Timeout is $TIMEOUT"
exit $WARNING
fi
Thanks!
You can use the same script (adding the header) and adding it to Geneos. But I highly recommend you to use a FKM sampler, and check this log file using Geneos directly.
Hope this helps you.
I'm piping some output of a command to egrep, which I'm using to make sure a particular failure string doesn't appear in.
The command itself, unfortunately, won't return a proper non-zero exit status on failure, that's why I'm doing this.
command | egrep -i -v "badpattern"
This works as far as giving me the exit code I want (1 if badpattern appears in the output, 0 otherwise), BUT, it'll only output lines that don't match the pattern (as the -v switch was designed to do). For my needs, those lines are the most interesting lines.
Is there a way to have grep just blindly pass through all lines it gets as input, and just give me the exit code as appropriate?
If not, I was thinking I could just use perl -ne "print; exit 1 if /badpattern/". I use -n rather than -p because -p won't print the offending line (since it prints after running the one-liner). So, I use -n and call print myself, which at least gives me the first offending line, but then output (and execution) stops there, so I'd have to do something like
perl -e '$code = 0; while (<>) { print; $code = 1 if /badpattern/; } exit $code'
which does the whole deal, but is a bit much, is there a simple command line switch for grep that will just do what I'm looking for?
Actually, your perl idea is not bad. Try:
perl -pe 'END { exit $status } $status=1 if /badpattern/;'
I bet this is at least as fast as the other options being suggested.
$ tee /dev/tty < ~/.bashrc | grep -q spam && echo spam || echo no spam
How about doing a redirect to /dev/null, hence removing all lines, but you still get the exit code?
$ grep spam .bashrc > /dev/null
$ echo $?
1
$ grep alias .bashrc > /dev/null
$ echo $?
0
Or you can simply use the -q switch
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)