summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Server-Code-New/SSHTunnelBoxClass.py
blob: 1b76d0b1786ff9f61dbcf6c30606ed46ad36f92b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#Here is a tutorial how to generate and copy your RSA SSH public key :) 
#>>> http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/
import subprocess
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
		self.__sshTunnel = 1

	def startTunneling(self):
		if self.__tunnelStarted == 0:
			command = str(self.lPort) + ':' + self.rServer + ':' + str(self.rPort) 
			uad = self.usern + '@' + self.rServer

			try:
				self.__sshTunnel = subprocess.Popen(['ssh','-p','7884','-N', '-L', command, uad], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
				sleep(2)				
				self.__tunnelStarted = 1
				return 1
			except Exception, e:
				error = str(e)
				return 0

	def killTunneling(self):
		if self.__tunnelStarted == 1:
			try:
				self.__sshTunnel.kill()
				self.__tunnelStarted = 0
				return 1

			except Exception, e:
				error = str(e)
				return 0