Last active
July 28, 2018 12:00
-
-
Save muenchow/61cbb225602bba78f952c0e5751d798e to your computer and use it in GitHub Desktop.
TeamSpeak SSH-Query Example using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function connect($host, $port, $username, $password) | |
{ | |
$connection = ssh2_connect($host, $port); | |
if (!$connection) | |
die('Connection failed'); | |
if (!ssh2_auth_password($connection, $username, $password)) | |
die('Authentication Failed...'); | |
$stream = ssh2_shell($connection, 'raw'); | |
if (!$stream) | |
die('Opening Shell failed'); | |
stream_set_blocking($stream, true); | |
return $stream; | |
} | |
function read_line($stream) | |
{ | |
return stream_get_line($stream, PHP_INT_MAX, "\n\r"); | |
} | |
function write_line($stream, $line) | |
{ | |
fwrite($stream, $line . "\n"); | |
} | |
function execute($stream, $command) | |
{ | |
$result = array(); | |
write_line($stream, $command); | |
do { | |
$line = read_line($stream); | |
array_push($result, $line); | |
} while ($line && substr($line, 0, 5) != "error"); | |
return $result; | |
} | |
$stream = connect('localhost', 10022, 'serveradmin', 'secret'); | |
$header = read_line($stream); | |
if ($header != "TS3") | |
die('Not a TS3-Server...'); | |
$motd = read_line($stream); | |
print("Executing 'whoami'...\n"); | |
$result = execute($stream, 'whoami'); | |
print_r($result); | |
execute($stream, 'quit'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment