Skip to content

Instantly share code, notes, and snippets.

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