Translate

2016년 7월 29일 금요일

[Linux] 파일내용 확인 시 vi 보다는 less 를 사용하자





리눅스/유닉스 환경에서 파일의 내용을 확인 시 vi 를 많이들 사용한다.

인터넷을 봐도 파일을 편집할 때 vi 를 이용해 편집하라고 많이 기술되어 있는 것도 vi 를 좀더 자주 사용하게되는 이유가 아닐까 싶다.


제목에서 vi 보다는 less 를 사용하자고 적은 이유는 less 가 vi 보다 기능이 많거나 더 좋아서 사용하자고 이야기하는게 아니다.
1004lucifer
vi 와 less 의 사용 용도가 다르기 때문이다.

vi : 파일 편집 시 사용
less : 파일 내용 확인 시 사용





vi 를 사용해도 괜찮지만 vi 가 가지는 불편함이 있다.


vi 를 이용해 파일을 열게되면 해당 파일의 용량만큼 vi 프로그램이 파일을 모두 읽어들여 메모리를 차지하게된다.
즉 3기가짜리 파일을 vi 로 열게되면 3기가를 모두 로드해서 메모리에 올리기 때문에 시간도 오래걸리고 OS의 메모리가 부족하게되어 디스크 스왑이 일어날 수 있게된다.




그에반해 less의 경우에는


랜덤엑세스 방식으로 파일을 읽기 때문에 명령어 입력 시 파일이 즉시 열리게 된다.
그리고 메모리 사용량이 높지도 않다.





사용방법은 vi 와 비슷하며 아래와 같이 입력 하면 해당 파일이 열린다.


 ~]$ less 파일명




내가 자주 사용하는 커맨드는 아래와 같다. (파일을 연상태에서 입력)
대부분 vi 와 커맨드가 같다.
1004lucifer

 -N : 라인넘버를 보여줌
 -n : 라인넘버를 숨김
 f : 다음 페이지 이동
 b : 이전 페이지 이동
 d : 다음 절반 페이지 이동
 u : 이전 절반 페이지 이동
 /문자열 : 해당 문자열 검색
 n : 문자열 다음 검색
 N : 문자열 이전 검색
 숫자 : 해당 라인으로 이동





어떤 특정 문자열을 뽑기 위해서는 cat 과 grep 명령어를 이용해 뽑아내긴 하지만 특정 문자열의 앞뒤를 대략적으로 살펴보기 위해서는 파일을 열어서 봐야한다.

그런경우에 나는 빠르고 실용적인 less 명령어를 추천한다.  :)



2016년 7월 27일 수요일

[SQL Developer] SQL 파일을 빠르게 수행하는 방법





최근에 30만건이 넘는 insert Query 를 돌려야 하는 상황이 발생을 했다.


옛날에는 몇천건 되는 sql 파일을 그냥 열고서 F5를 누르고 구동되면서 하염없이 떨어지는 쿼리들을 보곤 했는데..
1004lucifer
아무래도 sql파일을 통채로 돌리는게 있지 않을까 싶어 찾아보니 있었다.





1. 현재의 세션이 autocommit 상태인지 체크를 한다.


 - show autocommit;  <== 현재의 상태를 체크한다.
 - set autocommit off; <== autocommit off 설정
 - set autocommit on; <== autocommit on 설정

autocommit on 상태인경우 프로그램이 종료되거나 PC가 종료가 되더라도 롤백이 되지 않지만 속도가 많이 느려진다.
가급적 autocommit off 상태로 작업을 권장한다.







2. @path\scriptfile.sql 형식으로 sql 파일의 경로를 입력 후 실행한다.
1004lucifer

ex) sql 파일이 c드라이브의 test.sql 파일인경우
 - @c:\test.sql


ex) 테스트용으로 사용한 test.sql 파일의 내용은 아래와 같다.
====================
select 1 from dual;
select 1 from dual;
select 1 from dual;

commit;
====================

시작 시 autocommit off 로 시작하는 환경에 파일 마지막에 commit; 을 붙여주지 않으면 작업자가 직접 'commit' 버튼을 눌러줘야 한다.
commit 버튼을 누르지 않으면 해당 테이블에 락이 걸리므로 꼭 파일 마지막에 commit; 문자열을 집어넣어준다.







장점

SQL Developer 에서 sql파일을 열어서 F5로 해당 쿼리들을 실행 시 프로그램이 메모리도 많이 잡아먹을 뿐 아니라 점점 시간이 지날수록 작업시간이 느려진다.

위의 방법으로 작업 시 원격의 DB서버에 30만건 이상의 insert 구문을 수행하는데 약 100분 정도가 걸렸다.   :)





참고
http://stackoverflow.com/questions/31560367/how-to-run-sql-file-in-oracle-sql-developer-tool-to-import-database


2016년 7월 11일 월요일

[Web] UTF-8 의 BOM 유무에 따른 한글깨짐 이슈




BOM에 대한 기본적인 정보는 위키백과(링크)를 참조하기 바란다.
(개인블로그 이긴 하지만 링크 의 경우에도 잘 정리되어 있다.)


예제파일
1. BOM file: 다운로드 링크
2. without BOM file: 다운로드 링크



도대체 BOM(Byte Order Mark) 가 어떠한 문제를 일으키는 걸까?
1004lucifer
BOM이 있는 파일은 일반적으로 문제가 되는 부분은 Linux/Unix 운영환경에서 많이 문제가 된다.
(AIX(IBM) 서버 환경에서 XML 파일에 BOM이 붙어있어서 애플리케이션이 실행이 되지 않았던 경험이 아직도 기억이 난다. 알고나면 별거 아니지만 참 고생하면서 찾았었다.)


이런 이기종 시스템에 문제가 생기지 않게 하기위해서는 BOM을 붙이지 않는게 일반적이었고 BOM을 붙이는 것에 대해서 직접적인 장점을 본적이 없었다.



최근에 BOM의 장점과 웹에 미치는 영향을 보게되어 이렇게 글을 작성한다.


증상

UTF-8 인코딩으로 된 JSON 파일을 웹서버(Apache, WebtoB etc..) 에 올려서 서비스를 하게 되었는데 웹에서 특정 파일의 한글문자가 깨지는 증상이 발생을 했다.
한글이 정상적으로 표시되는 파일은 BOM이 있었고, 한글이 깨지는 파일은 BOM이 없었다.




원인

아래의 두가지 원인이 있다. (첫번째는 BOM에 대한 직접적인 원인이 아니다.)
1. 서버에서 Response 할 시 헤더의 'Content-Type' 속성에 charset 값을 넣어주지 않는다.
2. BOM이 없으며 'Content-Type' 속성에 charset 값이 없는경우 UTF-8 인코딩으로 특별히 지정해 주지 않으면 클라이언트의 로케일(Locale) 설정을 따라간다.








원인에 대해서 분석과 해결방안을 확인해 보겠다.
(테스트는 apache-tomcat 7.0 으로 작업을 했다.)


1. 서버에서 Response 할 시 헤더의 'Content-Type' 값에 charset 값을 넣어주지 않는다.

분석
1004lucifer

1. JSP 파일의 경우 상단에
<%@ page language="java" contentType="application/json; charset=utf-8" pageEncoding="utf-8"%>
문자열을 입력 시 별다른 서버 작업 없이
Response 헤더에 Content-Type: application/json;charset=utf-8 값으로 내려오며 한글이 정상적으로 보여졌다.

2. txt, json 등등 jsp 가 아닌 다른 파일에 대해서는 위의 작업을 할 수 없다.



해결방안 (jsp 가 아닌 파일에 대해서)


1. WEB에서 서비스 하는경우 WEB 애플리케이션에 따라 설정해 준다.
 ex) Apache - .htaccess 파일 설정 (아래의 3개의 설정중에 취행대로 설정해 주면 된다.)
1. AddDefaultCharset utf-8
2. AddType 'application/json; charset=UTF-8' .json
3. AddCharset utf8 .json
apache 설정: 링크1, 링크2


2. WAS에서 서비스 하는경우 web.xml 파일에 아래와 같이 해당 확장자에 대해서 mime-type 을 지정해 준다.
<mime-mapping>
        <extension>json</extension>
        <mime-type>application/json; charset=utf-8</mime-type>
</mime-mapping>


PS.
WAS에서 서비스 하는경우 web.xml 을 사용하지 않는경우 해결을 할 수 없을 수 있다.
(WAS에서 web.xml 을 사용하지 않는경우는 여러가지 경우가 있을 수 있는데 Web 서버 없이 WAS 단독 구성으로 되어있는 상황에서 특정 디렉토리를 alias 걸어서 사용하면 web.xml 이 없는 상황이 되어버린다.)
  ex) Tomcat 에서는 web.xml 을 사용하지 않으면 방법이 없다.






2. BOM이 없으며'Content-Type' 속성에 charset 값이 없는경우 UTF-8 인코딩으로 특별히 지정해 주지 않으면 클라이언트의 로케일(Locale) 설정을 따라간다.

분석
1004lucifer

1. Windows OS에서 'Content-Type' 속성에 charset 값이 없을 때 BOM 있는 파일의 경우 한글이 정상적으로 표시되며, BOM 없는 파일의 경우 한글이 깨졌다.
(UTF-8 Locale을 사용하는 Ubuntu에서는 둘다 한글이 정상적으로 보여졌다.)

BOM 있는 파일






BOM 없는 파일






Android 핸드폰의 경우에도 BOM 없는 파일의 한글이 깨졌으나 아래와 같이 설정을 변경 후 새로고침을 하니 정상적으로 보여졌다.
* 브라우저 설정 => 고급 => 텍스트인코딩
(핸드폰에 따라 설정 방법이 조금 다를 수 있다.)







해결방안


1. 위와 같이 이미 만들어진 프로그램(브라우저 같은..)의 경우에는 다음과 같이 할 수 있다.
  1) 'Content-Type' 속성에 charset=utf-8 이 나올 수 있도록 서버 설정을 변경한다.
  2) 파일에 BOM 을 붙인다. (권장하지 않음.)

2. 프로그램을 직접 제작/수정 하는경우 HTTP프로토콜을 이용해 해당 Resource를 받아올 시 UTF-8 인코딩을 지정한다.
  1) HttpClient (apache) 라이브러리
    - 별다른 설정 없이 기본으로 UTF-8을 지원하는지 한글이 깨지지 않는다.
  2) Spring RestTemplate 라이브러리
    - StringHttpMessageConverter 객체를 아래와 같이 생성 해야 한다. 그렇지 않은경우 구동되는 시스템 Locale을 따라간다.
new StringHttpMessageConverter(Charset.forName("UTF-8"))






PS.
BOM에 대한 고찰

RFC4627(링크) 문서에 보면 JSON Text 는 유니코드로 인코딩 되어야 하며 Default 로 UTF-8 을 사용한다고 정의되어 있다.
그렇다는건 현재 사용되어지는 브라우저 또한 'Content-Type' 값이 'application/json' 으로만 지정되어 있어도 알아서 UTF-8 로 인코딩하여 정상적으로 보여줘야 한다고 생각한다.

현재 사용하는 Chrome 51.0 버전과 Android 5.0.2 단말기의 브라우저에서 알아서 UTF-8 로 파싱해서 보여주고 있지 않고 있다.





참고
http://blog.wystan.net/2007/08/18/bom-byte-order-mark-problem
http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type
http://serverfault.com/questions/581760/how-do-i-set-proper-headers-for-json-in-apache
http://www.ietf.org/rfc/rfc4627.txt


2016년 7월 10일 일요일

[Ubuntu] WireShark - The capture session could not be initiated on interface 문제




OS: Ubuntu 15.10 Gnome



문제

WireShark 를 설치 후 인터페이스 선택하고 패킷 캡처를 하려는데 아래와 같은 문구가 나타나며 패킷캡처가 실행되지 않았다.
(root 권한으로 실행 시에는 문제가 없으며 일반계정으로 구동 시 문제가 발생한다.)

우분투,와이어샤크, 1004lucifer

The capture session could not be initiated on interface 'INTERFACE_NAME' (You don't have permission to capture on that device).

please check to make sure you have sufficient permissions, and that you have the proper interface or pipe specified.





해결방법
우분투,와이어샤크, 1004lucifer
실행 시 관리자 권한이 없어서 그러니 sticky bit 를 부여한다.
('ls -l' 명령어로 확인 시 s 권한이 붙어야 한다.)

lucifer@lucifer-Vostro-V13:~$ sudo chmod +s /usr/bin/dumpcap
lucifer@lucifer-Vostro-V13:~$ ls -l /usr/bin/dumpcap
-rwsr-sr-- 1 root wireshark 85632  8월 16  2015 /usr/bin/dumpcap



위의 작업 후 wireshark를 다시 실행하면 정상적으로 패킷 캡쳐가 되는 것을 확인 할 수 있다.



[Ubuntu] 우분투에서 WireShark 설치 방법



OS: Ubuntu 15.10 Gnome


아래와 같이 WireShark 설치를 했다.
(마지막 명령어로 /usr/bin/wireshark 경로에 설치가 되었음을 확인할 수 있다.)
와이어샤크, 1004lucifer

lucifer@lucifer-Vostro-V13:~$ sudo apt-get install wireshark
[sudo] password for lucifer: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-image-4.2.0-16-generic linux-image-4.2.0-35-generic linux-image-4.2.0-36-generic linux-image-extra-4.2.0-16-generic
  linux-image-extra-4.2.0-35-generic linux-image-extra-4.2.0-36-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  geoip-database-extra libjs-openlayers libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark-common
Suggested packages:
  snmp-mibs-downloader wireshark-doc
The following NEW packages will be installed:
  geoip-database-extra libjs-openlayers libsmi2ldbl libwireshark-data libwireshark5 libwiretap4 libwsutil4 wireshark wireshark-common
0 upgraded, 9 newly installed, 0 to remove and 3 not upgraded.
Need to get 23.6 MB of archives.
After this operation, 112 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
WARNING: The following packages cannot be authenticated!
  libsmi2ldbl geoip-database-extra libjs-openlayers libwireshark-data libwsutil4 libwiretap4 libwireshark5 wireshark-common wireshark
Install these packages without verification? [y/N] y
Get:1 http://kr.archive.ubuntu.com/ubuntu/ wily/main libsmi2ldbl amd64 0.4.8+dfsg2-9ubuntu3 [100 kB]
Get:2 http://kr.archive.ubuntu.com/ubuntu/ wily/universe geoip-database-extra all 20150810-1 [9,680 kB]
Get:3 http://kr.archive.ubuntu.com/ubuntu/ wily/universe libjs-openlayers all 2.13.1+ds2-1 [675 kB]
Get:4 http://kr.archive.ubuntu.com/ubuntu/ wily/universe libwireshark-data all 1.12.7+g7fc8978-1 [813 kB]
Get:5 http://kr.archive.ubuntu.com/ubuntu/ wily/universe libwsutil4 amd64 1.12.7+g7fc8978-1 [52.9 kB]
Get:6 http://kr.archive.ubuntu.com/ubuntu/ wily/universe libwiretap4 amd64 1.12.7+g7fc8978-1 [142 kB]
Get:7 http://kr.archive.ubuntu.com/ubuntu/ wily/universe libwireshark5 amd64 1.12.7+g7fc8978-1 [11.3 MB]
Get:8 http://kr.archive.ubuntu.com/ubuntu/ wily/universe wireshark-common amd64 1.12.7+g7fc8978-1 [144 kB]
Get:9 http://kr.archive.ubuntu.com/ubuntu/ wily/universe wireshark amd64 1.12.7+g7fc8978-1 [728 kB]
Fetched 23.6 MB in 4s (5,478 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package libsmi2ldbl:amd64.
(Reading database ... 315073 files and directories currently installed.)
Preparing to unpack .../libsmi2ldbl_0.4.8+dfsg2-9ubuntu3_amd64.deb ...
Unpacking libsmi2ldbl:amd64 (0.4.8+dfsg2-9ubuntu3) ...
Selecting previously unselected package geoip-database-extra.
Preparing to unpack .../geoip-database-extra_20150810-1_all.deb ...
Unpacking geoip-database-extra (20150810-1) ...
Selecting previously unselected package libjs-openlayers.
Preparing to unpack .../libjs-openlayers_2.13.1+ds2-1_all.deb ...
Unpacking libjs-openlayers (2.13.1+ds2-1) ...
Selecting previously unselected package libwireshark-data.
Preparing to unpack .../libwireshark-data_1.12.7+g7fc8978-1_all.deb ...
Unpacking libwireshark-data (1.12.7+g7fc8978-1) ...
Selecting previously unselected package libwsutil4:amd64.
Preparing to unpack .../libwsutil4_1.12.7+g7fc8978-1_amd64.deb ...
Unpacking libwsutil4:amd64 (1.12.7+g7fc8978-1) ...
Selecting previously unselected package libwiretap4:amd64.
Preparing to unpack .../libwiretap4_1.12.7+g7fc8978-1_amd64.deb ...
Unpacking libwiretap4:amd64 (1.12.7+g7fc8978-1) ...
Selecting previously unselected package libwireshark5:amd64.
Preparing to unpack .../libwireshark5_1.12.7+g7fc8978-1_amd64.deb ...
Unpacking libwireshark5:amd64 (1.12.7+g7fc8978-1) ...
Selecting previously unselected package wireshark-common.
Preparing to unpack .../wireshark-common_1.12.7+g7fc8978-1_amd64.deb ...
Unpacking wireshark-common (1.12.7+g7fc8978-1) ...
Selecting previously unselected package wireshark.
Preparing to unpack .../wireshark_1.12.7+g7fc8978-1_amd64.deb ...
Unpacking wireshark (1.12.7+g7fc8978-1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Processing triggers for man-db (2.7.4-1) ...
Processing triggers for shared-mime-info (1.3-1) ...
Processing triggers for gnome-menus (3.13.3-6ubuntu1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu3) ...
Processing triggers for mime-support (3.58ubuntu1) ...
Setting up libsmi2ldbl:amd64 (0.4.8+dfsg2-9ubuntu3) ...
Setting up geoip-database-extra (20150810-1) ...
Setting up libjs-openlayers (2.13.1+ds2-1) ...
Setting up libwireshark-data (1.12.7+g7fc8978-1) ...
Setting up libwsutil4:amd64 (1.12.7+g7fc8978-1) ...
Setting up libwiretap4:amd64 (1.12.7+g7fc8978-1) ...
Setting up libwireshark5:amd64 (1.12.7+g7fc8978-1) ...
Setting up wireshark-common (1.12.7+g7fc8978-1) ...
Setting up wireshark (1.12.7+g7fc8978-1) ...
Processing triggers for libc-bin (2.21-0ubuntu4.3) ...
lucifer@lucifer-Vostro-V13:~$ type wireshark 
wireshark is /usr/bin/wireshark
lucifer@lucifer-Vostro-V13:~$






설치 중 아래와 같은 화면이 보이게 된다.


위의 화면에서 'Yes' 를 누르면 설치는 끝나게 되고 앞으로 아래의 명령어와 같이 root 권한으로 wireshark 를 구동할 수 있다.
와이어샤크, 1004lucifer
(가장 간편한 방법이며, 'No' 를 선택했더라도 별다른 설정없이 아래의 명령어로 root 권한으로 바로 실행이 가능하다.)
 $ sudo wireshark





만일 평소 root 권한이 아닌 평소 로그인 계정으로 실행하고 싶다면 'No'를 선택 후 아래와 같이 작업을 해줘야 한다.

설정방법
(YOUR_USER_NAME 부분은 본인이 접속해 있는 계정명)
$ sudo addgroup -system wireshark
$ sudo chown root:wireshark /usr/bin/dumpcap
$ sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap
$ sudo usermod -a -G wireshark YOUR_USER_NAME




나의 경우에는 아래와 같이 작업을 했다.


# wireshark 설치시에 자동으로 추가가 되어있었나 보다.
lucifer@lucifer-Vostro-V13:~$ sudo addgroup -system wireshark
addgroup: The group `wireshark' already exists as a system group. Exiting.
lucifer@lucifer-Vostro-V13:~$ cat /etc/group | tail -3
sambashare:x:130:lucifer
vboxusers:x:131:
wireshark:x:132:

# dumpcap 파일의 소유권도 설치 시 자동으로 변경해 준것 같다.
lucifer@lucifer-Vostro-V13:~$ ls -l /usr/bin/dumpcap
-rwxr-xr-- 1 root wireshark 85632  8월 16  2015 /usr/bin/dumpcap

# 이건 확인방법을 몰라서 그냥 한번 해줬다.
lucifer@lucifer-Vostro-V13:~$ sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap

# 내 계정(lucifer)을 wireshark 그룹에 추가한다.
lucifer@lucifer-Vostro-V13:~$ sudo usermod -a -G wireshark lucifer
lucifer@lucifer-Vostro-V13:~$ cat /etc/group | tail -3
sambashare:x:130:lucifer
vboxusers:x:131:
wireshark:x:132:lucifer

lucifer@lucifer-Vostro-V13:~$






PS.

위의 작업을 했는데도 사용자 계정으로 wireshark 를 구동 시 인터페이스가 인식되지 못했다.
(root 계정으로 실행 시 문제없이 인터페이스가 인식되었다.)

(Couldn't run /usr/bin/dumpcap in child process: Permission denied)


원인분석을 해봤다.

lucifer@lucifer-Vostro-V13:~$ 
lucifer@lucifer-Vostro-V13:~$ ls -l /usr/bin/dumpcap
-rwxr-xr-- 1 root wireshark 85632  8월 16  2015 /usr/bin/dumpcap
lucifer@lucifer-Vostro-V13:~$ 
lucifer@lucifer-Vostro-V13:~$ whoami
lucifer
lucifer@lucifer-Vostro-V13:~$ 
lucifer@lucifer-Vostro-V13:~$ cat /etc/group | tail -2
vboxusers:x:131:
wireshark:x:132:lucifer
lucifer@lucifer-Vostro-V13:~$ 
lucifer@lucifer-Vostro-V13:~$ /usr/bin/dumpcap
bash: /usr/bin/dumpcap: Permission denied
lucifer@lucifer-Vostro-V13:~$


위의 상황대로라면 내계정(lucifer)은 wireshark 그룹에 들어있으니 /usr/bin/dumpcap 을 실행시킬 권한이 있음에도 불구하고 권한이 없다고 답변한다.

환경설정을 다시 로드 하기도 해보고 이것 저것 해봐도 안되길래 재부팅을 해보니 아무일 없다는 듯이 정상적으로 인터페이스가 추가된 것을 확인 할 수 있다.

* 위의 작업이 끝나고 아래의 링크의 작업을 추가로 해줘야 한다.
[Ubuntu] WireShark - The capture session could not be initiated on interface 문제







마음의 소리 - 다음엔 이런작업 안하고 root로 실행해서 사용해야겠다.

참고 링크
https://ask.wireshark.org/questions/7523/ubuntu-machine-no-interfaces-listed
http://blog.munilive.com/ubuntu-14-04-wireshark-no-interfaces-available/