Skip to content

Instantly share code, notes, and snippets.

@gugarosa
Last active October 8, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugarosa/08fc418b138c847d26000a9a18ac13dc to your computer and use it in GitHub Desktop.
Save gugarosa/08fc418b138c847d26000a9a18ac13dc to your computer and use it in GitHub Desktop.
Logs into a host via SSH, runs a command and dumps the file to local machine via SFTP.
#!/bin/bash
# User to enter the host
USER=username
# Script to run in host
# In this case, we will be dumping the process my memory usage order
SCRIPT="ps aux --sort -%mem"
# List of hosts
HOSTS="host@host.com"
# For every host in the list
for HOST in ${HOSTS};
do
# Access the host with SSH and runs the script, producing an output file
ssh -l ${USER} ${HOST} "${SCRIPT} > ${HOST}.txt"
# Acess the host with SFTP
sftp ${USER}@${HOST} <<EOF
get ${HOST}.txt
EOF
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment