LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2012, 10:15 AM   #1
loolooyyyy
Member
 
Registered: Nov 2011
Posts: 36

Rep: Reputation: Disabled
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?
 
Old 06-16-2012, 03:54 PM   #2
loolooyyyy
Member
 
Registered: Nov 2011
Posts: 36

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-19-2012, 10:56 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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,
 
1 members found this post helpful.
Old 06-19-2012, 03:03 PM   #4
loolooyyyy
Member
 
Registered: Nov 2011
Posts: 36

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
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!
 
Old 06-20-2012, 11:27 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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

Hope this helps,
 
1 members found this post helpful.
Old 06-20-2012, 11:36 AM   #6
loolooyyyy
Member
 
Registered: Nov 2011
Posts: 36

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-21-2012, 09:25 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by loolooyyyy View Post
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?
Ahhhh, very good, very good :P https://en.wikipedia.org/wiki/Lojban
 
1 members found this post helpful.
Old 03-15-2013, 10:25 AM   #8
loolooyyyy
Member
 
Registered: Nov 2011
Posts: 36

Original Poster
Rep: Reputation: Disabled
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!
 
Old 03-17-2013, 10:18 AM   #9
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

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


Reply

Tags
python, ssh, tunnel



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Initiate ssh tunnel to connect to ssh? brianmcgee Linux - Security 2 09-07-2011 10:07 AM
SSH tunnel over SSH tunnel vockleya Linux - Networking 6 01-22-2010 06:25 PM
Trying to tunnel X over SSH DaneM Linux - Software 23 06-07-2008 08:17 AM
setting up an ssh soxy or local ssh tunnel from within an ssh soxy Mangenius Linux - Networking 0 03-05-2007 03:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration