33 Language : Python
44 File Name : main.py
55 Date Created : 28-12-2019
6- Last Modified : 28 -12-2019
6+ Last Modified : 31 -12-2019
77"""
88
99from 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
1115
1216
1317def view_command ():
@@ -19,17 +23,43 @@ def view_command():
1923
2024 # SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
2125 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' )
2742 except :
2843 print ('Execution failed' )
2944 client .close ()
3045 server_response .insert (END )
3146
3247
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+
3363window = Tk ()
3464window .title ("Server Script Executer" )
3565
@@ -47,6 +77,9 @@ def view_command():
4777script_path_entry = Entry (window , textvariable = script_path_text )
4878script_path_entry .grid (row = 0 , column = 4 )
4979
80+ file_picker = Button (window , text = "Select" , width = 5 , command = file_picker )
81+ file_picker .grid (row = 0 , column = 6 )
82+
5083uname_label = Label (window , text = "Username" )
5184uname_label .grid (row = 1 , column = 0 )
5285
@@ -59,6 +92,7 @@ def view_command():
5992
6093password_text = StringVar ()
6194password_entry = Entry (window , textvariable = password_text )
95+ password_entry .config (show = '*' )
6296password_entry .grid (row = 1 , column = 4 )
6397
6498server_response = Listbox (window , height = 10 , width = 40 )
@@ -73,6 +107,9 @@ def view_command():
73107# server_response.bind('<<ListboxSelect>>', get_selected_row)
74108
75109execute_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 )
77114
78115window .mainloop ()
0 commit comments