-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-apps
More file actions
161 lines (129 loc) · 4.84 KB
/
Copy pathserver-apps
File metadata and controls
161 lines (129 loc) · 4.84 KB
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
#!/bin/bash
# Function to add a new server
add_server() {
local host=$(zenity --entry --title="Add Server" --text="Enter the hostname:")
local ip=$(zenity --entry --title="Add Server" --text="Enter the IP address:")
local password=$(zenity --password --title="Add Server" --text="Enter the password:")
if [[ -z "$host" || -z "$ip" || -z "$password" ]]; then
zenity --error --text="All fields are required."
return
fi
echo "$host|$ip|$password" >> servers.txt 2>/dev/null
if [[ $? -eq 0 ]]; then
zenity --info --text="Server added successfully."
else
zenity --error --text="Failed to add server."
fi
}
# Function to add a new command
add_command() {
local name=$(zenity --entry --title="Add Command" --text="Enter the command name:")
local command=$(zenity --entry --title="Add Command" --text="Enter the command:")
if [[ -z "$name" || -z "$command" ]]; then
zenity --error --text="All fields are required."
return
fi
echo "$name|$command" >> commands.txt 2>/dev/null
if [[ $? -eq 0 ]]; then
zenity --info --text="Command added successfully."
else
zenity --error --text="Failed to add command."
fi
}
# Function to edit an existing server
edit_server() {
local servers=$(awk -F '|' '{print $1}' "servers.txt")
if [[ -z "$servers" ]]; then
zenity --info --text="No servers available."
return
fi
local selected_server=$(zenity --list --title="Edit Server" --column="Servers" "$servers")
if [[ -z "$selected_server" ]]; then
return
fi
local new_host=$(zenity --entry --title="Edit Server" --text="Enter the new hostname:")
local new_ip=$(zenity --entry --title="Edit Server" --text="Enter the new IP address:")
local new_password=$(zenity --password --title="Edit Server" --text="Enter the new password:")
if [[ -z "$new_host" || -z "$new_ip" || -z "$new_password" ]]; then
zenity --error --text="All fields are required."
return
fi
sed -i "s/$selected_server|$host|$ip|$password|$name|$command|/|$new_host|$new_ip|$new_password|" servers.txt 2>/dev/null
if [[ $? -eq 0 ]]; then
zenity --info --text="Server edited successfully."
else
zenity --error --text="Failed to edit server."
fi
}
# Function to edit an existing command
edit_command() {
local commands=$(awk -F '|' '{print $1}' "commands.txt")
if [[ -z "$commands" ]]; then
zenity --info --text="No commands available."
return
fi
local selected_command=$(zenity --list --title="Edit Command" --column="Commands" "$commands")
if [[ -z "$selected_command" ]]; then
return
fi
local new_name=$(zenity --entry --title="Edit Command" --text="Enter the new command name:")
local new_command=$(zenity --entry --title="Edit Command" --text="Enter the new command:")
if [[ -z "$new_name" || -z "$new_command" ]]; then
zenity --error --text="All fields are required."
return
fi
sed -i "s/$selected_command|$name|$command|/|$new_name|$new_command|" commands.txt 2>/dev/null
if [[ $? -eq 0 ]]; then
zenity --info --text="Command edited successfully."
else
zenity --error --text="Failed to edit command."
fi
}
# Function to execute a command on a server
execute_command() {
local servers=$(awk -F '|' '{print $1}' "servers.txt")
if [[ -z "$servers" ]]; then
zenity --info --text="No servers available."
return
fi
local selected_server=$(zenity --list --title="Execute Command" --column="Servers" "$servers")
if [[ -z "$selected_server" ]]; then
return
fi
local selected_command=$(zenity --entry --title="Execute Command" --text="Enter the command name:")
if [[ -z "$selected_command" ]]; then
zenity --error --text="Command name is required."
return
fi
local command_line
for server in $servers; do
if [[ $server == *"$selected_server"* ]]; then
IFS='|' read -r name command <<< "$server"
if [[ "$name" == "$selected_command" ]]; then
command_line="$command"
break
fi
fi
done
if [[ -z "$command_line" ]]; then
zenity --error --text="Command not found."
return
fi
echo "Executing $command_line..."
eval "$command_line"
if [[ $? -eq 0 ]]; then
zenity --info --text="Command executed successfully."
else
zenity --error --text="Failed to execute command."
fi
}
# Main script logic
while true; do
choice=$(zenity --list --title="Server Manager" --column="Action" "Add Server" "Edit Server" "Execute Command" "Exit")
case $choice in
"Add Server") add_server ;;
"Edit Server") edit_server ;;
"Execute Command") execute_command ;;
"Exit") exit 0 ;;
esac
done