python ssh实现_python实现SSH 转发

#-*- coding: utf-8 -*-

from socket import *

importthreadingimportQueue#test in python 2.7

#a list of local port mapped to machine

ssh_server_list = [{"host": '10.239.58.102', "port": 8102, "ip": "192.168.122.102"},

{"host": '10.239.58.102', "port": 8103, "ip": "192.168.122.103"},

{"host": '10.239.58.102', "port": 8101, "ip": "192.168.122.101"},

{"host": '10.239.58.102', "port": 8104, "ip": "192.168.122.104"}]#sync all the threads in each of tunnel

running_flag =[]#receive buffSize

buffer_size = 2048

defget_data_from_ssh_server(rev_msg, tcp_socket, flag):""":param rev_msg: a queue buffer of message need to be send to SSH client

:param tcp_socket: instance of socket used for sending data

:param flag: control this function

:return: null"""

whilerunning_flag[flag]:

data=tcp_socket.recv(buffer_size)iflen(data):

rev_msg.put(str(data))else:

running_flag[flag]=Falsedefsend_data_to_ssh_client(rev_msg, tcp_socket, flag):""":param rev_msg: a queue buffer of message need to be send to SSH client

:param tcp_socket: instance of socket used for sending data

:param flag: control this function

:return: null"""

whilerunning_flag[flag]:try:

data= rev_msg.get(timeout=10)

data=tcp_socket.send(str(data))except:pass

defget_data_from_ssh_client(send_msg, tcp_socket, flag):""":param send_msg: a queue buffer of message need to be send to SSH server in each machine

:param tcp_socket: instance of socket used for sending data

:param flag: control this function

:return: null"""

whilerunning_flag[flag]:

data=tcp_socket.recv(buffer_size)iflen(data):

send_msg.put(str(data))else:

running_flag[flag]=Falsedefsend_data_to_ssh_server(send_msg, tcp_socket, flag):""":param send_msg: a queue buffer of message need to be send to SSH server in each machine

:param tcp_socket: instance of socket used for sending data

:param flag: control this function

:return: null"""

whilerunning_flag[flag]:try:

data= send_msg.get(timeout=10)

data=tcp_socket.send(str(data))except:pass

defhandle_connections(host, ip, port):""":param host: local ip

:param ip: which machine the data will be forwarded

:param port: local port

:return: null"""ssh_client_socket=socket(AF_INET, SOCK_STREAM)

ssh_client_socket.bind((host, port))#listen 10 client

ssh_client_socket.listen(10)whileTrue:

ssh_client_side, address=ssh_client_socket.accept()#two queue for keeping data from SSH client and SSH server

buffer_send =Queue.Queue()

buffer_rev=Queue.Queue()

ssh_server_side=socket(AF_INET, SOCK_STREAM)

ssh_server_side.connect((ip,22))

flag=True

running_flag.append(flag)

rev1= threading.Thread(target=get_data_from_ssh_server,

args=(buffer_rev, ssh_server_side, len(running_flag) - 1))

rev2= threading.Thread(target=send_data_to_ssh_client,

args=(buffer_rev, ssh_client_side, len(running_flag) - 1))

send1= threading.Thread(target=get_data_from_ssh_client,

args=(buffer_send, ssh_client_side, len(running_flag) - 1))

send2= threading.Thread(target=send_data_to_ssh_server,

args=(buffer_send, ssh_server_side, len(running_flag) - 1))

rev1.start()

rev2.start()

send1.start()

send2.start()if __name__ == "__main__":print("start SSH forward server")

thread_pool=[]for i inssh_server_list:print("ssh mapping" + i["host"] + ":" + str(i["port"]) + "=>" + i["ip"] + ":22")

t= threading.Thread(target=handle_connections, args=(i["host"], i["ip"], i["port"]))

thread_pool.append(t)

t.start()print("initialize SSH forward server done")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值