tag:crieit.net,2005:https://crieit.net/users/dsta50/feed dsta50の投稿 - Crieit Crieitでユーザーdsta50による最近の投稿 2022-08-07T01:46:25+09:00 https://crieit.net/users/dsta50/feed tag:crieit.net,2005:PublicArticle/17975 2022-02-04T22:26:58+09:00 2022-08-07T01:46:25+09:00 https://crieit.net/posts/Linux-messages [Linux] messagesログの日付指定参照 <p>Linuxにおける「/var/log/messages」ファイルのフォーマットは以下のように「日 時 ホスト名 情報」となっている。</p> <pre><code>Feb 16 18:00:10 web sshd[13865]: Accepted publickey for user01 from 192.168.0.2 port 45680 ssh2 </code></pre> <p>問題なのが1桁の日にちの場合以下のように「月(スペース)(スペース)日にち」の表現になっている点だ。</p> <pre><code>Feb 4 21:25:22 ホスト名 情報 </code></pre> <p>「04」ならともかく「(スペース)4」と表現されることにより、<br /> bashスクリプト等で実行日に絞り込みたい場合、以下のコマンドだと絞り込みができない。</p> <pre><code>$ LANG=C date "+%b %d" Feb 04 </code></pre> <hr /> <p>以下のコマンドだと、1桁の日にちも2桁の日にちも処理できる。</p> <pre><code>$ LANG=C date | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//" </code></pre> <p><code>LANG=C date</code>「 Fri Feb 4 22:10:50 JST 2022」のような日付を取得<br /> <code>cut -d " " -f 2-4</code> 「Feb 4 」に絞り込み、ただし、2桁日にちの場合時刻まで取れてしまう。<br /> <code>sed -e "s/ ..:.*$//"</code> 2桁日にちの場合の時刻除去</p> <p>1桁日にちのコマンド実行例</p> <pre><code>$ LANG=C date | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//" Feb 4 </code></pre> <p>2桁日にちのコマンド実行例</p> <pre><code>LANG=C date --date '5 day ago' | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//" Jan 30 </code></pre> <p><strong>注意として変数に入れて使用する場合、変数をダブルクォート(")で囲む必要がありそうだ。<br /> そうしないとスペースの2個が1個で処理してしまう可能性がある。</strong></p> <pre><code>$ LOGDATE=`LANG=C date | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//"` $ echo $LOGDATE Feb 4 echo "$LOGDATE" Feb 4 $ </code></pre> <hr /> <p>ワンライナーでの実行例 (先頭.は表現回避用)</p> <pre><code>.# cat /var/log/messages | grep "`LANG=C date | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//"`" Feb 4 10:0:01 ホスト名 情報 Feb 4 10:0:01 ホスト名 情報 </code></pre> <p>変数定義後の事項例 (先頭.は表現回避用)</p> <pre><code>.# LOGDATE=`LANG=C date | cut -d " " -f 2-4 | sed -e "s/ ..:.*$//"` .# cat /var/log/messages | grep "${LOGDATE}" Feb 4 10:0:01 ホスト名 情報 Feb 4 10:0:01 ホスト名 情報 </code></pre> dsta50 tag:crieit.net,2005:PublicArticle/17916 2022-01-03T23:53:56+09:00 2022-05-05T23:33:42+09:00 https://crieit.net/posts/Linux-ssh [Linux] ssh鍵の自動生成 <p>Linuxの1ユーザの複数サーバ分のssh鍵の自動生成</p> <p>コードブロックに行番号を付加できないかは模索中</p> <p>ファイル名;create_key.sh</p> <pre><code>#!/bin/bash # values KEYSIZE=2048 # script dir DIR_BASE=$(cd $(dirname ${0}); pwd) if [ $# = 0 ] ; then echo -e "Error: No arguments set" echo -e "Usage: $0 [user name] [server01:server02:...]" exit 10 fi USER=$1 SERVERS=`echo $2 | sed -e 's/:/ /g'` USERDIR=${DIR_BASE}/${USER} test ! -d ${USERDIR} && mkdir ${USERDIR} for server in ${SERVERS} do test ! -d ${USERDIR}/${server} && mkdir ${USERDIR}/${server} passphrase=`cat /dev/urandom | tr -dc 'a-kmnpr-zABD-HJL-NQRPT0-9#?_!' | fold -w 8 | head -n 300 | grep -v '^[#?_!]' | grep '[#?_!]' | grep [0-9] | grep -vE '(.)\1' | shuf -n 1` keyfile=${USERDIR}/${server}/${USER} ssh-keygen -f ${keyfile} -t rsa -b ${KEYSIZE} -N ${passphrase} if [ $? = 0 ] ; then echo -e "User: ${USER} ,Server: ${server}, Passphrase: [${passphrase}]" echo -e "## ${server}\nUser:\n${USER}\n\nPassphrase:\n${passphrase}" > ${server}_${USERDIR}/SSHinfo_${USER}.txt else echo -e "Couldn't make the key. -User: ${USER} ,Server: ${server}" fi done </code></pre> <p>実行例</p> <pre><code>$ ./create_key.sh user01 web:mail </code></pre> <p><code>引数1</code> ユーザ名<br /> <code>引数2</code> サーバ名、複数の場合はサーバ名を: (コロン)で区切る</p> <p>説明<br /> 1. スクリプトファイルと同じディレクトリ階層に作成したい ユーザ名のディレクトリを作成する (実行例:user01)<br /> 2. サーバごと以下2-1から2-4を繰り返す<br /> 2-1.1番で作1番で作成したユーザディレクトリ配下にサーバ名のディレクトリを作成する (実行例:web)<br /> 2-2.パスフレーズをランダムの文字列で生成<br /> 2-3.2-2番で生成したパスフレーズをもとに、2-1番で作成したサーバ名のディレクトリに鍵のペアを作成<br /> 2-4.1番で作成したユーザ名のディレクトリにサーバ名、ユーザ名、パスフレーズを記載したファイルを作成</p> <p>2-4で作成したファイル名はSSHinfo_サーバ名_ユーザ名.txt<br /> 例 web_SSHinfo_user01.txt</p> <pre><code>## web User: user01 Passphrase: U4#DvXnH </code></pre> <p>エラー処理を最小限にしているため、予期せぬ事態に対処できてないかも</p> <p>課題 (後日記載)<br /> 複数ユーザに対応するため、引数にリストファイルを指定することで、複数ユーザの指定サーバごとに鍵を作成する</p> <p>memo<br /> psslibでのパスワードのハッシュ化</p> <pre><code> passhash=`python3 -c 'import passlib.hash; print(passlib.hash.sha512_crypt.using(rounds=5000).hash("$passphrase"))'` echo -e "password hash is [${passhash}] " echo -e "\npassword hash\n${passhash}" >> ${server}_${USERDIR}/SSHinfo_${USER}.txt </code></pre> dsta50 tag:crieit.net,2005:PublicArticle/17908 2022-01-01T21:51:05+09:00 2022-05-05T23:31:37+09:00 https://crieit.net/posts/Linux-61d04e3986a0e [Linux] ランダム文字列 <p>Linux環境にてパスワードやパスフレーズ等の設定に使うランダム文字列を生成</p> <p>条件:8文字構成で数字、アルファベット小文字、大文字、記号の使用かつ類似文字を除外<br /> 数字 10文字 (0 1 2 3 4 5 6 7 8 9)<br /> 小文字 23文字 (a b c d e f g h i j k m n p r s t u v w y z)、除外 (l o q)<br /> 大文字 14文字 (A B D E F G H J L M N Q R T)、除外 (C I K O S U V W X Y Z)<br /> 記号 4文字 (# ? _ !)</p> <pre><code>cat /dev/urandom | tr -dc 'a-kmnpr-zABD-HJL-NQRPT0-9#?_!' | fold -w 8 | head -n 300 | grep -v '^[#?_!]' | grep '[#?_!]' | grep [A-Z] | grep [a-z] | grep [0-9] | grep -vE '(.)\1' | uniq | shuf -n 1 </code></pre> <p>省略版 記号と数字のチェックすれば問題なさそう</p> <pre><code>cat /dev/urandom | tr -dc 'a-kmnpr-zABD-HJL-NQRPT0-9#?_!' | fold -w 8 | head -n 300 | grep -v '^[#?_!]' | grep '[#?_!]' | grep [0-9] | grep -vE '(.)\1' | shuf -n 1 </code></pre> <p><code>cat /dev/urandom</code> ランダムな文字列を生成<br /> <code>tr -dc 'a-kmnpr-zABD-HJL-NQRPT0-9#?_!'</code> 条件の含めたい文字<br /> <code>fold -w 8</code> 8文字<br /> <code>head -n 300</code> 行頭から300文字<br /> <code>grep -v '^[#?_!]'</code> 先頭記号の文字列を除く<br /> <code>grep '[#?_!]' | grep [A-Z] | grep [a-z]</code> 条件に合致しているか絞り込み<br /> <code>grep -vE '(.)\1'</code> 繰り返し文字を除く<br /> <code>uniq</code> 念のため重複行の取り除き、後のコマンドで含まれているのでいらない<br /> <code>shuf -n 1</code> ランダムの1行を出力</p> <p>実行例</p> <pre><code>$ cat /dev/urandom | tr -dc 'a-kmnpr-zABD-HJL-NQRPT0-9#?_!' | fold -w 8 | head -n 300 | grep -v '^[#?_!]' | grep '[#?_!]' | grep [0-9] | grep -vE '(.)\1' | shuf -n 1 9eD?8aiG </code></pre> dsta50