리눅스 cron 스케줄러에 등록된 프로그램에서 특정단어 찾기
스케줄러에 등록된 프로그램에서 특정 함수 사용하는것이 있을 경우 어느 프로그램에서 사용하는지 알고 싶어서 만들게 된 프로그램 입니다.
사용은 이렇게 했지만 반드시 그럴 필요는 없지요.
특정 약간 수정해서 특정 디렉토리에 있는것을 찾을 수도 있고. 웬만한것은 쉘스크립트로도 가능할듯.
// cron_php_word_search_functions.php
// 설명: crontab/스케줄러 파일들을 읽어서 php 파일 경로를 찾아
// 해당 php 파일 내용에 미리 지정한 단어들이 있는지 확인합니다.
/**
* 크론 파일들을 전체 스캔하고 결과 출력
* @param array $aSearchWords 찾을 단어 배열
* @param array|null $aScanPaths 사용자 지정 경로 배열 (없으면 기본값 사용)
* @return void
*/
function wordsearch_run(array $aSearchWords, array $aScanPaths = null): void
{
// 기본 검사할 크론 경로
$aDefaultPaths = [
'/var/spool/cron',
'/var/spool/cron/crontabs',
// '/etc/cron.d',
// '/etc/crontab',
// '/etc/cron.daily',
// '/etc/cron.hourly',
// '/etc/cron.weekly',
// '/etc/cron.monthly',
];
$aScanPaths = $aScanPaths ?? $aDefaultPaths;
$aCronFiles = [];
// 크론 파일 목록 수집
foreach ($aScanPaths as $sPath) {
if (is_dir($sPath)) {
$aItems = scandir($sPath);
foreach ($aItems as $sItem) {
if ($sItem === '.' || $sItem === '..') continue;
$sFull = rtrim($sPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $sItem;
if (is_file($sFull)) $aCronFiles[] = $sFull;
}
} elseif (is_file($sPath)) {
$aCronFiles[] = $sPath;
}
}
if (empty($aCronFiles)) {
echo "검사할 크론 파일이 없습니다. (경로 확인 필요)\n";
return;
}
// 각 크론 파일 처리
foreach ($aCronFiles as $sCronFile) {
echo "=================================================================\n";
echo "크론 파일: {$sCronFile}\n";
// 크론 파일 읽기
if (!is_file($sCronFile) || !is_readable($sCronFile)) {
echo " 오류: 파일을 읽을 수 없습니다 (권한 또는 파일 아님).\n";
continue;
}
$sCronContent = file_get_contents($sCronFile);
if ($sCronContent === false) {
echo " 오류: 파일 내용을 읽을 수 없습니다.\n";
continue;
}
$aLines = preg_split('/\R/u', $sCronContent);
$aPhpPaths = [];
// 크론 라인에서 PHP 경로 추출
foreach ($aLines as $sLine) {
$sLine = trim($sLine);
if ($sLine === '' || $sLine[0] === '#') continue;
$sPattern = '/(\/[^\s;|&<>]+?\.php)(?=\s|$)/';
if (preg_match_all($sPattern, $sLine, $aMatches)) {
foreach ($aMatches[1] as $sMatch) {
if (!in_array($sMatch, $aPhpPaths)) $aPhpPaths[] = $sMatch;
}
}
}
if (empty($aPhpPaths)) {
echo " -> 이 크론 파일에서 찾은 php 경로가 없습니다.\n";
continue;
}
// 각 PHP 파일 검사
foreach ($aPhpPaths as $sPhpPath) {
echo " PHP 경로: {$sPhpPath}\n";
$bExists = is_file($sPhpPath);
echo " 존재: " . ($bExists ? '예' : '아니오') . "\n";
if (!$bExists) {
echo " 발견된 단어: 없음\n";
echo PHP_EOL;
continue;
}
$bReadable = is_readable($sPhpPath);
echo " 읽기 가능: " . ($bReadable ? '예' : '아니오') . "\n";
if (!$bReadable) {
echo " 발견된 단어: 없음\n";
echo PHP_EOL;
continue;
}
// PHP 파일에서 단어 검색
$sPhpContent = file_get_contents($sPhpPath);
$aFoundWords = [];
$aWordLines = [];
foreach ($aSearchWords as $sWord) {
$iCount = substr_count($sPhpContent, $sWord);
if ($iCount > 0) {
$aContentLines = preg_split('/\R/u', $sPhpContent);
$aLinesFound = [];
foreach ($aContentLines as $i => $sContentLine) {
if (stripos($sContentLine, $sWord) !== false) { // 대소문자 무시 검색
$aLinesFound[] = $i + 1;
}
}
$aFoundWords[$sWord] = $iCount;
$aWordLines[$sWord] = $aLinesFound;
}
}
// 결과 출력
if (!empty($aFoundWords)) {
echo " 발견된 단어: \n";
foreach ($aFoundWords as $sWord => $iCount) {
$aLines = $aWordLines[$sWord] ?? [];
$sLines = implode(', ', $aLines);
echo " - {$sWord} : {$iCount}회 (라인: {$sLines})\n";
}
} else {
echo " 발견된 단어: 없음\n";
}
echo PHP_EOL;
}
}
echo "\n완료.\n";
}
// ---------------- 실행 예제 ----------------
// 사용법: wordsearch_run(['https', 'twssp']);
// 또는 경로를 지정하여: wordsearch_run(['https', 'twssp'], ['/custom/path']);
// 예제 실행
wordsearch_run(['https', 'twssp']);
