Newer
Older
print("execute the code from working test file:\n")
from sshtunnel import SSHTunnelForwarder
import pymongo
import pprint
MONGO_HOST = "141.20.38.120"
MONGO_DB = "github_scraping"
with open("github_scraping/db_operations/mongodb_user.txt", "r") as f:
mongodb_username = f.readline().strip()
mongodb_password = f.readline().strip()
remote_username = f.readline().strip()
remote_password = f.readline().strip()
server = SSHTunnelForwarder(
remote_bind_address=('127.0.0.1', 27017)
)
server.start()
print("DEBUG:", "remote_bind_address", "('127.0.0.1' 27017)")
print("DEBUG:", "local_bind_port", str(server.local_bind_port))
print("DEBUG:", "local_bind_address", str(server.local_bind_address))
client = pymongo.MongoClient('127.0.0.1', server.local_bind_port) # server.local_bind_port is assigned local port
new_db = client[MONGO_DB]
print("New database collection names:", new_db.list_collection_names())
from ..db_operations.db_connector import DBConnector
old_db = DBConnector()
print("Old database collection names:", old_db.list_collection_names())
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
print("Do the migration.")
import paramiko
import socket
import sys
import subprocess
from sshtunnel import SSHTunnelForwarder
from pymongo import MongoClient
with open("github_scraping/db_operations/mongodb_user.txt", "r") as f:
mongodb_username = f.readline().strip()
mongodb_password = f.readline().strip()
remote_username = f.readline().strip()
remote_password = f.readline().strip()
# first tunnel connection to gruenau7
gruenau7_ip = '141.20.21.41'
mongodb_ip = '141.20.38.120'
#local_port = 49205
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#sock.bind(('', local_port)) # set source address
#sock.connect(('', 22)) # connect to the destination address
gruenau7 = paramiko.SSHClient()
gruenau7.set_missing_host_key_policy(paramiko.AutoAddPolicy())
gruenau7.connect(gruenau7_ip, username=remote_username, password=remote_password)
gruenau7_transport = gruenau7.get_transport()
dest_addr = (mongodb_ip, 22)
local_addr = (gruenau7_ip, 22)
gruenau7_channel = gruenau7_transport.open_channel("direct-tcpip", dest_addr, local_addr)
mongodb = paramiko.SSHClient()
mongodb.set_missing_host_key_policy(paramiko.AutoAddPolicy())
mongodb.connect(mongodb_ip, username=remote_username, password=remote_password, sock=gruenau7_channel)
mongodb_transport = mongodb.get_transport()
dest_addr = ('0.0.0.0', 8000)
local_addr = ('127.0.0.1', 32034)
mongodb_channel = mongodb_transport.open_channel("direct-tcpip", dest_addr, local_addr)
exit()
new_channel = mongodb_transport.open_session()
print(new_channel.getpeername())
print(new_channel.get_id())
print(new_channel.get_transport())
print(new_channel.get_name())
print(new_channel.__getattribute__("port"))
exit()
#stdin, stdout, stderr = mongodb.exec_command("mongod --version")
#print(stdout.read()) # edited#
#exit()
connection = MongoClient('127.0.0.1', local_port)
db = connection["github_scraping"]
data = db.list_collection_names()
print(data)
mongodb.close()
gruenau7.close()
# define ssh tunnel
#server = SSHTunnelForwarder(
# "141.20.38.120",
# ssh_username=remote_username,
# ssh_pkey=remote_password,
# remote_bind_address=('141.20.21.41', 22)
#)
# start ssh tunnel
#server.start()
#server.local_bind_address
#connection = MongoClient('127.0.0.1', server.local_bind_port)
#db = connection["guthub_scraping"]
#data = db.list_collection_names()
#print(data)