2010年11月12日金曜日

LinuxのシェルスクリプトでFTPする方法


LinuxのコマンドでFTPすることはもちろん可能ですが、
細かい内容はすぐ忘れてしまうのでシェルスクリプトにまとめてみました。

OS: CetnOS 5.7


■FTP用シェルスクリプトサンプル

#!/bin/sh

LOG="./log_ftp.log"
TARGET_HOST="192.168.216.232"
TARGET_USER="root"
TARGET_PASS="password"
TARGET_DIR="/tmp"
PUTDATA="putdata.txt"
GETDATA="getdata.txt"

uptime > $LOG

ftp -i -v -n $TARGET_HOST << END >> $LOG
user $TARGET_USER $TARGET_PASS
cd $TARGET_DIR
bin
# put $PUTDATA
# get $GETDATA
quit
END

uptime >> $LOG

exit 0

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

2010年7月4日日曜日

PHP5.2.9 インストールメモ

■PHPインストール先のディレクトリ作成
[root@tanyao-web02 ~]# mkdir /opt/php5


■libiconvのインストール(文字コード対応に必要っぽい。)
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz からモジュールをダウンロード
[root@tanyao-web02 library_20100129]# tar -zxf libiconv-1.13.1.tar.gz
[root@tanyao-web02 library_20100129]# cd libiconv-1.13.1
[root@tanyao-web02 libiconv-1.13.1]# ./configure --prefix=/opt/php5/local
[root@tanyao-web02 libiconv-1.13.1]# make
[root@tanyao-web02 libiconv-1.13.1]# make install


■libmcryptのインストール
http://sourceforge.net/projects/mcrypt/ にてlibmcrypt-2.5.8.tar.gzをダウンロード
[root@tanyao-web02 library_20100129]# tar -zxf libmcrypt-2.5.8.tar.gz
[root@tanyao-web02 library_20100129]# cd libmcrypt-2.5.8
[root@tanyao-web02 libmcrypt-2.5.8]# ./configure --prefix=/opt/php5/mcrypt
[root@tanyao-web02 libmcrypt-2.5.8]# make
[root@tanyao-web02 libmcrypt-2.5.8]# make install


■php5本体のインストール
[root@tanyao-web02 ~]# cd /home/operation/src/php5/
[root@tanyao-web02 php5]# tar -zxf php-5.2.9.tar.gz
[root@tanyao-web02 php5]# cd php-5.2.9
[root@tanyao-web02 php-5.2.9]# ./configure \
> --prefix=/opt/php5 \
> --with-nsapi=/opt/sjsws \
> --with-libxml-dir=/usr/lib \
> --with-cdb \
> --with-gd \
> --with-jpeg-dir=/usr/lib \
> --with-png-dir=/usr/lib \
> --with-zlib \
> --with-freetype-dir=/usr/lib \
> --with-mcrypt=/opt/php5/mcrypt \
> --with-pear=/opt/php5/lib/php \
> --without-mysql \
> --enable-libgcc \
> --enable-mbstring \
> --enable-mbregex \
> --enable-zend-multibyte \
> --enable-sigchild \
> --disable-pdo

最後に下記が表示される
+--------------------------------------------------------------------+
License:
This software is subject to the PHP License, available in this
distribution in the file LICENSE. By continuing this installation
process, you are bound by the terms of this license agreement.
If you do not agree with the terms of this license, you must abort
the installation process at this point.
+--------------------------------------------------------------------+

Thank you for using PHP.

[root@tanyao-web02 php-5.2.9]# make ←5分くらいはかかる
[root@tanyao-web02 php-5.2.9]# make install
Installing PHP SAPI module: nsapi
Installing PHP CLI binary: /opt/php5/bin/
Installing PHP CLI man page: /opt/php5/man/man1/
Installing build environment: /opt/php5/lib/php/build/
Installing header files: /opt/php5/include/php/
Installing helper programs: /opt/php5/bin/
program: phpize
program: php-config
Installing man pages: /opt/php5/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /opt/php5/lib/php/
[PEAR] Console_Getopt - installed: 1.2.3
[PEAR] Archive_Tar - installed: 1.3.2
[PEAR] Structures_Graph- installed: 1.0.2
pear/PEAR can optionally use package "pear/XML_RPC" (version >= 1.4.0)
[PEAR] PEAR - installed: 1.7.2
Wrote PEAR system config file at: /opt/php5/etc/pear.conf
You may want to add: /opt/php5/lib/php to your php.ini include_path


--- SunJavaWebServerのconfigファイル群の編集 ---
[root@tanyao-web02 ~]# cd /opt/sjsws/https-tanyao-web02.tanyao.net/config

■mime.types
[root@tanyao-web02 config]# cp mime.types mime.types.20100130
[root@tanyao-web02 config]# vi mime.types
→145行目あたりに追加
type=magnus-internal/x-httpd-php exts=php


■magnus.conf
[root@tanyao-web02 config]# cp magnus.conf magnus.conf.20100130
[root@tanyao-web02 config]# vi magnus.conf

→17行目と18行目の間あたりに下記2行を追加 ★これを有効化するとWebサーバが起動エラー!!!
Init fn="load-modules" funcs="php5_init,php4_execute,php5_auth_trans" shlib="/opt/sjsws/bin/libphp5.so"
Init fn="php5_init" LateInit="yes" errorString="Failed to initialize PHP!"

■obj.conf
[root@tanyao-web02 config]# cp obj.conf obj.conf.20100130
[root@tanyao-web02 config]# vi obj.conf
---
Service method="TRACE" fn="service-trace"
Service fn="php5_execute" type="magnus-internal/x-httpd-php" ←追加
Error fn="error-j2ee"

2009年4月1日水曜日

SSHを利用したrsync検証

20090401
10.47.216.180(Cent-web)の /opt/iw 領域をrsyncで /opt/iw にレプリケーション
10.47.216.180(Cent-web)の /nfs/apps と /nfs/export をcentos01のローカルストレージの/apps /exportでマウント
centos01でTSとODが起動するかどうか検証

→結果×
iwtsユーザや、/usr/bin/iwgethome等が存在しないため


■マウントポイント作成
[root@centos01 ~]# mkdir /opt/iw
[root@centos01 ~]# mkdir /apps
[root@centos01 ~]# mkdir /export


■スタンバイサーバで公開鍵を生成
[root@centos01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
cc:61:cc:68:71:02:8f:53:d7:7f:9f:c2:6a:2a:d7:0b root@centos01.tanyao.net ←FQDNはIPアドレスに変更してもOK


■生成した公開鍵をマスタサーバへコピー。
[root@centos01 ~]# scp /root/.ssh/id_dsa.pub 10.47.216.180:/tmp/


■マスタサーバにて/tmp/id_dsa.pubファイルをauthorized_keysに追記
[root@Cent-web ~]# cat /tmp/id_dsa.pub >> /root/.ssh/authorized_keys


■バックアップサーバにてスクリプト作成
[root@centos01 ~]# cat rsync.sh
#!/bin/sh

# レプリケート対象ホスト名(またはIPアドレス)
TARGET_HOST=10.47.216.180
# レプリケート対象ディレクトリ
TARGET_DIRECTORY=/nfs/iw/
# 保存先ディレクトリ
LOCAL_DIRECTORY=/opt/iw/
# ログ出力先
LOG=./rsync.log

echo "#`date \"+%Y-%m-%d %H:%M:%S\"` rsync start" >> $LOG
rsync -avz -e ssh $TARGET_HOST:$TARGET_DIRECTORY --delete $LOCAL_DIRECTORY >> $LOG
echo "#`date \"+%Y-%m-%d %H:%M:%S\"` rsync end" >> $LOG
echo "" >> $LOG



★マウント
[root@centos01 ~]# mount -t nfs 10.47.216.180:/nfs/apps /apps
[root@centos01 ~]# mount -t nfs 10.47.216.180:/nfs/export /export

★起動スクリプトのコピー(ここだけコピーしても無駄だった)
[root@centos01 ~]# scp root@10.47.216.195:/etc/init.d/iw* /etc/init.d/

2009年3月26日木曜日

ディスク追加手順

20090326
■現状確認
[root@Cent-web ~]# df -m
Filesystem 1M-ブロック 使用 使用可 使用% マウント位置
/dev/sda4 5946 2708 2936 48% /
/dev/sda1 99 12 82 13% /boot
none 252 0 252 0% /dev/shm
/dev/sda3 2016 132 1782 7% /var


■fdisk実行
[root@Cent-web ~]# fdisk /dev/sdb
デバイスは正常な DOS 領域テーブルも、Sun, SGI や OSF ディスクラベルも
含んでいません
新たに DOS ディスクラベルを作成します。あなたが書き込みを決定するまで、変更は
メモリ内だけに残します。その後はもちろん以前の内容は修復不可能になります。

このディスクのシリンダ数は 1566 に設定されています。
間違いではないのですが、1024 を超えているため、以下の場合
に問題を生じうる事を確認しましょう:
1) ブート時に実行するソフトウェア (例. バージョンが古い LILO)
2) 別の OS のブートやパーティション作成ソフト
(例. DOS FDISK, OS/2 FDISK)
警告: 領域テーブル 4 の不正なフラグ 0x0000 は w(書き込み)によって正常になります

コマンド (m でヘルプ): m
コマンドの動作
a ブート可能フラグをつける
b bsd ディスクラベルを編集する
c dos 互換フラグをつける
d 領域を削除する
l 既知の領域タイプをリスト表示する
m このメニューを表示する
n 新たに領域を作成する
o 新たに空の DOS 領域テーブルを作成する
p 領域テーブルを表示する
q 変更を保存せずに終了する
s 空の Sun ディスクラベルを作成する
t 領域のシステム ID を変更する
u 表示/項目ユニットを変更する
v 領域テーブルを照合する
w テーブルをディスクに書き込み、終了する
x 特別な機能 (エキスパート専用)

コマンド (m でヘルプ): p

Disk /dev/sdb: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = シリンダ数 of 16065 * 512 = 8225280 bytes

デバイス Boot Start End Blocks Id System

コマンド (m でヘルプ): n
コマンドアクション
e 拡張
p 基本領域 (1-4)
p
領域番号 (1-4): 1
最初 シリンダ (1-1566, default 1): 1
終点 シリンダ または +サイズ または +サイズM または +サイズK (1-1566, default 1566): 700

コマンド (m でヘルプ): n
コマンドアクション
e 拡張
p 基本領域 (1-4)
p
領域番号 (1-4): 2
最初 シリンダ (701-1566, default 701): 701
終点 シリンダ または +サイズ または +サイズM または +サイズK (701-1566, default 1566): 1100

コマンド (m でヘルプ): n
コマンドアクション
e 拡張
p 基本領域 (1-4)
p
領域番号 (1-4): 3
最初 シリンダ (1101-1566, default 1101): 1101
終点 シリンダ または +サイズ または +サイズM または +サイズK (1101-1566, default 1566): 1566

コマンド (m でヘルプ): p

Disk /dev/sdb: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = シリンダ数 of 16065 * 512 = 8225280 bytes

デバイス Boot Start End Blocks Id System
/dev/sdb1 1 700 5622718+ 83 Linux
/dev/sdb2 701 1100 3213000 83 Linux
/dev/sdb3 1101 1566 3743145 83 Linux

コマンド (m でヘルプ): w
領域テーブルは交換されました!

ioctl() を呼び出して領域テーブルを再読込みします。
ディスクを同期させます。


■ext3でフォーマット
[root@Cent-web ~]# mke2fs -j /dev/sdb1
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
703136 inodes, 1405679 blocks
70283 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1442840576
43 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

■同様にパーティション2,3もフォーマット
[root@Cent-web ~]# mke2fs -j /dev/sdb2
[root@Cent-web ~]# mke2fs -j /dev/sdb3

■ラベルの割当
[root@Cent-web ~]# e2label /dev/sdb1 /iw
[root@Cent-web ~]# e2label /dev/sdb2 /apps
[root@Cent-web ~]# e2label /dev/sdb3 /export

■ファイルシステムチェック
[root@Cent-web ~]# e2fsck /dev/sdb1
e2fsck 1.35 (28-Feb-2004)
/ts: clean, 11/703136 files, 33027/1405679 blocks
[root@Cent-web ~]# e2fsck /dev/sdb2
e2fsck 1.35 (28-Feb-2004)
/apps: clean, 11/402400 files, 22021/803250 blocks
[root@Cent-web ~]# e2fsck /dev/sdb3
e2fsck 1.35 (28-Feb-2004)
/export: clean, 11/468640 files, 24751/935786 blocks

■マウントポイントの作成
[root@Cent-web ~]# mkdir /nfs/iw
[root@Cent-web ~]# mkdir /nfs/apps
[root@Cent-web ~]# mkdir /nfs/export

■/etc/fstabに追記
LABEL=/iw /nfs/iw ext3 defaults 1 2
LABEL=/apps /nfs/apps ext3 defaults 1 2
LABEL=/export /nfs/export ext3 defaults 1 2

■マウント
[root@Cent-web ~]# mount -a
確認
[root@Cent-web ~]# mount grep sdb
/dev/sdb1 on /nfs/iw type ext3 (rw)
/dev/sdb2 on /nfs/apps type ext3 (rw)
/dev/sdb3 on /nfs/export type ext3 (rw)



■公開ディレクトリの設定
[root@Cent-web ~]# vi /etc/exports
/nfs/iw *(rw,sync,no_root_squash)
/nfs/apps *(rw,sync,no_root_squash)
/nfs/export *(rw,sync,no_root_squash)

■NFSサービス起動
[root@Cent-web ~]# /etc/init.d/nfs start
NFS サービスを起動中: [ OK ]
NFS クォータを起動中: [ OK ]
NFS デーモンを起動中: [ OK ]
NFS mountd を起動中: [ OK ]

■NFSサービス起動設定
[root@Cent-web ~]# chkconfig --level 3 nfs on
[root@Cent-web ~]# chkconfig --level 5 nfs on


■NFSクライアントが起動時にマウントする設定
[root@iwts04 iw]# cat /etc/fstab
(下記を追記)
# NFS mount settings
192.168.216.180:/nfs/iw /opt/iw nfs nfsvers=3,rsize=32768,wsize=32768,rw,hard,intr 0 0
192.168.216.180:/nfs/apps /apps nfs nfsvers=3,rsize=32768,wsize=32768,rw,hard,intr 0 0
192.168.216.180:/nfs/export /export nfs defaults 0 0


■手動マウント
mount -t nfs -o nfsvers=3,rsize=32768,wsize=32768,rw,hard,intr 192.168.216.180:/nfs/export /export
mount -t nfs -o nfsvers=3,rsize=32768,wsize=32768,rw,hard,intr 192.168.216.180:/nfs/iw /opt/iw

2008年6月14日土曜日

アクセスログ出力サンプル

_____________________________
index_access.pl
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
#!/usr/bin/perl --

#=================================================================
# 時刻設定
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon = $mon + 1;
$thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];

$jikoku = sprintf("%04d/%02d/%02d (%03s) %02d:%02d:%02d", $year, $mon, $mday, $thisday, $hour, $min, $sec);

# 環境変数の取得
$ip = $ENV{'REMOTE_ADDR'}; #// リモート端末のIP アドレス
$port = $ENV{'REMOTE_PORT'}; #// リモート端末のポート番号
$url = $ENV{'HTTP_REFERER'}; #// リモート端末の直前の URL
$blws = $ENV{'HTTP_USER_AGENT'}; #// ブラウザ情報
$path = $ENV{'PATH_INFO'}; #// 現 CGI のパス情報

$rtpc = $ENV{'REMOTE_HOST'}; #// リモート端末のホスト名
$svpc = $ENV{'SERVER_NAME'}; #// サーバのホスト名
$webs = $ENV{'SERVER_SOFTWARE'}; #// ウェブサーバのソフト名と ver
$wcgi = $ENV{'GATEWAY_INTERFACE'}; #// ウェブサーバの CGI ver

#===============================================================


open(FILE,">>index_access.log") or die "$!";
flock(FILE,2);
print FILE "$jikoku,$ip,$port,$url,$rtpc,$blws\n";
flock(FILE,8);
close(FILE);

#// 終了
exit(0);


_____________________________
呼び出し側HTML(一部)
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄