socket模拟SSH执行远端命令

made by G.M

接收命令执行断(server端)

import socket,commands
HOST = '192.168.0.200'                 # Symbolic name meaning all available interfaces
PORT = 33033                             # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while 1:
    conn, addr = s.accept()
    data = conn.recv(4096)
    if not data:
        continue
    meg=commands.getoutput(data)
    conn.send(meg)
conn.close()

server端侦听33033端口接收命令并做处理

发送命令断(client端)

import socket
so=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#解决Bind failed. Error Code : 98 Message Address already in use 问题
so.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)     
host="192.168.0.200"
cmd="ifconfig"
port=33033
so.connect((host,port))
so.send(cmd)
_srt=so.recv(2048)
print _srt

client端发送 shell 命令 ifconfig 查看网卡信息
结果会返回server 端的网卡信息

以上是一些基本功能的实现,如果用于生产要做好安全限制和异常的判断.