summaryrefslogtreecommitdiffstats
path: root/actions/services/_ssh_tcp.py
blob: 7660ea5d5ecb2dfba83497f66c9d1ff4485c2305 (plain)
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
#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import os
import pwd

def enter_callback(widget, win):
    win.destroy()

def SshLogin(hostname, username = None):
    global win
    win = gtk.Dialog("Ssh Connection", None,
		     gtk.DIALOG_MODAL,
		     (gtk.STOCK_OK, gtk.RESPONSE_OK,
		      gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))

    vbox = gtk.VBox(False, 5)
    win.vbox.pack_start(vbox, True, True, 0)
    vbox.set_border_width(5)
    
    label = gtk.Label()
    label.set_markup("Connecting to <b>%s</b>.\nPlease enter your <b>login</b>:" % (hostname))
    
    vbox.pack_start(label, False, False, 0)
    
    # Create our entry
    entry = gtk.Entry()
    if username!= None:
	entry.set_text(username)
    entry.connect("activate", enter_callback, win)
    vbox.pack_start(entry, False, False, 0)
    
    # Create the completion object
    completion = gtk.EntryCompletion()
    
    # Assign the completion to the entry
    entry.set_completion(completion)
    
    # Create a tree model and use it as the completion model
    completion_model = __create_completion_model()
    completion.set_model(completion_model)
    
    # Use model column 0 as the text column
    completion.set_text_column(0)
    
    win.show_all()
    if win.run() == gtk.RESPONSE_OK:
        win.destroy()
        return entry.get_text()
    else:
        win.destroy()
        return None
    return entry.get_text()
    
def __create_completion_model():
    ''' Creates a tree model containing the completions.
    '''
    store = gtk.ListStore(str)
    
    iter = store.append()
    store.set(iter, 0, "root")
    
    current_user = pwd.getpwuid(os.getuid())[0]
    iter = store.append()
    store.set(iter, 0, current_user)
    
    return store

def _ssh_tcp(name, hostname, address, port, txts):

    if txts.has_key("u"):
	username = SshLogin(name, txts["u"])
    else:
	username = SshLogin(name)

    if username == None:
        return
    elif username != "":
        sshline = "ssh -l %s -p %i %s" % (username, port, address)
    else:
        sshline = "ssh -p %i %s" % (port, address)

    cmdline = []
    cmdline.append("gnome-terminal")
    cmdline.append("--tab")
    cmdline.append("-t %s" % name)
    cmdline.append("-e %s" % sshline)
    pid = subprocess.Popen(cmdline).pid

if __name__ == '__main__':
#    res = SshLogin("localhost", "robert")
#    print res
    _ssh_tcp(name, hostname, address, port, txts)