LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   ssh through python tunnel? (http://www.linuxquestions.org/questions/programming-9/ssh-through-python-tunnel-4175411802/)

loolooyyyy 06-16-2012 10:15 AM

ssh through python tunnel?
 
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?

loolooyyyy 06-16-2012 03:54 PM

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


Snark1994 06-19-2012 10:56 AM

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.

Best of luck,

loolooyyyy 06-19-2012 03:03 PM

Quote:

Originally Posted by Snark1994 (Post 4707012)
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!

Snark1994 06-20-2012 11:27 AM

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 :D

Hope this helps,

loolooyyyy 06-20-2012 11:36 AM

instead of getting the client to send server test data, i use telnet, i say hi, server should say "hi" or even better "1st hi", "2nd hi"....

and i couldn't get what that code says: .ua mi do rinsa? :D

Snark1994 06-21-2012 09:25 AM

Quote:

Originally Posted by loolooyyyy (Post 4707878)
instead of getting the client to send server test data, i use telnet, i say hi, server should say "hi" or even better "1st hi", "2nd hi"....

Yeah, fine, in which case you know that the client's receiving and sending data properly. Good plan :)

Quote:

and i couldn't get what that code says: .ua mi do rinsa? :D
Ahhhh, very good, very good :P https://en.wikipedia.org/wiki/Lojban

loolooyyyy 03-15-2013 10:25 AM

Dear snark
Could you kindly translate the sentence? I couldn't (you're welcome to discover me?)

yeah... it's been a while i don't know why now i need to know!

Snark1994 03-17-2013 10:18 AM

It was actually typo'd: I meant ".ui mi do rinsa", meaning "I greet/welcome you happily", more or less


All times are GMT -5. The time now is 07:01 AM.