백틱(backticks)이란 유닉스의 터미널이나 윈도우의 명령프롬프트에서 사용되는 쉘 명령을 수행하는 것으로 윈도우에서의 dir 명령이나 유닉스의 ls 와 같은 명령어를 수행할 수 있습니다.

<?php 
 // 윈도우에서 dir 명령어를 수행합니다. 
 echo nl2br(`dir`); 

 // 리눅스에서 ls 명령어를 수행합니다. 
 echo nl2br(`ls -al`); 

 /*
 수행 결과

 total 164
 drwxr-xrwx 5 root user 4096 Jun 5 15:13 .
 drwx---rwx 15 root user 4096 May 5 21:58 ..
 -rw-r--r-- 1 root user 44850 May 10 18:14 Chart.js
 -rw-r--r-- 1 root user 72 Jun 5 21:08 adro.php
 drwxr-xr-x 5 root user 4096 May 10 15:19 chart
 -rw-r--r-- 1 root user 2173 May 17 19:44 chart.php
 -rw-r--r-- 1 root user 9650 May 10 18:14 effects.js
 -rw-r--r-- 1 root user 41209 May 10 18:14 excanvas.js
 -rw-r--r-- 1 root user 16133 May 11 10:33 get_Browser.php
 -rwxr--rwx 1 root user 43 Apr 14 13:06 highlight_string.php
 -rw-r--r-- 1 root user 4088 May 11 10:33 ipaddr.getreal.php
 drwxrwxrwx 2 root user 4096 Jun 5 07:26 logs
 -rw-r--r-- 1 root user 2683 May 17 20:40 style.css
 -rw-r--r-- 1 root user 4999 May 11 10:33 temp.php
 */
 ?>

다음은 서버와 클라이언트간 ping 테스트를 합니다. 윈도우에서는 -n 옵션을 유닉스에서는 -c 옵션을 사용하며 1회만 실행하려고 합니다.

<?php 
 // 윈도우에서 ping 테스합니다. 
 echo nl2br(`ping -n 1 habonyphp.com`); 

 // 리눅스에서 ping 테스합니다. 

 $addr = $_SERVER['REMOTE_ADDR'];

 echo nl2br(`ping -c 1 $addr`); 

 /*
 수행 결과

 Pinging habonyphp.com [180.70.134.239] with 32 bytes of data:

 Request timed out.

 Ping statistics for 180.70.134.239:
 Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
 */
 ?>

쉘 기능은 shell_exec 함수와 동일합니다. 그러므로 다른 사용자가 명령을 내리기전에 escapeshellarg()나 escapeshellcmd()를 이용하여, 위험한 명령을 실행하지 못하게 할 필요가 있습니다.

<?php 
 $e = 'mydir'; 
 $output = escapeshellcmd($e); 
 `mkdir ${output}`; // mydir 이라는 폴더를 생성합니다. 
 `chmod 0707 ${output}`; 
 ?>


0 댓글