2011年6月14日火曜日

smbセッションキャッシュクリア

--------------------------------------------------
同じユーザーによる、サーバーまたは共有リソースへの複数のユーザー名での複数の接続は許可されません。
サーバーまたは共有リソースへの以前の接続をすべて切断してから、再試行してください。
--------------------------------------------------
上記のようなメッセージが表示され、smb領域のディレクトリにアクセスできない場合
コマンドプロンプトから解消可能。

C:\Documents and Settings\Administrator>net use
新しい接続は記憶されます。


ステータス ローカル名 リモート名 ネットワーク名

-------------------------------------------------------------------------------
OK \\192.168.188.54\files_bk Microsoft Windows Network
\\TSCLIENT\C Microsoft Terminal Services
\\TSCLIENT\D Microsoft Terminal Services
\\TSCLIENT\S Microsoft Terminal Services
コマンドは正常に終了しました。


C:\Documents and Settings\Administrator>net use \\192.168.188.54\files_bk /delete

\\192.168.188.54\files_bk が削除されました。

2011年6月7日火曜日

Linux OSユーザ一括作成シェルスクリプト

通常OSユーザを作成する場合は、useraddコマンドおよびpasswdコマンドを手動で打ち、
一人ひとり作成していきますが、ユーザ数が多くなるとちょっと厄介、ぶっちゃけめんどいです。

今回紹介するスクリプトはユーザ情報がリスト化されたファイルを読み込んで一括でユーザを
作成することができます。
ただし、ユーザが所属するグループを事前に作成しておく必要があります。


■グループを事前に作成
[root@tanyao script]# /usr/sbin/groupadd -g 5000 public

■ユーザリストを準備
[root@tanyao script]# cat mk_user.list
# name:password:uid:gid:comment:home_directory:login_shell:sub_gid
user01:secret01:5001:5000:::/bin/bash
user02:secret02:5002:5000:::/bin/bash
user03:secret03:5003:5000:::/bin/bash
#user04:secret04:5004:5000:::/bin/bash
user05:secret05:5005:5000:::/bin/bash
[root@tanyao script]#
※ /etc/passwd ファイルのフォーマットに合わせてみた。つもり。

■ユーザ一括作成スクリプト(mk_user.sh)
[root@tanyao script]# cat mk_user.sh
#!/bin/sh
# mk_user.listに記述されたユーザを一括で作成します。
# 行頭に"#"がある行はコメント行とみなします。
# 既に存在しているユーザを作成しようとするとスキップされます。
# 事前にグループを作成ください。
# rootユーザで実行してください。

USERLIST=./mk_user.list

if [ ! -f ${USERLIST} ]; then
    echo "${USERLIST} no such file or directory"
    exit 1
fi

grep -v \^# ${USERLIST} | while read line
do
    name="`echo ${line} | cut -d ":" -f1`"
    passwd="`echo ${line} | cut -d ":" -f2`"
    uid="`echo ${line} | cut -d ":" -f3`"
    gid="`echo ${line} | cut -d ":" -f4`"
    comment="`echo ${line} | cut -d ":" -f5`"
    homedir="`echo ${line} | cut -d ":" -f6`"
    loginsh="`echo ${line} | cut -d ":" -f7`"
    subgid="`echo ${line} | cut -d ":" -f8`"

# サブグループがない場合
if [[ ${subgid} = "" ]]; then
    /usr/sbin/useradd -u ${uid} -g ${gid} -c "${comment}" ${name} || continue
# サブグループがある場合
else
    /usr/sbin/useradd -u ${uid} -g ${gid} -G ${subgid} -c "${comment}" ${name} || continue
fi

echo ${name}:${passwd} | chpasswd
echo "Making ${name} succeeded."
done

exit 0
[root@tanyao script]#
[root@tanyao script]# chmod 755 mk_user.sh

■ユーザ作成
[root@tanyao script]# ./mk_user.sh
Making user01 succeeded.
Making user02 succeeded.
Making user03 succeeded.
Making user05 succeeded.
[root@tanyao script]#

■確認
[root@tanyao script]# su - user01
[user01@tanyao ~]$ su - user02
Password:
[user02@tanyao ~]$ id
uid=5002(user02) gid=5000(public) groups=5000(public)
[user02@tanyao ~]$ pwd
/home/user02
[user02@tanyao ~]$

■ユーザ削除コマンド(参考)
[root@tanyao script]# userdel -r user05
[root@tanyao script]# id user05
id: user05: No such user
[root@tanyao script]#


◆ユーザ作成コマンドオプション確認
[root@tanyao script]# useradd --help
Usage: useradd [options] LOGIN

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

[root@tanyao script]#

◆ユーザ編集コマンドオプション確認
[root@tanyao script]# usermod --help
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -Z, --selinux-user            new SELinux user mapping for the user account

[root@tanyao script]#

◆ユーザ削除コマンドオプション確認
[root@tanyao script]# userdel --help
Usage: userdel [options] LOGIN

Options:
  -f, --force                   force removal of files,
                                even if not owned by user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -Z, --selinux-user            remove SELinux user from SELinux user mapping

[root@tanyao script]#


この情報がお役に立ちましたら、いえ、ご寄付はいりません。
サイト継続のご協力を賜りたく存じます。 m(_ _)m

2011年6月2日木曜日

シェルスクリプトサンプル:ログファイル等のアーカイブ

独自開発したアプリケーションのログ等でローテート処理が考慮されていない場合、
ログファイルが増殖し続け、気づいた時にはエライことになっていたなんてことは
誰しもあるはずです。

古くなってきたログファイルを洗い出して圧縮し、さらに一定期間が過ぎると自動削除
するシェルスクリプトを作成しcrontabに登録しておけば、ディスク使用率アラートで
夜中に起こされて対応することはなくなるはずです。


■シェルスクリプトサンプル
#!/bin/sh
# 指定日数より前のファイルを圧縮し、さらに指定日数より古いアーカイブファイルを削除

ARCHIVE_DAYS=10
DELETE_DAYS=60
TARGET_DIR=/root/scripts/logs
OUTPUT=${TARGET_DIR}/`date +%Y%m%d-%H%M`_ArchiveFiles.tar.gz

# 対象ディレクトリへ移動
cd ${TARGET_DIR}

# 圧縮するファイル(拡張子は、.logまたは.txt)のリストを作成
find . -mtime +${ARCHIVE_DAYS} -iregex ".*\.log$\|.*\.txt$" > ${TARGET_DIR}/file.list

# 圧縮対象ファイルが存在するかのチェック
num_line=`cat ${TARGET_DIR}/file.list | wc -l`
if [ ${num_line} -gt 0 ]; then

 # リストに従ってtar.gzファイルを作成
tar -zcf ${OUTPUT} -T ${TARGET_DIR}/file.list

 # リストに従って元ファイルを削除
for i in `cat ${TARGET_DIR}/file.list`
 do
 rm -rf ${i}
 done

else
 echo "ERROR: More than ${ARCHIVE_DAYS} days before the file does not exist !!"
fi

# 指定日数より古いアーカイブファイルを削除
find . -mtime +${DELETE_DAYS} -regex ".*\.tar.gz$" | xargs rm -rf

# リストファイルを削除
# rm -rf ${TARGET_DIR}/file.list

exit 0

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

2011年5月6日金曜日

プロキシ自動構成スクリプト


「インターネットへのアクセス時は proxy を通したいけど、イントラネットへのアクセスは proxy を通したくない」みたいな場合に使うファイル。

■IEの自動構成スクリプト設定方法
① インターネットオプション>『接続』タブ>『LANの設定』を押下
② 『ローカルエリアネットワーク(LAN)の設定』の『自動構成』にて
1) 『自動構成スクリプトを使用する』にチェック
2) 『アドレス』に、PACファイル配置先(例⇒file://c:\proxy.pac)を入力

■proxy.pacのサンプル
function FindProxyForURL(url, host) {

// URLにgoogleが含まれるサイトはプロキシ経由する。
if (shExpMatch(host, "*.google.*")) {
return "PROXY proxy1.tanyao.net:8080";
}

// プロキシ1がダウンしていた場合はプロキシ2を使用する。
else if (shExpMatch(host, "119.171.187.*") shExpMatch(host, "*.asahi.com")) {
return "PROXY 135.248.58.101:8080; PROXY proxy2.tanyao.net:8080";
}

// ローカルサイトはプロキシを経由しない。
else if (shExpMatch(host, "192.168.216.*") shExpMatch(host, "*.tanyao.net")) {
return "DIRECT";
}

else {
return "PROXY 135.248.58.101:8080";
}
}

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