Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TeamSpeak SSH-Query Example using PHP
<?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
You can’t perform that action at this time.