Last active
August 31, 2021 12:55
-
-
Save muenchow/3b2bc8c4ea46ebdfa1222d01eaf5b3dc to your computer and use it in GitHub Desktop.
TeamSpeak SSH-Query Example using Python
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
from paramiko import SSHClient, AutoAddPolicy | |
from sys import exit | |
def connect(host, port, username, password): | |
ssh_client = SSHClient() | |
ssh_client.set_missing_host_key_policy(AutoAddPolicy()) | |
ssh_client.connect(host, port=port, username=username, password=password) | |
return ssh_client.invoke_shell('raw') | |
def write_line(channel, line): | |
channel.send(line + '\n') | |
def read_line(channel): | |
if not hasattr(channel, 'line_buffer'): | |
channel.line_buffer = "" | |
while True: | |
end_of_line = channel.line_buffer.find('\n\r') | |
if not end_of_line == -1: | |
line = channel.line_buffer[0:end_of_line] | |
channel.line_buffer = channel.line_buffer[end_of_line+2:] | |
return line | |
channel.line_buffer += channel.recv(4096) | |
def execute(channel, command): | |
result = [] | |
write_line(channel, command) | |
while True: | |
line = read_line(channel) | |
result.append(line) | |
if line.startswith('error'): | |
return result | |
if __name__ == '__main__': | |
channel = connect('localhost', 10022, 'serveradmin', 'secret') | |
header = read_line(channel) | |
if not header == 'TS3': | |
exit('Not a TS3-Server...') | |
motd = read_line(channel) | |
print('Executing \'whoami\'...') | |
result = execute(channel, 'whoami') | |
print(result) | |
execute(channel, 'quit') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment