zoukankan      html  css  js  c++  java
  • Python 实现ssh远程命令

    server端:

    import
    socket import struct import subprocess IP = '127.0.0.1' PORT = 8989 def get_cmd(): sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sk.bind((IP,PORT)) sk.listen(5) while 1: connt,addr = sk.accept() while 1: cmd = connt.recv(1024) res = subprocess.getoutput(cmd.decode()) print(res) res_len = struct.pack('i',len(res)) connt.send(res_len) connt.send(res.encode()) if __name__ == '__main__': get_cmd()


    client端:
    import socket
    import struct

    IP = '127.0.0.1'
    PORT = 8989



    def get_cmd():
    sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sk.connect((IP,PORT))


    while 1:
    date = input('>>>: ').encode()
    sk.send(date)

    res_len = struct.unpack('i',sk.recv(4))[0]

    recv_len = 0
    res = b''
    while recv_len < res_len:
    date = sk.recv(1024)
    res += date
    recv_len += len(date)
    print(res.decode())


    if __name__ == '__main__':
    get_cmd()
     
  • 相关阅读:
    js使用笔记
    rabbit-mq使用官方文档
    tomcat Enabling JMX Remote
    Venom的简单使用
    Random模块
    时间模块
    shulti模块简述
    Python的os模块
    Python压缩及解压文件
    Kali的内网穿透
  • 原文地址:https://www.cnblogs.com/lxc123/p/13870077.html
Copyright © 2011-2022 走看看