🐛 fix: Odstránenie závislosti na jq — parsovanie JSON cez grep/sed
Proxmox host nemá jq nainštalované. Parsovanie users.json prepísané na grep/sed/awk v ct/ubuntu.sh aj ubuntu-install.sh. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
26
ct/ubuntu.sh
26
ct/ubuntu.sh
@@ -119,9 +119,23 @@ simple_install() {
|
|||||||
}
|
}
|
||||||
msg_ok "Zoznam užívateľov stiahnutý"
|
msg_ok "Zoznam užívateľov stiahnutý"
|
||||||
|
|
||||||
# Parsovanie užívateľov pre whiptail checklist
|
# Parsovanie užívateľov bez jq — cez grep/sed
|
||||||
local user_count
|
local usernames=()
|
||||||
user_count=$(echo "$users_json" | jq length)
|
local key_counts=()
|
||||||
|
while IFS= read -r uname; do
|
||||||
|
[[ -n "$uname" ]] && usernames+=("$uname")
|
||||||
|
done < <(echo "$users_json" | grep '"username"' | sed 's/.*"username"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
|
||||||
|
|
||||||
|
# Počet SSH kľúčov pre každého užívateľa
|
||||||
|
local idx=0
|
||||||
|
for uname in "${usernames[@]}"; do
|
||||||
|
local kc
|
||||||
|
kc=$(echo "$users_json" | sed -n "/$uname/,/\]/p" | grep 'ssh-' | wc -l)
|
||||||
|
key_counts+=("$kc")
|
||||||
|
idx=$((idx + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
local user_count=${#usernames[@]}
|
||||||
|
|
||||||
if [[ "$user_count" -eq 0 ]]; then
|
if [[ "$user_count" -eq 0 ]]; then
|
||||||
msg_warn "Žiadni užívatelia v users.json"
|
msg_warn "Žiadni užívatelia v users.json"
|
||||||
@@ -129,11 +143,7 @@ simple_install() {
|
|||||||
else
|
else
|
||||||
local checklist_args=()
|
local checklist_args=()
|
||||||
for i in $(seq 0 $((user_count - 1))); do
|
for i in $(seq 0 $((user_count - 1))); do
|
||||||
local uname
|
checklist_args+=("${usernames[$i]}" "${key_counts[$i]} SSH kľúč(ov)" "ON")
|
||||||
uname=$(echo "$users_json" | jq -r ".[$i].username")
|
|
||||||
local key_count
|
|
||||||
key_count=$(echo "$users_json" | jq ".[$i].ssh_keys | length")
|
|
||||||
checklist_args+=("$uname" "${key_count} SSH kľúč(ov)" "ON")
|
|
||||||
done
|
done
|
||||||
|
|
||||||
SELECTED_USERS=$(whiptail --backtitle "Ubuntu LXC Setup" \
|
SELECTED_USERS=$(whiptail --backtitle "Ubuntu LXC Setup" \
|
||||||
|
|||||||
@@ -26,43 +26,97 @@ if [[ -n "${USERS_JSON:-}" && -n "${SELECTED_USERS:-}" ]]; then
|
|||||||
# Parsovanie SELECTED_USERS (whiptail vracia "user1" "user2" formát)
|
# Parsovanie SELECTED_USERS (whiptail vracia "user1" "user2" formát)
|
||||||
selected_list=$(echo "$SELECTED_USERS" | tr -d '"')
|
selected_list=$(echo "$SELECTED_USERS" | tr -d '"')
|
||||||
|
|
||||||
user_count=$(echo "$USERS_JSON" | jq length)
|
# Parsovanie users.json bez jq — cez grep/sed/awk
|
||||||
for i in $(seq 0 $((user_count - 1))); do
|
# Prechádzame každý blok užívateľa
|
||||||
username=$(echo "$USERS_JSON" | jq -r ".[$i].username")
|
current_user=""
|
||||||
has_sudo=$(echo "$USERS_JSON" | jq -r ".[$i].sudo")
|
current_sudo="false"
|
||||||
|
current_keys=()
|
||||||
|
in_keys=false
|
||||||
|
|
||||||
# Kontrola, či bol užívateľ vybraný
|
while IFS= read -r line; do
|
||||||
if ! echo "$selected_list" | grep -qw "$username"; then
|
# Detekcia username
|
||||||
|
if echo "$line" | grep -q '"username"'; then
|
||||||
|
# Ak máme predchádzajúceho užívateľa, spracuj ho
|
||||||
|
if [[ -n "$current_user" ]]; then
|
||||||
|
# Kontrola, či bol užívateľ vybraný
|
||||||
|
if echo "$selected_list" | grep -qw "$current_user"; then
|
||||||
|
# Vytvorenie užívateľa s náhodným heslom
|
||||||
|
random_pw=$(openssl rand -base64 16)
|
||||||
|
useradd -m -s /bin/bash "$current_user"
|
||||||
|
echo "${current_user}:${random_pw}" | chpasswd
|
||||||
|
|
||||||
|
# SSH kľúče
|
||||||
|
user_home="/home/${current_user}"
|
||||||
|
mkdir -p "${user_home}/.ssh"
|
||||||
|
chmod 700 "${user_home}/.ssh"
|
||||||
|
for key in "${current_keys[@]}"; do
|
||||||
|
echo "$key" >> "${user_home}/.ssh/authorized_keys"
|
||||||
|
done
|
||||||
|
chmod 600 "${user_home}/.ssh/authorized_keys"
|
||||||
|
chown -R "${current_user}:${current_user}" "${user_home}/.ssh"
|
||||||
|
|
||||||
|
# Sudo bez hesla
|
||||||
|
if [[ "$current_sudo" == "true" ]]; then
|
||||||
|
echo "${current_user} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${current_user}"
|
||||||
|
chmod 440 "/etc/sudoers.d/${current_user}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
msg_ok "Užívateľ vytvorený: ${current_user}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
current_user=$(echo "$line" | sed 's/.*"username"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
|
||||||
|
current_sudo="false"
|
||||||
|
current_keys=()
|
||||||
|
in_keys=false
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Detekcia sudo
|
||||||
|
if echo "$line" | grep -q '"sudo"'; then
|
||||||
|
if echo "$line" | grep -q 'true'; then
|
||||||
|
current_sudo="true"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Detekcia SSH kľúčov
|
||||||
|
if echo "$line" | grep -q '"ssh_keys"'; then
|
||||||
|
in_keys=true
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
if [[ "$in_keys" == true ]]; then
|
||||||
# Vytvorenie užívateľa s náhodným heslom
|
if echo "$line" | grep -q '\]'; then
|
||||||
random_pw=$(openssl rand -base64 16)
|
in_keys=false
|
||||||
useradd -m -s /bin/bash "$username"
|
continue
|
||||||
echo "${username}:${random_pw}" | chpasswd
|
fi
|
||||||
|
local key
|
||||||
# SSH kľúče
|
key=$(echo "$line" | sed 's/.*"\(ssh-[^"]*\)".*/\1/')
|
||||||
user_home="/home/${username}"
|
[[ -n "$key" && "$key" != "$line" ]] && current_keys+=("$key")
|
||||||
mkdir -p "${user_home}/.ssh"
|
|
||||||
chmod 700 "${user_home}/.ssh"
|
|
||||||
|
|
||||||
key_count=$(echo "$USERS_JSON" | jq ".[$i].ssh_keys | length")
|
|
||||||
for k in $(seq 0 $((key_count - 1))); do
|
|
||||||
key=$(echo "$USERS_JSON" | jq -r ".[$i].ssh_keys[$k]")
|
|
||||||
echo "$key" >> "${user_home}/.ssh/authorized_keys"
|
|
||||||
done
|
|
||||||
|
|
||||||
chmod 600 "${user_home}/.ssh/authorized_keys"
|
|
||||||
chown -R "${username}:${username}" "${user_home}/.ssh"
|
|
||||||
|
|
||||||
# Sudo bez hesla
|
|
||||||
if [[ "$has_sudo" == "true" ]]; then
|
|
||||||
echo "${username} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${username}"
|
|
||||||
chmod 440 "/etc/sudoers.d/${username}"
|
|
||||||
fi
|
fi
|
||||||
|
done <<< "$USERS_JSON"
|
||||||
|
|
||||||
msg_ok "Užívateľ vytvorený: ${username}"
|
# Spracuj posledného užívateľa
|
||||||
done
|
if [[ -n "$current_user" ]]; then
|
||||||
|
if echo "$selected_list" | grep -qw "$current_user"; then
|
||||||
|
random_pw=$(openssl rand -base64 16)
|
||||||
|
useradd -m -s /bin/bash "$current_user"
|
||||||
|
echo "${current_user}:${random_pw}" | chpasswd
|
||||||
|
|
||||||
|
user_home="/home/${current_user}"
|
||||||
|
mkdir -p "${user_home}/.ssh"
|
||||||
|
chmod 700 "${user_home}/.ssh"
|
||||||
|
for key in "${current_keys[@]}"; do
|
||||||
|
echo "$key" >> "${user_home}/.ssh/authorized_keys"
|
||||||
|
done
|
||||||
|
chmod 600 "${user_home}/.ssh/authorized_keys"
|
||||||
|
chown -R "${current_user}:${current_user}" "${user_home}/.ssh"
|
||||||
|
|
||||||
|
if [[ "$current_sudo" == "true" ]]; then
|
||||||
|
echo "${current_user} ALL=(ALL) NOPASSWD: ALL" > "/etc/sudoers.d/${current_user}"
|
||||||
|
chmod 440 "/etc/sudoers.d/${current_user}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
msg_ok "Užívateľ vytvorený: ${current_user}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user