2009. 6. 11.

캐리지 리턴 문자 없애기

vi
:%s/^M//g

find `pwd` -name "*.*" | xargs grep "^M"

find `pwd` -name "*.*" -exec perl -pi -e 's/^M//g' {} \; -print

2009. 5. 21.

[Shell] 구구단 및 별 출력 스크립트

[1] 구구단 출력

#!/bin/bash
dan=2
while [ $dan -lt 10 ]
do
echo " $dan 단"
echo "======"
num=1
while [ $num -lt 10 ]
do
answer=`expr $dan \* $num`
if [ $answer -lt 10 ]; then
answer=" $answer"
fi
echo "${dan}x${num}=${answer}"
num=`expr $num + 1`
done

dan=`expr $dan + 1`
echo ""
done

출력 :
2 단
======
2x1= 2
2x2= 4
2x3= 6
2x4= 8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18

.....

9 단
======
9x1= 9
9x2=18
9x3=27
9x4=36
9x5=45
9x6=54
9x7=63
9x8=72
9x9=81


[2] 구구단 출력 가로
#!/bin/bash
echo ""
num=1
while [ $num -lt 10 ]
do
dan=2
while [ $dan -lt 6 ]
do
answer=`expr $dan \* $num`
if [ $answer -lt 10 ]; then
answer=" $answer"
fi
echo -n "${dan}x${num}=${answer} "
dan=`expr $dan + 1`
done

num=`expr $num + 1`
echo ""
done

echo ""

num=1
while [ $num -lt 10 ]
do
dan=6
while [ $dan -lt 10 ]
do
answer=`expr $dan \* $num`
if [ $answer -lt 10 ]; then
answer=" $answer"
fi
echo -n "${dan}x${num}=${answer} "
dan=`expr $dan + 1`
done

num=`expr $num + 1`
echo ""
done
echo ""

출력 :

2x1= 2 3x1= 3 4x1= 4 5x1= 5
2x2= 4 3x2= 6 4x2= 8 5x2=10
2x3= 6 3x3= 9 4x3=12 5x3=15
2x4= 8 3x4=12 4x4=16 5x4=20
2x5=10 3x5=15 4x5=20 5x5=25
2x6=12 3x6=18 4x6=24 5x6=30
2x7=14 3x7=21 4x7=28 5x7=35
2x8=16 3x8=24 4x8=32 5x8=40
2x9=18 3x9=27 4x9=36 5x9=45

6x1= 6 7x1= 7 8x1= 8 9x1= 9
6x2=12 7x2=14 8x2=16 9x2=18
6x3=18 7x3=21 8x3=24 9x3=27
6x4=24 7x4=28 8x4=32 9x4=36
6x5=30 7x5=35 8x5=40 9x5=45
6x6=36 7x6=42 8x6=48 9x6=54
6x7=42 7x7=49 8x7=56 9x7=63
6x8=48 7x8=56 8x8=64 9x8=72
6x9=54 7x9=63 8x9=72 9x9=81




[3] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
while [ $j -le $i ]
do
echo -n "*"
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done
echo ""
출력 :
Number : 10

*
**
***
****
*****
******
*******
********
*********
**********

[4] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su - $i`
while [ $j -le $su ]
do
if [ $j -le $k ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

*
**
***
****
*****
******
*******
********
*********
**********

[5] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su - $i`
l=`expr $su + $i - 1`
while [ $j -le $l ]
do
if [ $j -le $k ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************

[6] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su - $i + 1`
while [ $j -le $k ]
do
echo -n "*"
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

**********
*********
********
*******
******
*****
****
***
**
*

[7] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
while [ $j -le $su ]
do
if [ $j -lt $i ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

**********
*********
********
*******
******
*****
****
***
**
*

[8] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su \* 2 - $i`
while [ $j -le $k ]
do
if [ $j -lt $i ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`

done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

*******************
*****************
***************
*************
***********
*********
*******
*****
***
*

[9] 별 출력
#!/bin/bash
echo ""
echo -n "Number : "
read su
echo ""

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su - $i`
l=`expr $su - 1 + $i`
while [ $j -le $l ]
do
if [ $j -le $k ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`
done

i=`expr $i + 1`
echo ""
done

i=1
while [ $i -le $su ]
do
j=1
k=`expr $su \* 2 - $i - 1`
while [ $j -le $k ]
do
if [ $j -le $i ]
then
echo -n " "
else
echo -n "*"
fi
j=`expr $j + 1`

done

i=`expr $i + 1`
echo ""
done
echo ""

출력 :
Number : 10

*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*

2009. 5. 20.

grep, egrep, fgrep

grep, egrep, fgrep

grep(egrep, fgrep) [option] “pattern” filename



-i : 대소문자 구별을 하지 않는다.
-v : pattern 을 포함하지 않는 행만 출력한다.
-n : 행번호를 출력한다.
-l : 파일명만 출력한다.
-c : 패턴과 일치하는 라인의 갯수만 보여준다.



grep : 강력한 패턴 매칭 템플릿을 정의하기 위해 "정규 표현식"을 사용할 수 있다.
egrep [옵션] "패턴|패턴|..." [대상파일들] : 확장된 정규 표현식을 사용하며, 찾아낼 패턴을 여러개 지정할 수 있다. '|'기호는 불린 연산자 "OR"에 해당하므로, 정해진 패턴들에 포함되는 모든 라인을 보여준다.

fgrep [옵션] 패턴 [대상파일들] : 패턴과 정확히 일치하는 것만을 찾아 준다.



많은 시스템 관리 명령들과 파이프(pipe)를 이용해서 사용할 수 있다. 흔히 사용하는 것은 프로세스 확인이다.
$ ps aux | grep httpd
위와 같이 현재 시스템의 프로세스중 httpd 만 검색해 낸다.
만일 pipe 를 이용하여 검색할경우 검색 패턴이 한개 이상일 경우엔 egrep을 이용하여 검색할수 있다.

$ ps aux | egrep 'httpd|mysql'


$ grep -v "^[ ^I]*$" 파일명 //공백을 제거한 파일 내용 살펴보기
$ grep -v "^#*$" 파일명 //주석을 제거한 파일 내용 살펴보기

만일 현재 디렉토리와 그 하위 디렉토리까지 grep 패턴 검색을 하고자 할땐 find 명령을 이용하면 된다.
$ find . -exec grep "pattern" {} \;

2009. 4. 22.

VIM 이미 읽은 파일의 인코딩 변경하기

출처 : http://kldp.org/node/32987


VIM 이미 읽은 파일의 인코딩 변경하기 (charset encoding)
The image verification code you entered is incorrect.
kwon37xi 씀 (금, 2004/04/02 - 10:19am) 강좌
요즌 페도라에서 UTF-8 로 사용하다보니 한글 인코딩 문제에 자주 부딪힙니다.
특히 텍스트 파일 읽을 때 짜증이 이만저만이 아닙니다.

암튼, VIM 에서 파일을 읽어보니 문자 인코딩이 깨져 있을 때, 다음과 같이 하면 곧바로 인코딩을 바꿔서 화면에 표시합니다.

:e ++enc=euc-kr

이 상태에서... 파일의 인코딩을 바꿔서 저장하려면

:set fileencoding=utf-8

한뒤에 :w 해보세요. euc-kr 이던 파일이 utf-8 로 저장됩니다.

MS949 를 시도해봤는데 안되더군요. UHC 로 해야 MS-Windows 의 확장한글코드가 되는것 같습니다. - 맞는건가요? 확실치 않아서리...

리눅스 환경에서 한글 설정하는 방법

[Ctrl + Alt + T] 키를 눌러 터미널을 엽니다.


언어 환경설정 파일을 수정합니다.
참고1 : http://wiki.eeeuser.com/howto:changelanguage
참고2 : http://www.chitsol.com/455
참고3 : http://debianusers.org/DebianWiki/wiki.php/%C3%CA%BA%B8%B0%A3%B4%DC%C7%D1%B1%DB%B0%A1%C0%CC%B5%E5


로케일 설정을 담당하는 /etc/locale.gen 을 수정합니다.

sudo vi /etc/locale.gen
ko_KR.UTF-8 UTF-8

다음 명령을 실행합니다

sudo locale-gen
sudo update-locale LANG=ko_KR.UTF-8
-> 이 명령을 실행하면 /etc/default/locale 이라는 파일이 생성됩니다.


한글 입력기 설치를 위해 프로그램 설치 저장소를 추가합니다. 앞으로 다양한 응용프로그램 설치시에도 이용됩니다.

sudo vi /etc/apt/sources.list
deb http://xnv4.xandros.com/xs2.0/upkg-srv2 etch main contrib non-free
deb http://dccamirror.xandros.com/dccri/ dccri-3.0 main
deb http://www.geekconnection.org/ xandros4 main


아래의 명령을 순서대로 실행하여 입력기인 scim과 한글 폰트를 설치합니다.

wget http://download.tuxfamily.org/eeepcrepos/key.asc
sudo apt-key add key.asc
sudo apt-get update
sudo apt-get install scim scim-hangul scim-tables-ko ttf-alee ttf-unfonts

작업이 완료되면 재부팅을 합니다.



다중언어입력기 SCIM의 환경을 설정합니다.

오른쪽 아래의 트레이영역에서 입력기 아이콘(input method 라고 풍선도움말이 표시됨)에서 오른쪽 마우스를 클릭하여 SCIM 설정을 선택합니다.
[입력기엔진] -> [전체 설정] -> [설치된 입력기 서비스들:] 에서
[한국어] - [두벌식]과 [한자]만 선택하고(또는 각 개인이 사용하는 자판 형식) 나머지는 모두 선택을 해제 합니다.
설정을 적용하고 환경설정을 끝냅니다.

영어 입력 상태에서 한글 입력을 할려면 왼쪽 Ctrl + Space 키를 누르면 입력 상태가 전환됩니다. 반대의 경우도 같습니다.




** 콘솔에서 한글이 표시 되지 않고 영어가 표시되게 설정하는 방법입니다.
참고1 : http://kldp.org/node/78712
참고2 : http://safari.oreilly.com/0596526784/using_initialization_files_correctly

vi .bashrc (만약 전체 사용자에게 동일하기 적용할려면 /etc/bash.bashrc 파일을 맨 끝에 아래 내용을 추가하면 됩니다.)
export LANGUAGE="ko_KR:ko:en_GB:en"
export LC_MESSAGES="POSIX"
export LC_ALL=""

locate 명령으로 설정된 값을 확인합니다.

LANG=ko_KR.UTF-8
LC_CTYPE="ko_KR.UTF-8"
LC_NUMERIC="ko_KR.UTF-8"
LC_TIME="ko_KR.UTF-8"
LC_COLLATE="ko_KR.UTF-8"
LC_MONETARY="ko_KR.UTF-8"
LC_MESSAGES=POSIX
LC_PAPER="ko_KR.UTF-8"
LC_NAME="ko_KR.UTF-8"
LC_ADDRESS="ko_KR.UTF-8"
LC_TELEPHONE="ko_KR.UTF-8"
LC_MEASUREMENT="ko_KR.UTF-8"
LC_IDENTIFICATION="ko_KR.UTF-8"
LC_ALL=

공유기를 사용할때 IP확인

http://www.whatismyip.com/에 접속하면 접속한 PC의 실제 IP를 표시해준다.

VNC 서버 설정

1. VNC 서버 설치



yum install vnc-server

service vncserver start





2. 설정

/etc/sysconfig/vncserver



* root로 원격에서 접속가능하게 설정하기

VNCSERVERS="2:root"
VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -nohttpd"






* 패스워드 설정

vncpasswd 명령을 실행





* 그놈으로 VNC 원격 열기

~/.vnc/xstartup 수정



#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &

export GTK_IM_MODULE=scim <- 한글 입력을 위해
gnome-session & <- 그놈 데스크탑 실행





3. 클라이언트 접속

UltraVNC를 설치하여 UltraVNC Viewer 를 실행

VNC Server : 에 www.junix.pe.kr:2 를 입력하고 vncpasswd에서 지정한 암호 입력

logwatch 7.2.1 on fc5

1. 설치
yum install logwatch


2. 설정
vi /etc/logwatch/conf/logwatch.conf

########################################################
# This was written and is maintained by:
# Kirk Bauer
#
# Please send all comments, suggestions, bug reports,
# etc, to kirk@kaybee.org.
#
########################################################

# NOTE:
# All these options are the defaults if you run logwatch with no
# command-line arguments. You can override all of these on the
# command-line.

# You can put comments anywhere you want to. They are effective for the
# rest of the line.

# this is in the format of = . Whitespace at the beginning
# and end of the lines is removed. Whitespace before and after the = sign
# is removed. Everything is case *insensitive*.

# Yes = True = On = 1
# No = False = Off = 0

# Default Log Directory
# All log-files are assumed to be given relative to this directory.
LogDir = /var/log

# You can override the default temp directory (/tmp) here
TmpDir = /var/cache/logwatch

# Default person to mail reports to. Can be a local account or a
# complete email address.
MailTo = jongjun 보고서를 받을 메일 계정
# Default person to mail reports from. Can be a local account or a
# complete email address.
MailFrom = Logwatch

# If set to 'Yes', the report will be sent to stdout instead of being
# mailed to above person.
Print = Yes

# if set, the results will be saved in instead of mailed
# or displayed.
#Save = /tmp/logwatch

# Use archives? If set to 'Yes', the archives of logfiles
# (i.e. /var/log/messages.1 or /var/log/messages.1.gz) will
# be searched in addition to the /var/log/messages file.
# This usually will not do much if your range is set to just
# 'Yesterday' or 'Today'... it is probably best used with
# Archives = Yes
# Range = All

# The default time range for the report...
# The current choices are All, Today, Yesterday
Range = yesterday

# The default detail level for the report.
# This can either be Low, Med, High or a number.
# Low = 0
# Med = 5
# High = 10
Detail = Low


# The 'Service' option expects either the name of a filter
# (in /usr/share/logwatch/scripts/services/*) or 'All'.
# The default service(s) to report on. This should be left as All for
# most people.
Service = All
# You can also disable certain services (when specifying all)
Service = "-zz-network" # Prevents execution of zz-network service, which
# prints useful network configuration info.
# If you only cared about FTP messages, you could use these 2 lines
# instead of the above:
#Service = ftpd-messages # Processes ftpd messages in /var/log/messages
#Service = ftpd-xferlog # Processes ftpd messages in /var/log/xferlog
# Maybe you only wanted reports on PAM messages, then you would use:
#Service = pam_pwdb # PAM_pwdb messages - usually quite a bit
#Service = pam # General PAM messages... usually not many

# You can also choose to use the 'LogFile' option. This will cause
# logwatch to only analyze that one logfile.. for example:
#LogFile = messages
# will process /var/log/messages. This will run all the filters that
# process that logfile. This option is probably not too useful to
# most people. Setting 'Service' to 'All' above analyizes all LogFiles
# anyways...

#
# By default we assume that all Unix systems have sendmail or a sendmail-like system.
# The mailer code Prints a header with To: From: and Subject:.
# At this point you can change the mailer to any thing else that can handle that output
# stream. TODO test variables in the mailer string to see if the To/From/Subject can be set
# From here with out breaking anything. This would allow mail/mailx/nail etc..... -mgt
mailer = "sendmail -t"

#
# With this option set to 'Yes', only log entries for this particular host
# (as returned by 'hostname' command) will be processed. The hostname
# can also be overridden on the commandline (with --hostname option). This
# can allow a log host to process only its own logs, or Logwatch can be
# run once per host included in the logfiles.
#
# The default is to report on all log entries, regardless of its source host.
# Note that some logfiles do not include host information and will not be
# influenced by this setting.
#
#HostLimit = Yes

# vi: shiftwidth=3 tabstop=3 et


3. 보고서 작성
/usr/sbin/logwatch 실행

4. 확인
Logwatch로 부터 jongjun으로 온 메일 확인

swatch 사용

로그 감시툴을 swatch를 사용해 보았다.

yum install swatch


테스트용 설정파일 설정
vi ~/.swatchrc
watchfor /.*/
echo
mail=jongjun@junix.pe.kr, subject=----PLEASE INVESTIGATE SSH ACCESS----


실행
swatch --config-file=/root/.swatchrc --tail-file=/var/log/secure


동작 확인
ssh를 하나 더 열어서 로그인 실패 상황을 만들고 실패에 대한 보고 메일이 왔는지 확인한다.
제목: ----PLEASE INVESTIGATE SSH ACCESS----
내용:
Aug 29 23:44:45 junix sshd[1431]: Invalid user hjhkj from 222.117.139.98


man swatch를 참조하여 추가 설정한다.
추가할 내용 : 오류별로 구분, 메일 통보 시간 조절, 다양한 로그 감시

서버 모니터링 도구들

phpsysinfo - 웹에서 시스템 자원 모니터링
http://phpsysinfo.sourceforge.net/

iptables stat
http://www.phildev.net/iptstate/

htop
http://htop.sourceforge.net/

apache top
http://www.fr3nd.net/projects/apache-top/

iptraf
http://iptraf.seul.org/