Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - ability to export list of configured hosts to CSV #6

Open
Marthisdil opened this issue Nov 11, 2019 · 3 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Marthisdil
Copy link

Would be handy to be able to export host info (like when you do hosts list or hosts list -v) to a csv file

@HeySteets
Copy link
Contributor

HeySteets commented Nov 12, 2019

I'd add to this - What about the ability to import hosts from a CSV as well? That way, you can easily port around lists and swap them in and out without moving the database file.

@Marthisdil
Copy link
Author

That sounds like a good idea too! thanks for the tool :)

@gnur gnur added enhancement New feature or request good first issue Good for newcomers labels Aug 28, 2020
@lazee486
Copy link

lazee486 commented Nov 7, 2024

sudo nano hosts.txt
#insert one ip:port per line
ctrl-X then Y
sudo nano dimport.sh
#paste script inside
ctrl-x then Y
sudo chmod +X dimport.sh
./dimport.sh

#!/bin/bash

# File containing the original list of hosts
hosts_file="hosts.txt"
success_file="success_hosts.txt"
failed_file="failed_hosts.txt"

# Ensure success and failed files are empty before starting
> "$success_file"
> "$failed_file"

# Check if the hosts file exists
if [[ ! -f "$hosts_file" ]]; then
  echo "Hosts file '$hosts_file' not found!"
  exit 1
fi

# Loop through each host in the file and add it to Demeter
while IFS= read -r host; do
  # Skip empty lines or lines that start with a #
  if [[ -z "$host" || "$host" == \#* ]]; then
    continue
  fi

  # Check if the host returns JSON data
  if curl -s -I "$host" | grep -q "Content-Type: application/json"; then
    # Add host to Demeter and log to success file if successful
    if demeter host add "$host"; then
      echo "$host" >> "$success_file"
      echo "Successfully added $host"
    else
      echo "$host" >> "$failed_file"
      echo "Failed to add $host to Demeter"
    fi
  else
    # Log to failed file if response format is invalid
    echo "$host" >> "$failed_file"
    echo "Skipping $host: Invalid response format"
  fi
done < "$hosts_file"

# Move original hosts file to a backup and replace it with an empty file
mv "$hosts_file" "${hosts_file}.bak"
> "$hosts_file"

# Calculate success and failure statistics
success_count=$(wc -l < "$success_file")
failed_count=$(wc -l < "$failed_file")

# Display the stats
echo "Processing complete."
echo "Total successful hosts: $success_count"
echo "Total failed hosts: $failed_count"
echo "See '$success_file' for successful hosts and '$failed_file' for failed hosts."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants