import subprocess import string from time import sleep class SSHTunneling: def __init__(self, localPort, remotePort, remoteServer, username, password): self.lPort = localPort self.rPort = remotePort self.rServer = remoteServer self.usern = username self.passw = password self.__tunnelStarted = 0 def startTunneling(self): if self.__tunnelStarted == 0: command = str(self.lPort) + ':' + self.rServer + ':' + str(self.rPort) uad = self.usern + '@' + self.rServer #result = subprocess.Popen(['ssh', '-f', '-g', '-A', '-X', '-N', '-L', command, uad, '&'], stdout = subprocess.PIPE, stderr = subprocess.STDOUT, stdin = subprocess.PIPE).communicate()[0] #result = subprocess.Popen(['ssh', '-f', '-g', '-A', '-X', '-N', '-L', command, uad]) # result = subprocess.Popen(['ssh', '-f', '-L', command, uad, 'sleep', '40']) # stdout = subprocess.PIPE # stderr = subprocess.PIPE # output = result.communicate() # status = result.poll() return subprocess.Popen(['ssh', '-N', '-L', command, uad], stdin=subprocess.PIPE, stdout=subprocess.PIPE) # stat = ssh_proc.poll() # while stat == None: # stat = ssh_proc.poll() # sleep(1) # print 'test' # print stat # print 'ovo je output', stdout # print 'stderr ', stderr # print 'ovo je status', status # while status == 0: # status = result.poll() # print 'test' #print output # def closeTunneling(self): x = SSHTunneling(50000, 80, '132.230.4.202', 'rz-gsm-testing', 'r') y = x.startTunneling() print 'Tunnel started ' sleep(50) y.kill() print 'kill tunnel' #print y