-
Notifications
You must be signed in to change notification settings - Fork 7
/
pysshfsplus
executable file
·340 lines (284 loc) · 11.2 KB
/
pysshfsplus
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# Quick mount sshfs
#
# by ADcomp <[email protected]>
# http://www.ad-comp.be/
#
# Autofill + ssh-bind mount changes by Anil Gulecha
# http://www.gulecha.org
#
# This program is distributed under the terms of the GNU General Public License
# For more info see http://www.gnu.org/licenses/gpl.txt
##
import pexpect
import os
import gtk
## print debug info ( True / False )
DEBUG = False
class UI:
def __init__(self):
# Create window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Mount sshFS")
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.connect("destroy", self.quit)
self.af = autofill();
(a_user, a_host, a_dir, a_mntpnt, a_port, a_open) = self.af.get_fill()
# Containers
BoxBase = gtk.VBox()
#~ BoxBase.set_spacing(5)
BoxBase.set_border_width(5)
BoxMain = gtk.HBox()
BoxMain.set_spacing(5)
BoxMain.set_border_width(5)
BoxControls = gtk.HButtonBox()
#~ BoxControls.set_spacing(2)
BoxControls.set_layout(gtk.BUTTONBOX_END)
# Exit
button_exit = gtk.Button(stock=gtk.STOCK_CLOSE)
button_exit.connect("clicked", self.quit)
BoxControls.pack_end(button_exit, False, False)
table = gtk.Table()
table.set_row_spacings(5)
table.set_col_spacings(5)
## Label
label = gtk.Label()
label.set_markup("<b>New Connection</b>")
label.set_alignment(0, 1)
table.attach(label, 0, 2, 0, 1)
## User
self.User_entry = gtk.Entry()
self.User_entry.set_text(a_user)
label = gtk.Label('User :')
label.set_alignment(0, 1)
table.attach(label, 0, 1, 1, 2)
table.attach(self.User_entry, 1, 2, 1, 2)
## Password
self.Password_entry = gtk.Entry()
self.Password_entry.set_visibility(False)
label = gtk.Label('Password :')
label.set_alignment(0, 1)
table.attach(label, 0, 1, 2, 3)
table.attach(self.Password_entry, 1, 2, 2, 3)
## Host
self.Host_entry = gtk.Entry()
label = gtk.Label('Host :')
self.Host_entry.set_text(a_host)
label.set_alignment(0, 1)
table.attach(label, 0, 1, 3, 4)
table.attach(self.Host_entry, 1, 2, 3, 4)
## Dir
self.Dir_entry = gtk.Entry()
label = gtk.Label('Dir :')
self.Dir_entry.set_text(a_dir)
label.set_alignment(0, 1)
table.attach(label, 0, 1, 4, 5)
table.attach(self.Dir_entry, 1, 2, 4, 5)
## Mountpoint
self.Mountpoint_entry = gtk.Entry()
label = gtk.Label('Mountpoint :')
self.Mountpoint_entry.set_text(a_mntpnt)
label.set_alignment(0, 1)
table.attach(label, 0, 1, 5, 6)
table.attach(self.Mountpoint_entry, 1, 2, 5, 6)
## Port
self.Port_entry = gtk.Entry()
label = gtk.Label('Port :')
self.Port_entry.set_text(a_port)
label.set_alignment(0, 1)
table.attach(label, 0, 1, 6, 7)
table.attach(self.Port_entry, 1, 2, 6, 7)
# Open directory
self.OpenDir_checkbox = gtk.CheckButton('Open directory')
if a_open == "True":
self.OpenDir_checkbox.set_active(True)
else:
self.OpenDir_checkbox.set_active(False)
table.attach(self.OpenDir_checkbox, 0, 1, 7, 8)
## Connect
self.Connect_Bt = gtk.Button(stock=gtk.STOCK_CONNECT)
self.Connect_Bt.connect("clicked", self.mount_sshfs)
table.attach(self.Connect_Bt, 1, 2, 7, 8)
## Separator
table.attach(gtk.HSeparator(), 0, 2, 8, 9)
## Label
label = gtk.Label()
label.set_markup("<b>Existing Mountpoints</b>")
label.set_alignment(0, 1)
table.attach(label, 0, 2, 9, 10)
## Mounted_FS
self.Mounted_fs_combo = gtk.combo_box_new_text()
table.attach(self.Mounted_fs_combo, 0, 2, 10, 11)
## Open / Umount
self.Open_Bt = gtk.Button(stock=gtk.STOCK_OPEN)
self.Open_Bt.connect("clicked", self.open_mountpoint)
table.attach(self.Open_Bt, 0, 1, 11, 12)
self.Umount_Bt = gtk.Button(stock=gtk.STOCK_DISCONNECT)
self.Umount_Bt.connect("clicked", self.umount_sshfs)
table.attach(self.Umount_Bt, 1, 2, 11, 12)
## Separator
table.attach(gtk.HSeparator(), 0, 2, 12, 13)
BoxMain.add(table)
BoxBase.pack_start(BoxMain, True)
BoxBase.pack_end(BoxControls, False)
self.window.add(BoxBase)
self.User_entry.connect("changed", self.auto_mountpoint)
self.Host_entry.connect("changed", self.auto_mountpoint)
self.update_mountedfs()
#Show main window frame and all content
self.window.show_all()
def auto_mountpoint(self, widget):
User = self.User_entry.get_text()
Host = self.Host_entry.get_text()
self.Mountpoint_entry.set_text('%s@%s' % (User, Host))
def open_mountpoint(self, widget):
mount_point = self.Mounted_fs_combo.get_active()
if mount_point == 0 or mount_point == -1:
return
else:
os.system('xdg-open %s' % self.Mounted_fs_combo.get_active_text())
def umount_sshfs(self, widget):
mount_point = self.Mounted_fs_combo.get_active()
if mount_point == 0 or mount_point == -1:
return
else:
os.system('fusermount -u %s' % self.Mounted_fs_combo.get_active_text())
os.system('rmdir %s' % self.Mounted_fs_combo.get_active_text())
self.update_mountedfs()
def update_mountedfs(self):
self.mounted_fs_tab = get_mounted_fs()
self.Mounted_fs_combo.get_model().clear()
self.Mounted_fs_combo.insert_text(0, 'Choose')
ind = 1
for mounted_fs in self.mounted_fs_tab:
self.Mounted_fs_combo.insert_text(ind, mounted_fs[1])
ind += 1
self.Mounted_fs_combo.set_active(0)
def mount_sshfs(self, widget):
User = self.User_entry.get_text()
Password = self.Password_entry.get_text()
Host = self.Host_entry.get_text()
Dir = self.Dir_entry.get_text()
Mountpoint = os.getenv('HOME') + '/' + self.Mountpoint_entry.get_text()
if not os.path.exists(Mountpoint):
os.mkdir(Mountpoint)
Port = self.Port_entry.get_text()
if User == '' or Host == '' or Mountpoint =='' or Port =='':
#!FixME
debug_info('Error : please check your config')
show_msg('Error : please check your config')
return
sshfs = sshFs()
ret = sshfs.mount(User, Password, Host, Dir, Mountpoint, Port)
self.update_mountedfs()
if ret[0] == 0:
self.af.set_fill(self.User_entry.get_text(),self.Host_entry.get_text(),self.Dir_entry.get_text(), \
self.Mountpoint_entry.get_text(), self.Port_entry.get_text(),str(self.OpenDir_checkbox.get_active()))
if self.OpenDir_checkbox.get_active():
os.system('xdg-open %s' % Mountpoint)
else:
show_msg(ret[1])
def run(self):
gtk.main()
def quit(self, widget=None, data=None):
gtk.main_quit()
## Initialize the module.
class sshFs:
def __init__(self):
## Three responses we might expect.
self.Initial_Responses = ['Are you sure you want to continue connecting (yes/no)?',
'password:', pexpect.EOF]
def mount(self, User="", Password="", Host="", Dir="", Mountpoint="", Port=22, Timeout=120):
Command = "sshfs -p %s %s@%s:%s %s" % (Port, User, Host, Dir, Mountpoint)
debug_info("Command : %s" % Command)
child = pexpect.spawn(Command)
## Get the first response.
ret = child.expect (self.Initial_Responses, Timeout)
## The first reponse is to accept the key.
if ret==0:
debug_info("The first reponse is to accept the key.")
#~ T = child.read(100)
child.sendline("yes")
child.expect('password:', Timeout)
child.sendline(Password)
## The second response sends the password.
elif ret == 1:
debug_info("The second response sends the password.")
child.sendline(Password)
# check for ssh bound mount - child exited and exitstatus=0
elif child.isalive() == False:
if child.exitstatus == 0:
return (0, "ssh-bound host mounted")
else:
#this is wierd.. quit
return (-3, 'ERROR: Unknown')
## Otherwise, there is an error.
else:
debug_info("Otherwise, there is an error.")
return (-3, 'ERROR: Unknown: ' + str(child.before))
## Get the next response.
Possible_Responses = ['password:', pexpect.EOF]
ret = child.expect (Possible_Responses, Timeout)
## If it asks for a password, error.
if ret == 0:
debug_info("If it asks for a password, error.")
return (-4, 'ERROR: Incorrect password.')
elif ret == 1:
debug_info("Otherwise we are okay.")
return (0, str(child.after))
## Otherwise we are okay.
else:
debug_info("Otherwise, there is an error.")
return (-3, 'ERROR: Unknown: ' + str(child.before))
class autofill:
def __init__(self):
homedir = os.getenv("HOME")
self.a_user = os.getenv('USER')
self.a_host = ""
self.a_dir = ""
self.a_mntpnt = ""
self.a_port = "22"
self.a_open = ""
debug_info("Conf file: " + homedir + "/.pysshfs")
if os.path.exists(homedir + "/.pysshfs"):
f = open(homedir + "/.pysshfs");
contents = f.read()
f.close()
fields=contents.split("\n")
self.a_user = fields[0]
self.a_host = fields[1]
self.a_dir = fields[2]
self.a_mntpnt = fields[3]
self.a_port = fields[4]
self.a_open = fields[5]
def get_fill(self):
return (self.a_user, self.a_host, self.a_dir, self.a_mntpnt, self.a_port, self.a_open)
def set_fill(self, a_user, a_host, a_dir, a_mntpnt, a_port, a_open):
f = open(os.getenv("HOME") + "/.pysshfs","w")
f.write("\n".join((a_user, a_host, a_dir, a_mntpnt, a_port, a_open)))
f.close()
def show_msg(msg=' .. '):
""" """
message = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, msg)
resp = message.run()
message.destroy()
def get_mounted_fs():
""" get_mounted_fs() -> reads mtab and returns a list of mounted sshfs filesystems. """
try:
mounted_fs = []
lines = [line.strip("\n").split(" ") for line in open ("/etc/mtab", "r").readlines()]
for line in lines:
if line[2] == "fuse.sshfs" and "user=%s" % os.getenv('USER') in line[3]:
mounted_fs.append((line[0], line[1]))
return mounted_fs
except:
debug_info("Could not read mtab")
show_msg("Could not read mtab .. :(")
def debug_info(msg):
if DEBUG:
print "# %s" % msg
if __name__ == "__main__":
ui = UI()
ui.run()