• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

script

Logging In to AWS and GCP

January 22, 2023

Here’s my Bash script to login to both AWS and GCP. It has a little bit of intelligence. It checks if you are already logged in, and skips if you are. If not logged in, it will open up the cloud web console which is protected by Okta. The script has been redacted and replace with generic usernames and web pages for security reasons.

#!/bin/bash
## CHECK IF LOGGED IN TO GCP 
read -p "Login to GCP? (y/n) : " login_gcp
if [[ $login_gcp = "y" ]]; then
  file1="/Users/username/code/etc/auth-gcp.txt"
  gcloud auth print-identity-token 1> /dev/null 2> $file
  user=$(gcloud config list account --format "value(core.account)")
  auth=$(cat "$file" | head -n 1)
  rm -f $file1
  if [[ $auth == "Reauthentication required." ]] ||  [[ $user != "first.last@domain.com" ]]; then
    echo "Logging in to Google Cloud Platform."
    gcloud auth login
    gcloud auth application-default login
    open https://okta-login
  else
    echo "You are already logged in to Google Cloud Platform."
  fi
else
  echo "Skipping GCP ... "
fi
## CHECK IF LOGGED IN TO AWS
read -p "Login to AWS? (y/n) : " login_aws
if [[ $login_aws = "y" ]]; then  
  file2=""/Users/username/code/etc/auth-aws.txt""
  aws sts get-caller-identity 2> $file2
  expired=$(tail -n +2 "$file2")
  rm -f $file2
  if [[ $expired =~ "expired" ]] || [[ $expired =~ "Unable" ]]; then
    open https://okta-login
    echo "please wait until web page loads ... "
    read -p "Press any key to continue... " -n1 -s
    echo ""
    basecred='/Users/username/.aws/credentials.base'
    newcreds='/Users/username/Downloads/credentials'
    creds='/Users/username/.aws/credentials'
    if [ ! -f $newcreds ]; then
      echo 'No AWS credentials.'
      exit
    else
      cat $newcreds $basecred > $creds
      echo 'New AWS credentials.'
      sleep 3
      rm -f $newcreds
    fi
  else
    echo "You are already logged in to AWS."
  fi
else 
  echo "Skipping AWS ... "
fi

#!/bin/bash ## CHECK IF LOGGED IN TO GCP read -p "Login to GCP? (y/n) : " login_gcp if [[ $login_gcp = "y" ]]; then file1="/Users/username/code/etc/auth-gcp.txt" gcloud auth print-identity-token 1> /dev/null 2> $file user=$(gcloud config list account --format "value(core.account)") auth=$(cat "$file" | head -n 1) rm -f $file1 if [[ $auth == "Reauthentication required." ]] || [[ $user != "first.last@domain.com" ]]; then echo "Logging in to Google Cloud Platform." gcloud auth login gcloud auth application-default login open https://okta-login else echo "You are already logged in to Google Cloud Platform." fi else echo "Skipping GCP ... " fi ## CHECK IF LOGGED IN TO AWS read -p "Login to AWS? (y/n) : " login_aws if [[ $login_aws = "y" ]]; then file2=""/Users/username/code/etc/auth-aws.txt"" aws sts get-caller-identity 2> $file2 expired=$(tail -n +2 "$file2") rm -f $file2 if [[ $expired =~ "expired" ]] || [[ $expired =~ "Unable" ]]; then open https://okta-login echo "please wait until web page loads ... " read -p "Press any key to continue... " -n1 -s echo "" basecred='/Users/username/.aws/credentials.base' newcreds='/Users/username/Downloads/credentials' creds='/Users/username/.aws/credentials' if [ ! -f $newcreds ]; then echo 'No AWS credentials.' exit else cat $newcreds $basecred > $creds echo 'New AWS credentials.' sleep 3 rm -f $newcreds fi else echo "You are already logged in to AWS." fi else echo "Skipping AWS ... " fi

Filed Under: Cloud, Linux Tagged With: aws, gcp, login, script

AWS Create Volume From Snapshot

January 18, 2023

Here’s a bash script that creates a volume from a snapshot in AWS.

#!/bin/bash
read -p "snapshotId : " snapshot
read -p "server     : " server
read -p "tag1       : " tag1
read -p "tag2       : " tag2
read -p "region     : " region
read -p "zone       : " zone
read -p "profile    : " profile
aws ec2 create-volume \
--availability-zone $zone \
--encrypted \
--iops 3000 \
--volume-type gp3 \
--snapshot-id $snapshot \
--tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value="$server"},{Key=tag1,Value="$tag1"},{Key=tag2,Value="$tag2"}]" \
--region $region \
--profile $profile

#!/bin/bash read -p "snapshotId : " snapshot read -p "server : " server read -p "tag1 : " tag1 read -p "tag2 : " tag2 read -p "region : " region read -p "zone : " zone read -p "profile : " profile aws ec2 create-volume \ --availability-zone $zone \ --encrypted \ --iops 3000 \ --volume-type gp3 \ --snapshot-id $snapshot \ --tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value="$server"},{Key=tag1,Value="$tag1"},{Key=tag2,Value="$tag2"}]" \ --region $region \ --profile $profile

Filed Under: Cloud, Linux Tagged With: bash, create, script, snapshot, volume

GCP Compute Startup Script

February 12, 2022

How to add startup and shutdown scripts on GCP Compute Engine.

Startup Script

gcloud compute instances add-metadata servername \
--project project-id \
--zone us-central1-c \
--metadata=startup-script='#! /bin/bash
sudo -i
echo "Time: $(date)" >> /tmp/date.txt'

gcloud compute instances add-metadata servername \ --project project-id \ --zone us-central1-c \ --metadata=startup-script='#! /bin/bash sudo -i echo "Time: $(date)" >> /tmp/date.txt'

Shutdown Script

gcloud compute instances add-metadata servername \
--project project-id \
--zone us-central1-c \
--metadata=shutdown-script='#! /bin/bash
# Shuts down Apache server
/etc/init.d/apache2 stop'

gcloud compute instances add-metadata servername \ --project project-id \ --zone us-central1-c \ --metadata=shutdown-script='#! /bin/bash # Shuts down Apache server /etc/init.d/apache2 stop'

Filed Under: Cloud Tagged With: compute, gcloud, metadata, script, startup

Bash Script on Startup

December 23, 2021

How to add bash scripts on startup.

update-rc.d

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh
sudo update-rc.d /etc/init.d/yourscript.sh defaults
chmod +x /etc/init.d/yourscript.sh

sudo cp /path/to/yourscript.sh /etc/init.d/yourscript.sh sudo update-rc.d /etc/init.d/yourscript.sh defaults chmod +x /etc/init.d/yourscript.sh

Root Crontab

sudo crontab -e
@reboot /path/to/yourscript.sh

sudo crontab -e @reboot /path/to/yourscript.sh

Filed Under: Linux Tagged With: bash, crontab, script, startup, update-rc.d

Terraform User Data

October 26, 2021

Here’s how to add userdata in Terraform.

user_data = filebase64("${path.module}/example.sh")

user_data = filebase64("${path.module}/example.sh")

Filed Under: Cloud Tagged With: bootstrap, ec2, script, terraform, userdata

Startup Script

April 6, 2021

Here’s how to run a script on bootup on Rhel 7 that’s running systemd.

First create a script. It this case, it will create a file called test.txt.

#!/bin/bash
> /root/test.txt

#!/bin/bash > /root/test.txt

Make the script executable.

chmod /root/test.sh

chmod /root/test.sh

Now create a service in /etc/systemd/system/test.service

# vi /etc/systemd/system/test.service
[Unit]
Description=A test service
After=network.target
[Service]
Type=simple
ExecStart=/root/test.sh
TimeoutStartSec=0
[Install]
WantedBy=default.target

# vi /etc/systemd/system/test.service [Unit] Description=A test service After=network.target [Service] Type=simple ExecStart=/root/test.sh TimeoutStartSec=0 [Install] WantedBy=default.target

Reload systemd.

systemctl daemon-reload

systemctl daemon-reload

Enable the service.

systemctl enable test.service

systemctl enable test.service

Start the service.

systemctl start test.service

systemctl start test.service

Reboot and test.

reboot

reboot

Validate if the script ran. Check if the file was created.

Filed Under: Cloud, Linux Tagged With: bootup, rhel 7, script, systemd

Linux Script

June 30, 2020

The script command records all your keystrokes and output into a file called typescript.

You can start recording by simply typing script.

script
Script started, file is typescript
df -h
ls -l
exit

script Script started, file is typescript df -h ls -l exit

Typing exit stops the recording. Just cat typescript to see your recording.

cat typescript

cat typescript

Filed Under: Linux Tagged With: keystrokes, output, record, script, typescript

Wowza 4.8.0 Scripts

February 22, 2020

Here are the start and stop scripts for the lastest Wowza version 4.8.0.

Start scripts

sudo service WowzaStreamingEngine start
# or
/etc/init.d/WowzaStreamingEngine start

sudo service WowzaStreamingEngine start # or /etc/init.d/WowzaStreamingEngine start

Stop scripts

sudo service WowzaStreamingEngine stop
# or
/etc/init.d/WowzaStreamingEngine stop

sudo service WowzaStreamingEngine stop # or /etc/init.d/WowzaStreamingEngine stop

Filed Under: Cloud Tagged With: 4.8.0, script, start, stop, wowza

  • Go to page 1
  • Go to page 2
  • Go to Next Page »
  • Home
  • About
  • Archives

Copyright © 2023