dlinux-cli/dlinux

128 lines
2.7 KiB
Plaintext
Raw Normal View History

2024-11-30 08:28:24 -05:00
#!/bin/bash
KEY_FILE=~/.dlinux-cli.key
if [ ! -f $KEY_FILE ]; then
echo -e "Key file not found, run:\necho \"[YOUR-API-KEY]\" > $KEY_FILE"
exit 1
fi
API_KEY="$(cat $KEY_FILE)"
print_help() {
printf "
$0 hello - Get a hello message
$0 name - Get your SSHID
2024-11-30 09:26:48 -05:00
$0 start - Start your container
$0 stop - Stop your container
$0 restart - Restart your container
2024-11-30 09:12:32 -05:00
$0 info - Get info about your container
$0 stats - Get your container memory usage statistics
2024-11-30 09:19:29 -05:00
$0 time - Get your container expiration time
2024-11-30 10:02:26 -05:00
$0 rootpass - Reset root user password
2024-11-30 09:19:29 -05:00
$0 key-time - Get your api key expiration time
2024-11-30 15:04:48 -05:00
$0 exec [--user user] [--pwd directory] --cmd command - Execute a command in your container
$0 notify <message> - Send a message DM to your discord
2024-11-30 08:28:24 -05:00
"
}
# Helper function
call_api() {
2024-11-30 13:48:56 -05:00
curl -X $1 -H "x-ssh-auth: $API_KEY" -H "Content-Type: application/json" "https://api.ssh.surf/$2" -d "$3" 2> /dev/null
2024-11-30 08:28:24 -05:00
}
# Commands
command_hello() {
2024-11-30 09:12:32 -05:00
call_api GET hello | jq -r .message
2024-11-30 08:28:24 -05:00
}
command_name() {
2024-11-30 09:12:32 -05:00
call_api GET name | jq -r .message
}
2024-11-30 09:26:48 -05:00
command_start() {
call_api GET start | jq -r .message
}
command_stop() {
call_api GET stop | jq -r .message
}
command_restart() {
call_api GET restart | jq -r .message
}
2024-11-30 09:12:32 -05:00
command_info() {
data=$(call_api GET info)
printf "
Name: $(echo $data | jq -r .data.name)
IP Address: $(echo $data | jq -r .data.IPAddress)
MAC Address: $(echo $data | jq -r .data.MACAddress)
Memory: $(echo $data | jq -r .data.memory)
CPUs: $(echo $data | jq -r .data.cpus)
Restart policy: $(echo $data | jq -r .data.restartPolicy.Name)
Restarts: $(echo $data | jq -r .data.restarts)
State: $(echo $data | jq -r .data.state.Status)
Pid: $(echo $data | jq -r .data.state.Pid)
Started at: $(echo $data | jq -r .data.state.StartedAt)
"
}
command_stats() {
data=$(call_api GET stats)
printf "
Raw: $(echo $data | jq -r .data.memory.raw)
Percentage: $(echo $data | jq -r .data.memory.percent)%
CPU: $(echo $data | jq -r .data.cpu)%
"
2024-11-30 08:28:24 -05:00
}
2024-11-30 09:19:29 -05:00
command_time() {
call_api GET time | jq -r .expireDate
}
2024-11-30 10:02:26 -05:00
command_rootpass() {
data=$(call_api GET rootpass)
echo "Your new root password is: $(echo $data | jq -r .newRootPass)"
}
2024-11-30 09:19:29 -05:00
command_key-time() {
call_api GET key-time | jq -r .keyexpireString
}
2024-11-30 13:48:56 -05:00
command_exec() {
while test $# -gt 0; do
case $1 in
2024-11-30 15:04:48 -05:00
--user|-u) user=$2; shift ;;
2024-11-30 13:48:56 -05:00
--pwd|-p) pwd=$2; shift ;;
--cmd|-c) cmd=$2; shift ;;
esac
shift
done
2024-11-30 15:04:48 -05:00
user=${user:-"root"}
2024-11-30 13:48:56 -05:00
pwd=${pwd:-"/"}
2024-11-30 15:04:48 -05:00
if ! test $user == "root"; then
cmd="runuser -u $user -- $cmd"
fi
2024-11-30 13:48:56 -05:00
call_api POST exec "{\"pwd\":\"$pwd\",\"cmd\":\"$cmd\"}" | jq -r .stdout
}
command_notify() {
call_api POST notify "{\"message\":\"$2\"}" | jq -r .message
}
2024-11-30 08:28:24 -05:00
# Command handler
if [ $1 ]; then
2024-11-30 13:48:56 -05:00
command_$1 "$@"
2024-11-30 08:28:24 -05:00
else
print_help
fi