ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i'm trying to use python to create a tunnel between two machines: A on 192.168.1.8 and B on 192.168.1.9 and ssh through this tunnel (i really want to do it using python, learning stuff)<br>
this is the schema:
SSH_To_Port_3000_local<==>[port3000listening]client.py[connecting when something received to B on 4000]<==>[port4000listening_On_B]server.py[connecting when something came to port 22/ssh]<==>[port22]SSH_deamon
<br>
when i telnet through this tunnel ( telnet localhost 3000) ssh daemon answers, i answer it with what should be actually a ssh program answering, then daemon answers again, and and and....
but when i do `ssh localhost -p 3000`, after the exact 5th message from server.py log that indicates : package: sent, recived,recived, sent sent (right here)<br>
everything stops
what kind of information should i provide to get help?
the reason i used pickling is that i had no idea how to compress data using zlib, got every kind of error you could think of, untill i pickled data contained in a buffer class
what i'm thinking now, is that the problem might be unsynchronized transmission of data?
client:
Code:
from threading import Thread
from socket import *
from zlib import compress as c,decompress as d
from pickle import loads,dumps
from sys import argv
send_host = "192.168.1.9"
srvc_host = ""
send_port = int(argv[2])
srvc_port = int(argv[1])
send_skt = socket(AF_INET,SOCK_STREAM) #the other machine were we connect to (server)
srvc_skt = socket(AF_INET,SOCK_STREAM) #data that is supposed to go to server arrives here
srvc_skt.bind((srvc_host,srvc_port))
srvc_skt.listen(10)
srvc_skt,addr=srvc_skt.accept() #wait for someone to connect
send_skt.connect((send_host,send_port)) #someone is connected(accept() returend) so connect to server
class c_buff: #buffer
data=None
sdata_container=c_buff()
rdata_container=c_buff()
def send(send_skt,srvc_skt): #send data to server as soon as it arives
global sdata_container
while True:
try:
sdata_container.data=srvc_skt.recv(512) #get data
send_data=c(dumps(sdata_container)) #pickle and then compress it
send_skt.send(send_data) #send it
print('send') #log it
except:
pass
def recv(send_skt,srvc_skt):
global rdata_container
while True:
try:
rdata_container=loads(d(send_skt.recv(512))) #got data from server? so unpickle and decompress it
recv_data=rdata_container.data #extract data
srvc_skt.send(recv_data) #send it to whoever asked for it
print('recv') #log it
except:
pass
threads = []
threads.append(Thread(target=send,args=(send_skt,srvc_skt)))
threads.append(Thread(target=recv,args=(send_skt,srvc_skt)))
threads[0].start()
threads[1].start()
server:
Code:
from threading import Thread
from socket import *
from zlib import compress as c,decompress as d
from pickle import dumps,loads
from sys import argv
send_host = ""
send_port = int(argv[1]) #send port (to client)
srvc_host = ""
srvc_port = int(argv[2]) #ssh port
send_skt = socket(AF_INET,SOCK_STREAM)
srvc_skt = socket(AF_INET,SOCK_STREAM)
send_skt.bind((send_host,send_port))
send_skt.listen(10)
send_skt,addr=send_skt.accept() #wait for someone to connect
srvc_skt.connect((srvc_host,srvc_port)) #someone connected so connect to ssh
class c_buff: #buffer
data=None
sdata_container=c_buff()
rdata_container=c_buff()
def send(send_skt,srvc_skt):
global sdata_container
while True:
try:
sdata_container.data=srvc_skt.recv(512) #get data from ssh
send_data=c(dumps(sdata_container)) #pickle and compress it
send_skt.send(send_data) # send it to client
print('send') #log it
except:
pass
def recv(send_skt,srvc_skt):
global rdata_container
while True:
try:
rdata_container=loads(d(send_skt.recv(512))) #get data from client,decompress and unpickle it
recv_data=rdata_container.data #extract data
srvc_skt.send(recv_data) #send to ssh daemon
print('recv')#log it
except:
pass
threads = []
threads.append(Thread(target=send,args=(send_skt,srvc_skt)))
threads.append(Thread(target=recv,args=(send_skt,srvc_skt)))
threads[0].start() #star reciving data
threads[1].start() #start sending data
Last edited by loolooyyyy; 06-16-2012 at 04:06 PM.
You should break it down into components. I've only had a cursory look at your code, but do you know where it's going wrong?
* Is the client getting data from srvc_skt?
* Is it sending it to the server?
* Is the server receiving it?
* Are you sure it should be sending/receiving more messages?
* Have you tried comparing the client and the server logs?
* Have you tried e.g. making the client single threaded, sending some dummy data, and testing that the server deals with it properly?
* If in doubt, add more print() statements until you know exactly what it's doing.
Essentially, you need to narrow it down to a smaller problem - either you'll spot a bug, or you'll reduce it to a piece of behaviour you can't explain (for example, "I don't see why removing this line of code should affect anything, but it does!") which you can then post here and we can try to help you.
You should break it down into components. I've only had a cursory look at your code, but do you know where it's going wrong?
Best of luck,
that's exactly my problem, how do i make it single threaded? i dont know the data order!
but that's the best advice, i should do it from beginning, adding options step by step, see which one breaks it
thanks for reading my code!
Actually, before you do anything, change all your 'except's into something that will print an error message - or remove the try/except structure altogether. It's really easy to have subtle errors being hidden by 'except's which don't name a type of error.
If that doesn't point you to a solution, I would recommend to forget about the connecting (from the client) and the ssh service (from the server), just check your two programmes can bounce messages to each other.
So, instead of client.py reading data from srvc_skt, it has:
Code:
def send(send_skt):
string = "a"
while True:
send_skt.send(string.encode('utf8'))
print('Sent',string)
string += chr((ord(string[-1])-96)%26+97) #just puts the next letter of the alphabet on
time.sleep(1)
and change server.py to bounce the data back. If that works, your programmes are communicating properly. Then change the server programme back, and get the client to read from STDIN (so you yourself run the ssh session), and if that works, then you know your server programme is working correctly, and the problem is with the client reading data. Or something like that anyway
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.