2011年4月26日火曜日

シェルスクリプトサンプル:ユーザ一括ロック


#!/bin/sh
# ロック対象ユーザリストを読込んで対象ユーザをロックするスクリプト

LOCK_USER=lock_user.list

# ロック対象ユーザリスト存在チェック
if [ ! -e $LOCK_USER ]; then
echo "`date \"+%Y%m%d %H:%M:%S\"` ERROR: $LOCK_USER is not exist."
exit 1
fi

# ロック対象ユーザリスト読込み
i=0
while read line
do
user_name[$i]="$line"

# ユーザ存在チェック
id ${user_name[$i]} > /dev/null 2>&1
if [ $? -eq 0 ]; then
# ユーザをロック(無効化)
/usr/bin/passwd -l ${user_name[$i]}

# ユーザロック解除(参考)
# /usr/bin/passwd -u ${user_name[$i]}
else
echo "`date \"+%Y%m%d %H:%M:%S\"` ERROR: ${user_name[$i]} is not exist."
fi

i=`expr $i + 1`
done < $LOCK_USER

exit 0

-------------------------------------------------
■ユーザネーム一覧出力コマンド
[root@tanyao08~]# awk -F":" '{ print $1 }' /etc/passwd

■ロックユーザ一覧出力コマンド
[root@tanyao08 ~]# grep '!!' /etc/shadow

2011年3月31日木曜日

シェルスクリプトサンプル:ファイルローテート


#!/bin/sh
# ファイルサイズを判定してローテートするサンプル

# << SCRIPT SUB SECTION >>

##### ファイルサイズを判定してローテート
# $1:対象ファイル
# $2:ローテート条件
rotate() {

file_size=`ls -lk --time-style='+%Y%m%d' $1 | awk '{print $5}'`
file_time=`ls -lk --time-style='+%Y%m%d' $1 | awk '{print $6}'`

# ファイルサイズがローテート条件を超えたら
if [ ${file_size} -gt $2 ]; then
tar -zcf $1.${file_time}.tar.gz $1
# 0バイト化
:> $1
fi
}


# << SCRIPT MAIN SECTION >>

TRAGET_DIR=/home/operation # ローテート対象ディレクトリ
TRAGET_FILE=rsync.log # ローテート対象ファイル
THRESHOLD=20 # KBで指定

BASE=`pwd`
cd ${TRAGET_DIR}

rotate ${TRAGET_FILE} ${THRESHOLD}

ls -l ${TRAGET_FILE}*
cd ${BASE}

exit 0

2011年3月10日木曜日

ApacheのBasic認証設定

今さらですがApacheのBasic認証設定のメモ。

OS: CentOS 5.7


■.htpasswdの作成
[root@f1 ~]# /usr/bin/htpasswd -c /etc/httpd/.htpasswd secret
New password: ←yak*****
Re-type new password: ←yak*****
Adding password for user secret
[root@f1 ~]#
※OSユーザにsecretが登録されている必要はない。

■httpd.confの修正
[root@f1 ~]# vi /etc/httpd/conf/httpd.conf
(中略)
ServerName www.dummy84.com
 DocumentRoot /files3/www

 AllowOverride AuthConfig ←Noneから変更(.htaccessが無ければ認証はかからない)
 Options ExecCGI FollowSymLinks
 Order allow,deny
 Allow from all
 Dav On

■Apache再起動
[root@f1 ~]# /usr/sbin/apachectl stop
[root@f1 ~]# /usr/sbin/apachectl -f /etc/httpd/conf/httpd_vhost.conf

■.htaccessの配置
[root@f1 ~]# vi /files3/www/.htaccess
[root@f1 ~]# cat /files3/www/.htaccess
AuthType Basic
AuthUserFile /etc/httpd/.htpasswd
AuthGroupFile /dev/null
AuthName "Please Enter Your Name and Password"
require valid-user
[root@f1 ~]# chmod 444 /files3/www/.htaccess 

こちらの情報が何かのお役にたてれば幸いです。m(_ _)m

2011年2月1日火曜日

シェルスクリプトサンプル:一括文字コード変換


#!/bin/sh

# 作業ディレクトリ(現在地)
BASE=`pwd`

# 変換対象元ディレクトリ
DIR=/home/efiles3/public/iw/072_STG-Web

# 変換対象ファイルの拡張子を指定する。
FILE_TYPE=".txt"

# 変換先文字コードオプション(UTF-8:-w SJIS:-s EUC:-e)
CHARA_CODE="-s"


# 出力先ディレクトリ作成
if [ ${CHARA_CODE} = "-w" ];then
OUTPUT_DIR="output_utf-8"
elif [ ${CHARA_CODE} = "-s" ];then
OUTPUT_DIR="output_sjis"
elif [ ${CHARA_CODE} = "-e" ];then
OUTPUT_DIR="output_euc"
else
echo "Character code is not existing error !!"
exit 0
fi
mkdir ./$OUTPUT_DIR

# 文字コード変換
cd $DIR
for i in `ls -1` ; do
/usr/bin/nkf $CHARA_CODE $i > $BASE/$OUTPUT_DIR/$i
done
cd $BASE


exit 0

2011年1月7日金曜日

シェルスクリプトサンプル:diffの差分比較結果をファイル出力

diffコマンドを利用して、2つのファイルの差分比較を行い、その結果を
ファイルに出力するシェルスクリプトのサンプルです。
Webコンテンツの移行作業において、移行前と移行後のファイルの差分が
発生していないか検証する際に便利です。


■サンプルスクリプト(sabun.sh)

[root@tanyao ~]# cat sabun.sh
#!/bin/sh

# 必ず比較対象ファイルと同階層にこのシェルを配置して実行してください!!

# 比較対象(パス指定禁止)
FILE1=file1.txt
FILE2=file2.txt

# ファイル名にスペースを含むファイルはNG扱いとする
if [ `grep " " ${FILE1} | wc -l` != 0 ] ; then
    grep " " ${FILE1} > diff_NG_${FILE1}
fi
if [ `grep " " ${FILE2} | wc -l` != 0 ] ; then
    grep " " ${FILE2} > diff_NG_${FILE2}
fi

# 並び替え
grep -v " " ${FILE1} | sort > sort_${FILE1}
grep -v " " ${FILE2} | sort > sort_${FILE2}

# 比較
diff -y -t -d sort_${FILE1} sort_${FILE2} > diff.txt

# 同一レコードの抽出
grep -v "<" diff.txt | grep -v ">" | grep -v "|" > diff_same.txt

# 第1ファイルにのみ存在
grep -e "<" -e "|" diff.txt | cut -d " " -f 1 > diff_only_${FILE1}

# 第2ファイルにのみ存在
grep ">" diff.txt | cut -d ">" -f 2 > diff_tmp_${FILE2}
grep "|" diff.txt | cut -d "|" -f 2 >> diff_tmp_${FILE2}
sed -e 's/^  //' diff_tmp_${FILE2} > diff_only_${FILE2}

# 作業ファイルの削除
rm -rf sort_${FILE1}
rm -rf sort_${FILE2}
rm -rf diff_tmp_${FILE2}

exit 0
[root@tanyao ~]#


■テストデータ準備

[root@tanyao ~]# cat file1.txt
/var/log/httpd/
/var/log/httpd/error_log
/var/log/httpd/error_log-20110103
/var/log/httpd/access_log
/var/log/httpd/access_log-20110103
[root@tanyao ~]#

[root@tanyao ~]# cat file2.txt
/var/log/httpd/
/var/log/httpd/error_log
/var/log/httpd/error_log-20110104
/var/log/httpd/access_log
/var/log/httpd/access_log.1
/var/log/httpd/access_log-20110103
[root@tanyao ~]#


■使用例および結果確認

[root@tanyao ~]# ./sabun.sh
[root@tanyao ~]# cat diff_only_file1.txt
/var/log/httpd/error_log-20110103
[root@tanyao ~]# cat diff_only_file2.txt
/var/log/httpd/access_log.1
/var/log/httpd/error_log-20110104
[root@tanyao ~]# cat diff_same.txt
/var/log/httpd/                                 /var/log/httpd/
/var/log/httpd/access_log                       /var/log/httpd/access_log
/var/log/httpd/access_log-20110103              /var/log/httpd/access_log-20110103
/var/log/httpd/error_log                        /var/log/httpd/error_log
[root@tanyao ~]#


■diffオプション

-y, --side-by-side
        side-by-side 出力形式を用いる。

side-by-side 形式

ファイルは 2 列に表示され、間に溝 (gutter) が置かれる。溝には以下のマーカーのいずれか 1 つが含まれる。

' '    対応する行が共通である。つまり、両方の行が同一であるか、違いが --ignore オプションのいずれかによって無視された。
|      対応する行が異なる。両方とも完全か、両方とも不完全かである。
<      ファイルは異なり、1 番目のファイルにだけこの行が含まれている。
>      ファイルは異なり、2 番目のファイルにだけこの行が含まれている。
(      1 番目のファイルにだけこの行が含まれているが、違いは無視される。
)      2 番目のファイルにだけこの行が含まれているが、違いは無視される。


サイト継続にご協力ありがとうございます。こちらの情報が何かのお役にたてれば幸いです。m(_ _)m