2013-12-19 3 views
3

SSH를 통해 CPU 사용률을 검색하고 "top"명령을 시도했지만이를 허용하지 않습니다.SSH를 통한 CPU 사용량 얻기

나는이 코드

$connection = ssh2_connect("IP", PORT); 
ssh2_auth_password($connection, "root", "PASS"); 
$stream = ssh2_exec($connection, "top"); 
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); 

// Enable blocking for both streams 
stream_set_blocking($errorStream, true); 
stream_set_blocking($stream, true); 

// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing 
echo "Output: " . stream_get_contents($stream); 
echo "Error: " . stream_get_contents($errorStream); 

// Close the streams   
fclose($errorStream); 
fclose($stream); 

을 시도하지만 그 때마다 나에게 오류 줄에 CentOS 6

을 사용하고 있습니다 : 출력 : 오류 : TERM 환경 변수 설정되지 않았습니다.

나는 PHP를 사용하고 있습니다.

+2

당신은 아마 어떤 수단 "그것은 나를 못하게"에 대한 몇 가지 세부 사항을 제공해야합니다 ... 오류 메시지 또는 무언가처럼 ... – TypeIA

+0

프로그래밍 관련이 있습니까? 아니면 일반적인 OS 질문입니까? –

+0

이것이 일반적인 운영 체제 질문이고 프로그래밍과 직접 관련이없는 경우 수퍼 유저 또는 서버 오류와 같은 다른 스택 Exchange 사이트에 더 적합합니다. 해당 사이트의 목적을 검토하고 질문에 가장 적합한 것을 결정하십시오. – dthree

답변

7

모두에게 감사를 사용할 수 있지만 관리. 내가이 명령을했다 :

top -b -n 10 -d.2 | grep 'Cpu' | awk 'NR==3{ print($2)}' 
0

당신은

top -n 1 
mpstat 
iostat 
0

phpseclib, a pure PHP SSH implementation을, 그것으로 top in PHP을 수행하는 방법의 논의 멋진 있습니다

http://phpseclib.sourceforge.net/ssh/pty.html#top

즉. 당신은 PTY가 필요합니다.

$ssh->enablePTY(); 
$ssh->exec('top'); 
$ssh->setTimeout(5); 
$ansi->appendString($ssh->read()); 
echo $ansi->getScreen(); 

너무 대화 형 모드로 작업을 수행 할 수 있습니다 출력이 ANSI 이스케이프 코드있을 것이라는 점을

$ansi->appendString($ssh->read('[email protected]:~$')); 
$ssh->write("top\n"); 
$ssh->setTimeout(5); 
$ansi->appendString($ssh->read()); 
echo $ansi->getScreen(); 

참고. 여기가 ANSI 이스케이프 코드는 HTML로 렌더링하는 경우처럼 보일 것입니다 무엇의 예

http://phpseclib.sourceforge.net/ssh/examples.html#top

+0

감사합니다. 하지만 난 관리 :) – user3120926