たいちょーの雑記

ぼくが3日に一度くらい雑記をかくところ

シェル芸160ノック29

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題156

$ echo gihyo.jp github.com gitlab.com wikipedia.org | fmt -1 | while read fqdn; do [[ "$(nslookup $fqdn | awk '/Non-authoritative answer:/,0' | grep -Pe 'Address: \d+\.\d+\.\d+' | wc -l)" -gt 1 ]] && echo $fqdn; done
gihyo.jp

問題157

$ sudo tcpdump -x -c 1 icmp 2>/dev/null | grep 0x00 | sel --remove-empty 2: | fmt -1 | awk '/0800/,0' | awk 'NR>=4'

問題158

$ ping gihyo.jp | grep -m1 -oP "(?<=ttl=)\d+"
56
$ sudo traceroute -I gihyo.jp | awk 'END{print NR-1}'
10

問題159

$ cat qdata/159/sites.txt | while read D;do whois $D | grep "No entries" && echo $D >> fake_sites.txt; done

問題160

$ echo | openssl s_client -showcerts -connect example.com:443 |& openssl x509 -noout -dates |& grep not
notBefore=Dec 10 00:00:00 2021 GMT
notAfter=Dec  9 23:59:59 2022 GMT

この有効期限を端末で表示出来たら便利そうですね。

ホンマか?

完走した完走

完走した感想です。 めちゃめちゃいろいろ勉強になりました。普段シェル芸勉強会でやるような問題はさっと解けることもあるのですが、ファイルシステムやネットワークの話とかになるとそうもいきませんでした。そういった部分はまたもう一度解こうと思います。とても楽しかったです。ありがとうございました。

シェル芸160ノック28

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題151

$ curl http://file.ueda.tech/eki/l/11302.xml > a
$ cat a | grep -Pe "lon|lat|name" | sd '<[^>]+>' '' | awk '{print $1}' | xargs -n3 | while read D E F; do echo $D $(curl "https://cyberjapandata2.gsi.go.jp/general/dem/scripts/getelevation.php?lon=$E&lat=$F" | jq -r '.elevation'); sleep 1s; done

問題152

$ while true; do command wget localhost:8000/index.html  |& grep 200 &> /dev/null && echo Success || echo Warning; sleep 1s; done
Success
Success
Success
...

問題153

$ (echo -e "HEAD / HTTP/1.1\nHost: www.google.co.jp\n\n";sleep 1s) | telnet www.google.co.jp 80

問題154

$ echo wget curl ping dig nslookup | fmt -1 | while read CMD; do sudo strace -f $CMD google.com |& grep /etc/hosts && echo $CMD; done

問題155

$ echo -n test > index.html&&python3 -m http.server
$ curl localhost:8000 -v
*   Trying 127.0.0.1:8000...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/3.9.0
< Date: Tue, 21 Dec 2021 12:52:52 GMT
< Content-type: text/html
< Content-Length: 4
< Last-Modified: Tue, 21 Dec 2021 12:52:51 GMT
<
* Closing connection 0
test

$ (echo -e "HTTP/1.1 200 OK\nContent-Length: 5";echo -en "\ntest") | nc -N -l 8080

シェル芸160ノック27

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題146

$ git branch --no-merged=main -r | xargs -n1 -I@ zsh -c "git log -1 --since $(date -d '30 days ago' +%F) @ | grep . &> /dev/null || {echo branch @; git log @ -1 | head -n3}"  | sel --remove-empty 2: | paste - - - - -d ' '

問題147

$ cat o | grep -P "[\p{Hiragana}\p{Katakana}\p{Han}]+" | sd '<[^>]+>' '' | sed '/^$/d' | mecab | sel -gd\\s+ 1 | grep -v EOS | sort | uniq -c | sort -rn | head
     8988 シェル
     85747269665143 勉強
     42

問題148

$ grep line_cd o -A1 | sd '<[^>]+>' '' | awk '{print $1}' | grep -v -- -- | paste - -  | grep 京急本線
27001   京急本線
$ http http://file.ueda.tech/eki/l/27001.xml > o
$ cat o | grep station_name | sd '<[^>]+>' '' | awk '{print $1}'
泉岳寺
品川
北品川
新馬場
青物横丁
鮫洲
立会川
大森海岸
平和島
大森町
梅屋敷
京急蒲田
雑色
六郷土手
京急川崎
...

問題149

$ curl wttr.in/Shizuoka
Weather report: Shizuoka

      \   /     Clear
       .-.      +7(5) °C
    ― (   ) ―   ↘ 10 km/h
       `-10 km
      /   \     0.0 mm
...

問題150

$ http https://gihyo.jp/magazine/SD/backnumber > o
$ cat o | grep 品切 -B4 | nkf -Lu | sd '<[^>]+>' '' | sed '/^$/d' | grep -v -- -- | paste - - -  | sort -u
Software Design 202010月号    2020918日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 202011月号    20201017日発売      定価1,342円(本体1,220円+税10%)[品切]
Software Design 202012月号    20201118日発売      定価1,342円(本体1,220円+税10%)[品切]
Software Design 20206月号     2020518日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20207月号     2020618日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20208月号     2020718日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20209月号     2020818日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 202110月号    2021918日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 202111月号    20211018日発売      定価1,342円(本体1,220円+税10%)[品切]
Software Design 202112月号    20211118日発売      定価1,342円(本体1,220円+税10%)[品切]
Software Design 20211月号     20201218日発売      定価1,342円(本体1,220円+税10%)[品切]
Software Design 20212月号     2021118日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20213月号     2021218日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20214月号     2021318日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20215月号     2021416日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20216月号     2021518日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20217月号     2021617日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20218月号     2021716日発売       定価1,342円(本体1,220円+税10%)[品切]
Software Design 20219月号     2021818日発売       定価1,342円(本体1,220円+税10%)[品切]

$ dateutils.dseq --format "%Y/%m https://gihyo.jp/magazine/SD/archive/%Y/%Y%m"  2020-01-01 1mo 2020-12-01 | while read YM URL; do curl "$URL" | nkf -Lu | grep ございません &> /dev/null && echo $YM;done

シェル芸160ノック26

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題141

$ yes | awk '{print "echo",NR",""$(shuf -n5 -re {a..z}),$(shuf -n10 -re {a..z}),$(shuf -n5 -re {a..z})@mail.com"}' | head -n1000 |bash | tr -d ' ' | head
1,hyoah,pgdawiluzg,xwrui@mail.com
2,xbabm,gnmbcoxsri,ixpzm@mail.com
3,jyojm,rdjdcqqcus,hwrpa@mail.com
4,ehzdr,ihdnpkjvzi,gknmw@mail.com
5,jdxht,hnyxucnbcw,rvvvi@mail.com
6,bypgy,brjilztgyc,pnqwy@mail.com
7,foxwp,aymknffafy,mvclb@mail.com
8,dlwte,umqxtwcdal,uwoth@mail.com
9,wbhkw,cxrtjswfmt,bxsrq@mail.com
10,alkvf,bpdwkemdsh,phqcv@mail.com

問題142

$ echo blog.ueda | command xxd -b -c1 | sel 2 | tr -d \\n | ocs '{for(var i=0;i<F0.Length;i++)println(F0)}' | awk '{$NR=!$NR;print}' FS='' OFS='' | perl -lne 'print pack("B*", $_)' | grep -P '^[a-z\.]+$'
rlog.ueda
jlog.ueda
flog.ueda
clog.ueda
bdog.ueda
bhog.ueda
bnog.ueda
bmog.ueda
blgg.ueda
...

問題143

$ git status  --short | awk '$1=="M"&&$2~/B/{print $2}'  | xargs git add

問題144

$ fd . ./img ./css --type=f --exec git checkout "HEAD^" {}
Updated 1 path from c335175
Updated 1 path from c335175
Updated 1 path from c335175
Updated 1 path from c335175

問題145

$ git log --date=unix | grep Date: | awk '{if(b)print $2,b;b=$2}' | awk '{print ($2-$1)/3600}'
5.87694
0.0141667
232.218
47.4703
31.0544
0.0580556
16.2336
1181.22
1345.62
1581.95
...

シェル芸160ノック25

9月末に発売されたシェル芸本の続きです。組み合わせの列挙系は楽しいですね。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題132

$ cat ./qdata/132/httpd-access.log | awk '$(NF-1)==500' | while read L; do mail -s "500があったよ" こ こにメールアドレス <<< $L; done

問題133

$ echo "@reboot sleep 180s && shutdown -h now" | sudo crontab

問題134

$ grep -vnPe "^( {4})*[^ ]|^$" qdata/134/hoge.py
5:     print(i+1)

$ autopep8 qdata/134/hoge.py --select E11 --diff
--- original/qdata/134/hoge.py
+++ fixed/qdata/134/hoge.py
@@ -2,5 +2,5 @@

 for i in range(1,10):
     print(i)
-     print(i+1)
+    print(i+1)
     print(i+2)

autopep8解はもうちょっと成形してやる方が題意通りだけど、diffの方がいいかなって

問題135

cat qdata/135/sample.lisp | grep defun | ocs "F0.Count(c=>c=='(')!=F0.Count(c=>c==')'){println(F[2])}"
fib
sum1

問題136

$ cat qdata/136/somecode.c | awk '/^#/{h=h$0"\n"}/void a/,/^\}/{a=a$0"\n"}/int b/,/^\}/{b=b$0"\n"}/main/,/^}/{m=m$0"\n"}END{print h;print a;print b;print m}'
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void a()
{
        int i = 0, j = b();
        for (; i < j; i++){
                puts("a");
        }
}

int b()
{
        return rand()%10;
}

int main(int argc, char const *argv[])
{
        srand(time(NULL));
        a();
        return 0;
}

問題137

$ clang-format ./qdata/137/fib.c
#include <stdio.h>

int fib(int n) {
  if (n <= 1) {
    return n;
  }
  return fib(n - 1) + fib(n - 2);
}

int main(void) {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d\n", fib(i));
  }
  return 0;
}

問題138

$ cat os.csv|tr , \\n| join -j9 - <(cat service.csv|tr , \\n) | join -j9 - <(cat browser.csv|tr , \\n)| awk '!($1!="Windows"&&$3=="IE")&&!($1!="macOS"&&$3=="Safari")'
 Windows ServiceA IE
 Windows ServiceA Chrome
 Windows ServiceA FireFox
 Windows ServiceB IE
 Windows ServiceB Chrome
 Windows ServiceB FireFox
 Windows ServiceC IE
 Windows ServiceC Chrome
 Windows ServiceC FireFox
 macOS ServiceA Chrome
 macOS ServiceA FireFox
 macOS ServiceA Safari
 macOS ServiceB Chrome
 macOS ServiceB FireFox
 macOS ServiceB Safari
 macOS ServiceC Chrome
 macOS ServiceC FireFox
 macOS ServiceC Safari
 Linux ServiceA Chrome
 Linux ServiceA FireFox
 Linux ServiceB Chrome
 Linux ServiceB FireFox
 Linux ServiceC Chrome
 Linux ServiceC FireFox

問題139

$ echo https://{,{,{,cc.}bb.}aa.}example.com/A{/B{/C,},} | fmt -1
https://example.com/A/B/C
https://example.com/A/B
https://example.com/A
https://aa.example.com/A/B/C
https://aa.example.com/A/B
https://aa.example.com/A
https://bb.aa.example.com/A/B/C
https://bb.aa.example.com/A/B
https://bb.aa.example.com/A
https://cc.bb.aa.example.com/A/B/C
https://cc.bb.aa.example.com/A/B
https://cc.bb.aa.example.com/A

一般解じゃないのでダメかな?

問題140

$ mkdir tmp
$ cp ./dir.tar.gz ./tmp/
$ cd ./tmp
$ tar xvfz ./dir.tar.gz
$ rm ./dir.tar.gz
$ shuf -i 0-255 -rn 400 | paste - - - - -d. | paste - <(dateutils.dseq --format "- - [%Y/%m/%d %H:%M:%S +0900] \"GET" 2021-01-01T00:00:00 1s 2021-12-31T23:59:59 | shuf -n 100 | sort) | paste -d ' ' - <(fd . ./ --exec echo /{}|shuf -rn100) | sed 's@$@ HTTP/1.1" 200@' | paste -d ' ' - <(shuf -i 100-1000 -rn100)
225.20.241.13   - - [2021/01/09 05:13:30 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 272
208.207.0.117   - - [2021/01/13 12:36:05 +0900] "GET /product.php HTTP/1.1" 200 593
14.11.149.123   - - [2021/01/19 04:56:24 +0900] "GET /cart.php HTTP/1.1" 200 153
242.122.183.51  - - [2021/01/21 16:38:16 +0900] "GET /search.php HTTP/1.1" 200 334
220.85.189.142  - - [2021/01/24 16:01:08 +0900] "GET /js/main.js HTTP/1.1" 200 494
122.200.73.176  - - [2021/01/25 18:47:54 +0900] "GET /index.php HTTP/1.1" 200 266
22.80.42.4      - - [2021/02/02 04:45:09 +0900] "GET /img HTTP/1.1" 200 757
129.176.186.244 - - [2021/02/04 06:21:02 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 575
203.238.5.90    - - [2021/02/07 22:17:50 +0900] "GET /js/main.js HTTP/1.1" 200 484
233.1.189.253   - - [2021/02/08 19:48:11 +0900] "GET /img HTTP/1.1" 200 273
63.156.210.229  - - [2021/02/18 15:54:29 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 530
149.201.91.75   - - [2021/03/06 01:26:53 +0900] "GET /js/main.js HTTP/1.1" 200 716
...

dseqが激重なので範囲減らすかファイルに書き出すといいと思いますね

シェル芸160ノック24

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題127

$ while true; do echo -en "$(tput lines) $(tput cols)\r"; done

問題128

$ man pcrepattern | tail -n1 | sel 1 2
PCRE 8.38

問題129

$ base64 -d qdata/129/crypt | zcat | xxd | grep 50: | sel -- -1 | tr -d . | grep -o .... | sed "s/.*/echo -e '\\\\U&'/" | bash | tr -d \\n
シェル

30..みたいな文字列から日本語っぽいな…?とまではならなかった…まだまだですね

問題130

$  python3 -m http.server

問題131

wget example.com/big_file.tar.gz && { echo "完了だよ!" | mail -s "通知だよ" ここにメールアドレス } || { echo "失敗だよ!" | mail -s "通知だよ" ここにメールアドレス }

メールじゃなくてwslからwindowsのトーストを表示させようと思ったんだけどある程度のサイズのスクリプトを用意しないといけないのでやめた

シェル芸160ノック23

9月末に発売されたシェル芸本の続きです。むずかしい

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題121

$ (strace seq 1e7 | head) |& grep -oPe '"[^"]+"' | grep /
"/usr/bin/seq"
"/etc/ld.so.preload"
"/etc/ld.so.cache"
"/lib/x86_64-linux-gnu/libc.so.6"
"/usr/lib/locale/locale-archive"
"/usr/share/locale/locale.alias"
"/usr/lib/locale/C.UTF-8/LC_IDENTIFICATION"
"/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache"
"/usr/lib/locale/C.UTF-8/LC_MEASUREMENT"
"/usr/lib/locale/C.UTF-8/LC_TELEPHONE"
"/usr/lib/locale/C.UTF-8/LC_ADDRESS"
"/usr/lib/locale/C.UTF-8/LC_NAME"
"/usr/lib/locale/C.UTF-8/LC_PAPER"
"/usr/lib/locale/C.UTF-8/LC_MESSAGES"
"/usr/lib/locale/C.UTF-8/LC_MESSAGES/SYS_LC_MESSAGES"
"/usr/lib/locale/C.UTF-8/LC_MONETARY"
"/usr/lib/locale/C.UTF-8/LC_COLLATE"
"/usr/lib/locale/C.UTF-8/LC_TIME"
"/usr/lib/locale/C.UTF-8/LC_NUMERIC"
"/usr/lib/locale/C.UTF-8/LC_CTYPE"

問題122

$ strace xargs -n1 < qdata/122/words|&grep -cPe "^clone"
3000

問題123

$ awk -F '[( ]' '$NF ~ /\<[0-9]+\>/{print FILENAME=="dir1_strace"?1:2,$1,$NF}' dir{1,2}_strace  | tr -d "<>" | awk '{a[$2]+=$1==1?-$3:$3}END{for(k in a) print k, a[k]*1000}' | sort -k2nr | head -n3
read 11879.7
openat 860.119
close 487.181

問題124

$ ip a | sel 2 | tr -d : | xargs
lo bond0 dummy0 eth0 tunl0@NONE sit0@NONE

selはデフォルトの区切り文字がスペース1つなのがこういう時ちょっと便利

問題125

壊していいLinuxが無かった

問題126

$ bash --version                                                                                      
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.