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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-16-2012, 10:15 AM
|
#1
|
|
Member
Registered: Nov 2011
Posts: 36
Rep: 
|
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?
|
|
|
|
06-16-2012, 03:54 PM
|
#2
|
|
Member
Registered: Nov 2011
Posts: 36
Original Poster
Rep: 
|
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.
|
|
|
|
06-19-2012, 10:56 AM
|
#3
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
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.
|
06-19-2012, 03:03 PM
|
#4
|
|
Member
Registered: Nov 2011
Posts: 36
Original Poster
Rep: 
|
Quote:
Originally Posted by Snark1994
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!
|
|
|
|
06-20-2012, 11:27 AM
|
#5
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
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.
|
06-20-2012, 11:36 AM
|
#6
|
|
Member
Registered: Nov 2011
Posts: 36
Original Poster
Rep: 
|
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
|
|
|
|
06-21-2012, 09:25 AM
|
#7
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
Quote:
Originally Posted by loolooyyyy
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.
|
03-15-2013, 10:25 AM
|
#8
|
|
Member
Registered: Nov 2011
Posts: 36
Original Poster
Rep: 
|
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!
|
|
|
|
03-17-2013, 10:18 AM
|
#9
|
|
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,624
|
It was actually typo'd: I meant ".ui mi do rinsa", meaning "I greet/welcome you happily", more or less
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:03 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|