Skip to content

Instantly share code, notes, and snippets.

@shokoe
Last active February 5, 2018 19:39
Show Gist options
  • Save shokoe/519cc2e86c94d009e0547724bfca3f9f to your computer and use it in GitHub Desktop.
Save shokoe/519cc2e86c94d009e0547724bfca3f9f to your computer and use it in GitHub Desktop.
EC2ulz_ops - minimal tools set for working with AWS from command line
# EC2ulz_ops
echo -ne "\033]0;`hostname`\007"
ec_key='/root/.ssh/my-keypair.pem'
# awscli autocomplete
complete -C '/usr/bin/aws_completer' aws
## _EC2hosts_cache - hostname completion (dynamic cahced)
###################
_EC2hosts_cache='/tmp/EC2hosts.cache'
# with hosts names cache
_EC2hosts() {
COMPREPLY=( $( compgen -W "$([ -e $_EC2hosts_cache ] &&\
[ $((`date +%s`-`stat -c %Y $_EC2hosts_cache`)) -gt 300 ] &&\
cat $_EC2hosts_cache ||\
Eins | sed 1d | awk '{print $1}' | tee $_EC2hosts_cache &&\
chmod 666 $_EC2hosts_cache)" "$2") )
}
# without hosts names cache
#_EC2hosts() { COMPREPLY=( $( compgen -W "$(Eins | sed 1d | awk '{print $1}' )" "$2") ); }
## Econn - ssh instances by name tag
#########
export ec2_sshopt="-o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -i $ec_key"
putty_name(){ echo -ne "\033]0;$1\007"; }
Econn(){
ip=$1
[[ ! $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] && \
ip=`Eins | awk -v N=$1 '$1==N{print $5}' | head -1`
[ -z "$ip" ] && echo "Server '$1($ip)' not found" && return 2
[[ -t 1 ]] && [ -z "$2" ] && putty_name $1
ssh $ec2_sshopt $ip "$2"
[[ -t 1 ]] && putty_name `hostname`
}
complete -F _EC2hosts Econn
export -f Econn
## Ebulk - parallel ssh execution with isntaces name tags regex
#########
ec2_fork_count=10
Ebulk_ssh(){
local debug=0
(($debug)) && echo "Econn $1 '$rcmd'"
o=`Econn $1 "$rcmd" 2>&1`
if [ ! -z "$o" ]; then
echo "$o" | while read l; do
printf "%-28s %s\n" "$1" "$l"
done
else
printf "%-28s %s\n" "$1" "NULL OUTPUT"
fi
}
Ebulk(){
rcmd="$2"; export rcmd;
Eins | grep running | sed 1d | egrep "$1" | awk '{print $1}' | xargs -n 1 -P $ec2_fork_count -i bash -c "Ebulk_ssh {}"
}
export -f Ebulk Ebulk_ssh
## Escp
########
export ec2_scpopt='-3rp'
Escp(){ scp $ec2_scpopt $(echo " $*" | sed "`Eins | awk 'NR!=1{printf "s# "$1":# "$5":#g;"}'`"); }
complete -F _EC2hosts Escp
## Ediff*
##########
Ediff(){ s=( ${1/:/ } ); vimdiff <(Econn ${s[0]} "cat $2") <(Econn ${s[1]} "cat $2"); }
Edifd(){ s=( ${1/:/ } ); vimdiff <(Econn ${s[0]} "ls -la $2") <(Econn ${s[1]} "ls -la $2"); }
Edif5(){
srs=`Eins | sed 1d | awk '{print $1}' | egrep "$1" | head -4 | while read z; do echo -n "<(Econn $z \"find $2 -type f | xargs md5sum | sort -k 2\") "; done`
eval vimdiff $srs
}
complete -F _EC2hosts Ediff Edifd Edif5
## Eprop
#########
fork_prop=7
Eprop_cmd(){ Escp $prop_s:$prop_f $1:$prop_f && echo -n "copied" || echo -n "failed"; echo " - $prop_s:$prop_f > $1:$prop_f"; }
Eprop(){
# syntax: Eprop <source server name>::<target servers regex> <source file>
prop_f=$2; prop_s=${1/::*/}; export prop_f prop_s;
Eins | sed 1d | awk '{print $1}' | egrep "${1/*::/}" | grep -v "$prop_s" | xargs -n 1 -P $fork_prop -i bash -c "Eprop_cmd {}"
}
export -f Eprop_cmd Escp
## Eins
########
Eins(){
(echo "Name ID AMI PubIP IntIP State Type Time Zone Root Spot"
aws ec2 describe-instances --query 'Reservations[*].Instances[?State.Name==`running`].[Tags[?Key==`Name`].Value|[0], InstanceId, ImageId, PublicIpAddress, PrivateIpAddress, State.Name, InstanceType, LaunchTime, Placement.AvailabilityZone, RootDeviceType, InstanceLifecycle]' --output=text | sort) | column -t
}
export -f Eins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment