-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.sh
executable file
·53 lines (45 loc) · 1.93 KB
/
local.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
#!/bin/bash
# Define the path to your private key file
private_key_path="/Users/surajmandal/Desktop/dev.nosync/cloud/keys/_personal/oracle.cer"
# Function to validate the IP address format
function is_valid_ip {
local ip=$1
# Regular expression to match the IP address format (IPv4 or IPv6)
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || [[ $ip =~ ^[0-9a-fA-F:]+$ ]]; then
return 0 # Valid IP address format
else
return 1 # Invalid IP address format
fi
}
# Function to update or add an entry in the .ssh/config file
function update_ssh_config {
local host=$1
local host_name=$2
if grep -q "^Host $host$" ~/.ssh/config; then
# If the host entry already exists, update the HostName line
sed -i '' -e "s/^ *HostName.*/ HostName $host_name/" ~/.ssh/config
else
echo "" >> ~/.ssh/config
# If the host entry does not exist, add it to the .ssh/config file
echo "Host $host" >> ~/.ssh/config
echo " HostName $host_name" >> ~/.ssh/config
echo " User ubuntu" >> ~/.ssh/config
echo " IdentityFile $private_key_path" >> ~/.ssh/config
fi
}
# Get the public IP addresses of instance-1, instance-2, and instance-3 from the Terraform output
instance_1_ip=$(terraform output -raw public_ip_arm_1 2>&1)
instance_2_ip=$(terraform output -raw public_ip_x86_1 2>&1)
instance_3_ip=$(terraform output -raw public_ip_x86_2 2>&1)
# Update or add the oci1 entry in .ssh/config (if instance_1_ip is a valid IP address)
if is_valid_ip "$instance_1_ip"; then
update_ssh_config "oci1" "$instance_1_ip"
fi
# Update or add the oci2 entry in .ssh/config (if instance_2_ip is a valid IP address)
if is_valid_ip "$instance_2_ip"; then
update_ssh_config "oci2" "$instance_2_ip"
fi
# Update or add the oci3 entry in .ssh/config (if instance_3_ip is a valid IP address)
if is_valid_ip "$instance_3_ip"; then
update_ssh_config "oci3" "$instance_3_ip"
fi