-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMBMounter.py
271 lines (222 loc) · 8.73 KB
/
SMBMounter.py
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk, messagebox
import subprocess
import os
import getpass
from pathlib import Path
class SMBMounter:
def __init__(self, root):
self.root = root
self.root.title("SMB Share Mounter")
# Configuration
self.default_server = "lagrange"
self.server = self.default_server
self.username = getpass.getuser()
self.smb_links = Path.home() / "SMBLinks"
self.credentials_dir = Path.home() / ".credentials"
# Default shares if detection fails
self.default_shares = {
"photo": {"mounted": False},
"music": {"mounted": False},
"video": {"mounted": False},
"commun": {"mounted": False}
}
# Create main container
self.main_container = ttk.Frame(self.root, padding="10")
self.main_container.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Create server input form
self.create_server_form()
# Initialize shares as empty
self.shares = {}
def create_server_form(self):
"""Creates the server input form"""
form_frame = ttk.Frame(self.main_container)
form_frame.grid(row=0, column=0, pady=10, sticky=(tk.W, tk.E))
# Server label and entry
ttk.Label(
form_frame,
text="Server:",
font=('Helvetica', 10)
).pack(side=tk.LEFT, padx=5)
self.server_entry = ttk.Entry(form_frame, width=30)
self.server_entry.pack(side=tk.LEFT, padx=5)
self.server_entry.insert(0, self.default_server)
# Connect button
ttk.Button(
form_frame,
text="Connect",
command=self.connect_to_server
).pack(side=tk.LEFT, padx=5)
def connect_to_server(self):
"""Connects to the specified server and detects shares"""
new_server = self.server_entry.get().strip()
if not new_server:
messagebox.showerror("Error", "Please enter a server name")
return
self.server = new_server
# Clear previous widgets if they exist
if hasattr(self, 'main_frame'):
self.main_frame.destroy()
# Detect shares on the new server
self.shares = self.detect_shares()
# Create necessary directories
self.setup_directories()
# Create interface
self.create_widgets()
# Check mount status
self.check_mounted_shares()
def setup_directories(self):
"""Creates necessary directories"""
self.smb_links.mkdir(parents=True, exist_ok=True)
self.credentials_dir.mkdir(parents=True, exist_ok=True)
self.credentials_dir.chmod(0o700)
def detect_shares(self):
"""Detects available shares on the server"""
shares = {}
try:
cmd = ["smbclient", "-L", self.server, "-N"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
lines = result.stdout.split('\n')
for line in lines:
if "Disk" in line and not "$" in line:
share_name = line.split()[0].strip()
shares[share_name] = {"mounted": False}
if not shares:
messagebox.showwarning(
"Warning",
f"No shares found on {self.server}\nUsing default shares list"
)
shares = self.default_shares.copy()
else:
messagebox.showerror(
"Error",
f"Unable to list shares on {self.server}:\n{result.stderr}\nUsing default shares list"
)
shares = self.default_shares.copy()
except Exception as e:
messagebox.showerror(
"Error",
f"Error while detecting shares: {str(e)}\nUsing default shares list"
)
shares = self.default_shares.copy()
return shares
def create_widgets(self):
"""Creates the main interface widgets"""
self.main_frame = ttk.Frame(self.main_container)
self.main_frame.grid(row=1, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Header with refresh button
header_frame = ttk.Frame(self.main_frame)
header_frame.grid(row=0, column=0, columnspan=2, pady=10)
ttk.Label(
header_frame,
text=f"Available shares on {self.server}",
font=('Helvetica', 10, 'bold')
).pack(side=tk.LEFT, padx=5)
ttk.Button(
header_frame,
text="↻",
width=3,
command=self.refresh_shares
).pack(side=tk.LEFT, padx=5)
# Checkboxes for shares
self.check_vars = {}
for idx, share in enumerate(self.shares, start=1):
self.check_vars[share] = tk.BooleanVar()
cb = ttk.Checkbutton(
self.main_frame,
text=f"Share \\{share}",
variable=self.check_vars[share]
)
cb.grid(row=idx, column=0, sticky=tk.W, pady=2)
# Action buttons
btn_frame = ttk.Frame(self.main_frame)
btn_frame.grid(row=len(self.shares) + 2, column=0, columnspan=2, pady=10)
ttk.Button(
btn_frame,
text="Mount Selected",
command=self.mount_selected
).grid(row=0, column=0, padx=5)
# Add new Unmount Selected button
ttk.Button(
btn_frame,
text="Unmount Selected",
command=self.unmount_selected
).grid(row=0, column=1, padx=5)
ttk.Button(
btn_frame,
text="Unmount All",
command=self.unmount_all
).grid(row=0, column=2, padx=5)
def refresh_shares(self):
"""Refreshes the list of available shares"""
# Save current mount states
mounted_states = {share: self.shares[share]["mounted"]
for share in self.shares if share in self.check_vars}
# Detect shares again
self.shares = self.detect_shares()
# Recreate widgets
if hasattr(self, 'main_frame'):
self.main_frame.destroy()
self.create_widgets()
# Restore mount states
for share, mounted in mounted_states.items():
if share in self.check_vars:
self.check_vars[share].set(mounted)
def mount_selected(self):
"""Mounts selected shares"""
for share, var in self.check_vars.items():
if var.get():
self.mount_share(share)
self.check_mounted_shares()
def mount_share(self, share):
"""Mounts a specific share"""
try:
cmd = ["gio", "mount", f"smb://{self.server}/{share}"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
endpoint = Path(f"/run/user/{os.getuid()}/gvfs/smb-share:server={self.server},share={share}")
link_path = self.smb_links / share
if endpoint.exists():
if link_path.exists():
link_path.unlink()
link_path.symlink_to(endpoint)
messagebox.showinfo("Success", f"Share {share} mounted successfully in {self.smb_links}")
return True
else:
messagebox.showerror("Error", f"Error mounting {share}\n{result.stderr}")
return False
except Exception as e:
messagebox.showerror("Error", str(e))
return False
def unmount_selected(self):
"""Unmounts only the selected shares"""
for share, var in self.check_vars.items():
if var.get():
self.unmount_share(share)
self.check_mounted_shares()
def unmount_share(self, share):
"""Unmounts a specific share"""
try:
# Remove symbolic link
link_path = self.smb_links / share
if link_path.exists():
link_path.unlink()
# Unmount share
cmd = ["gio", "mount", "-u", f"smb://{self.server}/{share}"]
subprocess.run(cmd, capture_output=True, check=True)
return True
except Exception as e:
messagebox.showerror("Error", f"Error unmounting {share}: {str(e)}")
return False
def check_mounted_shares(self):
"""Checks which shares are currently mounted"""
for share in self.shares:
link_path = self.smb_links / share
self.shares[share]["mounted"] = link_path.exists()
if __name__ == "__main__":
root = tk.Tk()
app = SMBMounter(root)
root.mainloop()