-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcloud_scp.sh
145 lines (116 loc) · 2.38 KB
/
gcloud_scp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#/bin/bash
unset instance_list
unset zone_list
_default_zone=us-central1-c
ayuda()
{
echo
echo "Ayuda:"
echo
echo $0 [user] [instance] src_file [dst_file] [zone]
echo
}
show_instances()
{
printf "\n[*] Instancias:\n\n"
gcloud compute instances list
}
get_instance()
{
cnt_inst=0
for ((i=0; i<$cnt_inst; i++)); do
if [[ "${instance_list[${i}]}" = "$_instance" ]]
then
cnt_inst=$((cnt_inst+1))
fi
done
if [ $cnt_inst -eq 0 ]
then
echo "[-] Error: La instancia especificada no esta corriendo.\n"
exit 2
fi
}
get_zone()
{
cnt_zone=0
for ((i=0; i<$cnt_inst; i++)); do
if [[ "${instance_list[${i}]}" = "$_instance" ]]
then
_zone="${zone_list[${i}]}"
cnt_zone=$((cnt_zone+1))
fi
done
if [ $cnt_zone -eq 0 ]
then
echo "[-] Error: No se encontro zona para la instancia especificada.\n"
exit 2
fi
}
valid_args()
{
_user=$1
_instance=$2
_src_path=$3
_dst_path=$4
_zone=$5
_num_inst=0
_num_zone=0
if [[ $_instance = "" ]]
then
if [ $cnt_inst -gt 0 ]
then
_instance="${instance_list[${_num_inst}]}"
fi
else
get_instance
fi
if [[ $_zone = "" ]]
then
if [ $cnt_inst -gt 0 ]
then
_zone="${zone_list[${_num_zone}]}"
fi
fi
if [ ! -f "$_src_path" ]
then
echo "[-] Error: archivo de origen: $_src_path no existe."
ayuda
exit 2
fi
if [[ $_dst_path = "" ]]
then
_dst_path="~/""$_src_path"
fi
}
fetch_instances()
{
gcloud compute instances list| grep RUNNING > /dev/shm/instances
local src_file=/dev/shm/instances
local line=""
cnt_inst=0
while IFS= read -r line
do
if [[ ! "$line" = "" ]]
then
instance_list[${cnt_inst}]=$(printf '%s\n' "$line")|awk '{print $1;}'
zone_list[${cnt_inst}]=$(printf '%s\n' "$line")|awk '{print $2;}'
cnt_inst=$((cnt_inst+1))
fi
done <"$src_file"
if [ $cnt_inst -eq 0 ]
then
echo "[-] $cnt_inst Instancias activas corriendo.\n"
show_instances
exit 2
else
echo "[+] $cnt_inst Instancia/s activas corriendo...\n"
fi
}
call_gcloud()
{
printf "[*] Copiando archivo...\n"
gcloud compute scp $_src_path $_user@$_instance:$_dst_path --zone $_zone
}
fetch_instances
valid_args $1 $2 $3 $4 $5
call_gcloud