Analog ssh remote command execution

The server must meet at least three points: 
1. Bind a fixed ip and Port
2. external services have been stable operation
3. be able to support concurrent
from socket import *

client = socket(AF_INET, SOCK_STREAM)
client.connect(('127.0.0.1', 8081))

# 通信循环
while True:
    cmd=input('>>: ').strip()
    if len(cmd) == 0:continue
    client.send(cmd.encode('utf-8'))
    cmd_res=client.recv(1024000)
    print(cmd_res.decode('gbk'))

client.close()
Client
The server must meet at least three things:
 1 Binding a fixed ip and Port
 2 has been to provide services, and stable operation.
 3 now supports concurrent.
 From socket Import *
 Import subprocess 

Server = socket (AF_INET, SOCK_STREAM) 
server.bind ( ( ' 127.0.0.1 ' , 8081 )) 
server.listen ( . 5 ) 

# links circulating 
the while True: 
    Conn, client_addr = server.accept ()
     Print (client_addr) 

    # communications cycle 
    the while True:
         the try : 
            cmd= conn.recv(1024) #cmd=b'dir'
            if len(cmd) == 0: break  # 针对linux系统
            #subprocess.Popen 执行系统命令
            obj=subprocess.Popen(cmd.decode('utf-8'),
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE
                             )
            stdout=obj.stdout.read()
            stderr=obj.stderr.read()
            print(len(stdout) + len(stderr))
            conn.send(stdout+stderr)
        except ConnectionResetError:
            break

    conn.close()

server.close()
Server

 

Guess you like

Origin www.cnblogs.com/zhouhao123/p/11261824.html