Created
September 9, 2016 01:16
-
-
Save nenodias/7c16fa0f9cce19056f9f7802d8a66541 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # *-* coding:utf-8 *-* | |
| import socket | |
| import subprocess | |
| import sys | |
| import os | |
| reload(sys) | |
| sys.setdefaultencoding('utf8') | |
| porta = 2222 | |
| s = None | |
| clientes = [] | |
| try: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| s.bind(('0.0.0.0',porta)) | |
| s.listen(3) | |
| print('Servidor ouvindo porta %d' %(porta)) | |
| while True: | |
| try: | |
| print('Esperando Cliente') | |
| conn, addr = s.accept() | |
| clientes.append(conn) | |
| except: | |
| continue | |
| print('Cliente conectado %s ' %( str(addr) ) ) | |
| autenticado = False | |
| tentativas = 0 | |
| mensagem = '' | |
| while True: | |
| try: | |
| if not autenticado: | |
| conn.send('Login:') | |
| login = conn.recv(2048) | |
| print(login) | |
| conn.send('\nSenha:') | |
| senha = conn.recv(2048) | |
| print(senha) | |
| if login == 'root' and senha == '123': | |
| autenticado = True | |
| comando = True | |
| mensagem = '>>>' | |
| else: | |
| tentativas += 1 | |
| conn.send('Tentativa invalida!\n%d tentativas restantes' %( 3 - tentativas) ) | |
| if tentativas == 4: | |
| conn.send('\nEsgotado numero de tentativas') | |
| conn.close() | |
| break | |
| else: | |
| if comando: | |
| conn.send(mensagem) | |
| comando = conn.recv(2048) | |
| print(comando) | |
| if comando != 'exit' and comando: | |
| try: | |
| processo = subprocess.Popen(comando, stdout=subprocess.PIPE, shell=True ) | |
| retorno , erro = processo.communicate() | |
| if erro: | |
| mensagem = erro +'\n>>>' | |
| else: | |
| mensagem = retorno +'\n>>>' | |
| except Exception as ex: | |
| conn.send(str(ex)) | |
| elif comando == 'exit': | |
| conn.send('\n\nFlws o/') | |
| raise Exception('Logout') | |
| except: | |
| try: | |
| conn.send('exit') | |
| except: | |
| pass | |
| conn.close() | |
| clientes.remove(conn) | |
| break | |
| finally: | |
| for c in clientes: | |
| c.close() | |
| s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment