3
3
Language : Python
4
4
File Name : main.py
5
5
Date Created : 28-12-2019
6
- Last Modified : 28 -12-2019
6
+ Last Modified : 31 -12-2019
7
7
"""
8
8
9
9
from tkinter import Entry , Label , StringVar , Tk , Listbox , Scrollbar , Button , END
10
- from paramiko import SSHClient , AutoAddPolicy
10
+ from paramiko import SSHClient , AutoAddPolicy , sftp
11
+ from tkinter .filedialog import askopenfile
12
+ from os .path import basename
13
+
14
+ local_script_path = None
11
15
12
16
13
17
def view_command ():
@@ -19,17 +23,43 @@ def view_command():
19
23
20
24
# SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
21
25
try :
22
- stdout = client .exec_command (script_path_text .get ())[1 ]
23
- for line in stdout :
24
- # Process each line in the remote output
25
- # print(line)
26
- server_response .insert (END , line )
26
+ global local_script_path
27
+
28
+ # Setup sftp connection and transmit this script
29
+ sftp = client .open_sftp ()
30
+ sftp .put (local_script_path .name , f'/tmp/{ basename (local_script_path .name )} ' )
31
+ sftp .close ()
32
+ path_to_script_on_server = '/tmp/' + basename (local_script_path .name )
33
+ try :
34
+ stdout = client .exec_command ('chmod +x ' + path_to_script_on_server )
35
+ stdout = client .exec_command (path_to_script_on_server )[1 ]
36
+ for line in stdout :
37
+ # Process each line in the remote output
38
+ # print(line)
39
+ server_response .insert (END , line )
40
+ except :
41
+ print ('Error executing the script' )
27
42
except :
28
43
print ('Execution failed' )
29
44
client .close ()
30
45
server_response .insert (END )
31
46
32
47
48
+ def file_picker ():
49
+ global local_script_path
50
+ local_script_path = askopenfile ()
51
+ script_path_entry .delete (0 , END )
52
+ script_path_entry .insert (END , local_script_path .name )
53
+ # print(local_script_path.name)
54
+
55
+
56
+ def clear ():
57
+ host_entry .delete (0 , END )
58
+ script_path_entry .delete (0 , END )
59
+ uname_entry .delete (0 , END )
60
+ password_entry .delete (0 , END )
61
+ server_response .delete (0 , END )
62
+
33
63
window = Tk ()
34
64
window .title ("Server Script Executer" )
35
65
@@ -47,6 +77,9 @@ def view_command():
47
77
script_path_entry = Entry (window , textvariable = script_path_text )
48
78
script_path_entry .grid (row = 0 , column = 4 )
49
79
80
+ file_picker = Button (window , text = "Select" , width = 5 , command = file_picker )
81
+ file_picker .grid (row = 0 , column = 6 )
82
+
50
83
uname_label = Label (window , text = "Username" )
51
84
uname_label .grid (row = 1 , column = 0 )
52
85
@@ -59,6 +92,7 @@ def view_command():
59
92
60
93
password_text = StringVar ()
61
94
password_entry = Entry (window , textvariable = password_text )
95
+ password_entry .config (show = '*' )
62
96
password_entry .grid (row = 1 , column = 4 )
63
97
64
98
server_response = Listbox (window , height = 10 , width = 40 )
@@ -73,6 +107,9 @@ def view_command():
73
107
# server_response.bind('<<ListboxSelect>>', get_selected_row)
74
108
75
109
execute_button = Button (window , text = "Execute" , width = 12 , command = view_command )
76
- execute_button .grid (row = 3 , column = 4 )
110
+ execute_button .grid (row = 4 , column = 4 )
111
+
112
+ clear_button = Button (window , text = "Clear" , width = 5 , command = clear )
113
+ clear_button .grid (row = 5 , column = 4 )
77
114
78
115
window .mainloop ()
0 commit comments