mirror of
https://github.com/letic/Shell-Scripts.git
synced 2024-09-28 05:56:04 +00:00
Update scp-remote-to-local.sh
This commit is contained in:
parent
a5bf22a8e8
commit
3916ae39f7
@ -44,20 +44,27 @@ fi
|
|||||||
|
|
||||||
logdir=$(mktemp -d logs.XXXX)
|
logdir=$(mktemp -d logs.XXXX)
|
||||||
|
|
||||||
data=($(curl --silent --cert $cert "$api/instances" | jq --raw-output ".[] | .id,.private_ip_address"))
|
data=($(curl --silent --cert $cert "$api/instances" | jq --raw-output ".[] | .id,.private_ip_address,.launch_time"))
|
||||||
data_len=$((${#data[@]} / 2)) # we know we'll always have a pair of data -> <id>,<ip>
|
data_len=$((${#data[@]} / 3)) # we know we'll always have a triad of data -> <id>,<ip>,<launch_time>
|
||||||
|
|
||||||
for ((n = 0; n < $data_len; n++))
|
for ((n = 0; n < $data_len; n++))
|
||||||
do
|
do
|
||||||
ssh_success=false
|
ssh_success=false
|
||||||
valid="current"
|
valid="current"
|
||||||
|
|
||||||
id=$(($n * 2))
|
# parse array indexes needed to extract data
|
||||||
|
id=$(($n * 3))
|
||||||
ip=$(($id + 1))
|
ip=$(($id + 1))
|
||||||
|
ti=$(($id + 2))
|
||||||
|
|
||||||
instance_id=${data[$id]}
|
instance_id=${data[$id]}
|
||||||
instance_ip=${data[$ip]}
|
instance_ip=${data[$ip]}
|
||||||
|
launch_time=${data[$ti]}
|
||||||
|
|
||||||
|
printf "\n######################################\n"
|
||||||
|
printf "\nrequesting ssh access for: $instance_id\n"
|
||||||
|
|
||||||
|
# use cosmos api to generate ssh access token
|
||||||
response=$(curl --silent \
|
response=$(curl --silent \
|
||||||
--cert $cert \
|
--cert $cert \
|
||||||
--header "Content-Type: application/json" \
|
--header "Content-Type: application/json" \
|
||||||
@ -65,6 +72,7 @@ do
|
|||||||
--data "{\"instance_id\":\"$instance_id\"}" \
|
--data "{\"instance_id\":\"$instance_id\"}" \
|
||||||
"$api/logins/create")
|
"$api/logins/create")
|
||||||
|
|
||||||
|
# parse token from api response
|
||||||
checkpoint_id=$(echo $response | jq --raw-output .url | cut -d '/' -f 7)
|
checkpoint_id=$(echo $response | jq --raw-output .url | cut -d '/' -f 7)
|
||||||
|
|
||||||
until $ssh_success
|
until $ssh_success
|
||||||
@ -74,16 +82,18 @@ do
|
|||||||
if [ "$status" = "$valid" ]; then
|
if [ "$status" = "$valid" ]; then
|
||||||
ssh_success=true
|
ssh_success=true
|
||||||
printf "\n"
|
printf "\n"
|
||||||
echo "OK we've got SSH access to instance number $(($n + 1)): $instance_id ($instance_ip)"
|
echo "ssh access granted for instance $(($n + 1)): $instance_id ($instance_ip)"
|
||||||
printf "\n"
|
printf "\n"
|
||||||
else
|
else
|
||||||
echo "Sorry, still waiting access to: $instance_id (status == $status)"
|
echo -ne "status == $status "\\r
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
scp -r "$user@$instance_ip,eu-west-1:/var/log/component/app.log" "./$logdir/$instance_ip.log" &
|
scp -r "$user@$instance_ip,eu-west-1:/var/log/component/app.log" "./$logdir/$launch_time-$instance_ip.log"
|
||||||
done
|
done
|
||||||
|
|
||||||
wait
|
# removed 'wait' command here and '&' backgrounding of scp process
|
||||||
|
# as stdout feedback was getting interleaved and really confusing
|
||||||
|
|
||||||
echo "Complete"
|
printf "\n######################################\n\n"
|
||||||
|
echo "all logs copied successfully"
|
||||||
|
Loading…
Reference in New Issue
Block a user