We changed our privacy policy. Read more.

Hot answers tagged

31

The awk snippet works for extracting the different parts, but you still need to know which section is the key / cert / chain. I needed to extract a specific section, and found this on the OpenSSL mailinglist: http://openssl.6102.n7.nabble.com/Convert-pem-to-crt-and-key-files-tp47681p47697.html # Extract key openssl pkey -in foo.pem -out foo-key.pem # ...


25

I almost deleted this question. D'oh! From man iptables: -L, --list [chain] List all rules in the selected chain.


22

The split command is available on most systems, and its invocation is likely easier to remember. If you have a file collection.pem that you want to split into individual-* files, use: split -p "-----BEGIN CERTIFICATE-----" collection.pem individual- If you don't have split, you could try csplit: csplit -s -z -f individual- collection.pem '/-----...


16

awk NF=NF RS= OFS=+ Result hi+there.hopefully+you+can+get+this+email+which+are+being+send+as+sms.


15

From https://www.gnu.org/software/gawk/manual/html_node/Numeric-Functions.html CAUTION: In most awk implementations, including gawk, rand() starts generating numbers from the same starting number, or seed, each time you run awk. Thus, a program generates the same results each time you run it. The numbers are random within one awk run but predictable from ...


12

There is operatingsystemmajrelease % facter operatingsystemmajrelease 6 If you have redhat-lsb-core package installed, facter will get as well the family of lsb-provided facts (which includes lsbmajdistrelease): % facter |grep ^lsb lsbdistcodename => Final lsbdistdescription => CentOS release 6.4 (Final) lsbdistid => CentOS lsbdistrelease => 6....


11

I would personally base64-encode the key, store it, then base64 decode it when you need it. Encode: echo "-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----" | openssl base64 | tr -d '\n' Decode: echo "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLSBteV9zdXBlcl9zZWNyZXRfcGFzc3dvcmQgLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0K" |...


10

A one-liner based on the /dev/stdin trick from ℝaphink's answer and man xargs → EXAMPLES: egrep '^[^#]' ~/.ssh/authorized_keys | xargs -n1 -I% bash -c 'ssh-keygen -l -f /dev/stdin <<<"%"'


9

Here's a portable way to show all key fingerprints for a given file, tested on Mac and Linux: #!/bin/bash fingerprint_keys() { if (( $# != 1 )); then echo "Usage: ${FUNCNAME} <authorized keys file>" >&2 return 1 fi local file="$1" if [ ! -r "$file" ]; then echo "${FUNCNAME}: File '${file}' does not ...


8

The most straightforward answer is to not try and utilize grep, sed, awk, etc for this - even though they will do it, you will most assuredly end up making assumptions and having to constantly shuffle the code. You could do it with the built-in json module in python, or a purpose-built CLI tool like jq. jq example: % cat ids.txt | jq '.data.id' "ocid1....


7

If you want to get a single certificate out of a multi-certificate PEM bundle, try: $ openssl crl2pkcs7 -nocrl -certfile INPUT.PEM | \ openssl pkcs7 -print_certs | \ awk '/subject.*CN=host.domain.com/,/END CERTIFICATE/' The first two openssl commands will process a PEM file and and spit it back out with pre-pended "subject:" and "issuer:" lines ...


6

You'll be farther ahead by extending fail2ban by dropping in a custom configuration file into its .d config directory. Do as little work as possible!


6

If the file name is always like in your example you can use something like: for x in *.log; do year=${x:9:4}; month=${x:13:2}; day=${x:15:2}; [[ ! -d "$year/$month/$day" ]] && mkdir -p "$year/$month/$day"; done This substring extraction is available in bash, not sure about other shells.


6

Use stat instead of du: #!/bin/bash for i in `grep -v ^# ~/cache_temp | grep -v "dovecot.index.cache"`; do [ -f "$i" ] && totalsize=$[totalsize + $(stat -c "%s" "$i")] done echo totalsize: $totalsize bytes


6

According to du(1), there is a -c option whose purpose is to produce the grand total. % du -chs * /etc/passwd 92K ABOUT-NLS 196K NEWS 12K README 48K THANKS 8,0K TODO 4,0K /etc/passwd 360K total


5

I'm guessing that you are trying to make some sort of decision based off of the install version. You can use regexes in your logic. So something like: case $operatingsystemrelease { /^6.*/: { //do 6.x stuff } /^5.*/: { //do 5.x stuff } } or if if is more your style: if $operatingsystemrelease =~ /^6.*/ { //do 6.x ...


5

If you need to use the file this snippet is hopefully efficient. xargs -a cache_file stat --format="%s" | paste -sd+ | bc -l The xargs is to prevent overflowing the argument limit but getting the max number of files into one invocation of stat each time.


4

If you are handling full chain certificates (i.e. the ones generated by letsencrypt / certbot etc), which are a concatenation of the certificate and the certificate authority chain, you can use bash string manipulation. For example: # content of /path/to/fullchain.pem -----BEGIN CERTIFICATE----- some long base64 string containing the certificate -----END ...


4

I would also prefer the sed-only solution given by @SvW, but to answer your question: The RS=" " comes too late. It is set after the first line is read. You should call it in a BEGIN block, for example, to have an effect also for the first line: mdadm --misc -sD | sed 's/ /=/' | awk 'BEGIN{RS=" "}{print $0}' Edit: Okay, @Thor was faster. :)


4

Use Perl, it's excellent for parsing text. #!/usr/bin/perl use strict; use warnings; my $filename = "adn_DF9D_20140515_0001.log"; my ( $year, $month, $day ) = ( $filename =~ m/adn_DF9D_(\d{4})(\d{2})(\d{2})/ ); print "$year $month $day\n"; Amend for differing file formats to taste. (The 'brackets' in the regular expression select pattern elements). ...


4

For those who might want to look at the mangle or nat tables with chains you can do so like iptables -L mychainname -t mangle Then it will show you that chain in the mangle table.


4

If you remove the "-h" flag from your "du" command, you'll get the raw byte sizes. You can then add them with the ((a += b)) syntax: a=0 for i in $(find . -type f -print0 | xargs -0 du -s | awk {'print $1'}) do ((a += i)) done echo $a The -print0 and -0 flags to find/xargs use null-terminated strings to preserve whitespace. EDIT: turns out I type slower ...


3

Use sed twice, the second time to replace spaces with newlines: mdadm --misc -sD | sed 's/ /=/'| sed 's/ /\n/g' or slightly optimized, only once but with two patterns: mdadm --misc -sD | sed 's/ /=/;s/ /\n/g'


3

I think you might find pidof much easier to use. pidof php 24293 19810 19467 You can use ~ to match a field and ^ to anchor the match to the beginning of the field. awk '$6 ~ /^something/ {print $2}'


3

The most idiomatic way to do it in awk is as follows: awk 'ORS=NR%2?FS:RS' file It outputs: Runtime Name: vmhba2:C0:T3:L14 Group State: active Runtime Name: vmhba3:C0:T0:L14 Group State: active unoptimized Runtime Name: vmhba2:C0:T1:L14 Group State: active unoptimized Runtime Name: vmhba3:C0:T3:L14 Group State: active Runtime Name: vmhba2:C0:T2:L14 Group ...


3

The reason this won't work is that the shell splits the command into two parts like this: screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt awk '{print $1}' >/var/www/html/filter/first.txt The screen runs cat on a separate pty. There is then no output on stdout to pipe to awk. If you want the pipe to be run under screen, you need to group it ...


3

Even if this can probably be solved by a sed command, it would be simpler to use the proper command to change yum configuration: yum install yum-utils yum-config-manager --enable remi-php56 And notice, this is explained in the Wizard instructions.


3

Try this: lsscsi 4 0 1 | sed -r '/\[4:0:1:(1|12|13)\].*/!d' Output will be: [4:0:1:1] /dev/sdab [4:0:1:12] /dev/sdo [4:0:1:13] /dev/sdp


3

IMHO Typically date/time manipulation works better when you treat a time string as time and not a text string. Use the formatting options of the date command to get the timestamp to display in your desired formatting and do something along the lines of: var1="Wed Apr 10 06:44:10 UTC 2019" date --date="$var1" +%Y,%m,%d and you can for instance also ...


3

Sure you can solve your issue with one-liner, but it isn't reusable. So I suggest try to use this quickly written script and try to figure out, what it does, to improve your skills. #!/bin/sh channel_id="499" old_source_id="igst0-iva" new_source_id="igst1-ven" if [ $# -ne 2 ]; then echo "usage: ${0} INPUT-FILE OUTPUT-FILE" exit 1 fi if [ ! -f ${1}...


Only top voted, non community-wiki answers of a minimum length are eligible