bash | install launchd | run the same command | Search

These shell commands perform the following actions: create a directory and execute a command if it doesn't exist, copy files using Secure Copy (SCP) from a local directory to a remote EC2 instance, using private key authentication. The commands copy different patterns of files using SCP, including https_www_googleapis, client_secret, and aws-sdk files.

Run example

npm run import -- "copy credentials"

copy credentials

npm run ssh -t "mkdir ~/.credentials || true" \
   && scp -i ~/.credentials/selenium.pem \
   ~/.credentials/https_www_googleapis* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/ \
   && scp -i ~/.credentials/selenium.pem \
   ~/.credentials/client_secret* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/ \
   && scp -i ~/.credentials/selenium.pem \
   ~/.credentials/aws-sdk* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/ \

What the code could have been:

bash
#!/bin/bash

# Define credentials directory and upload files to remote server
REMOTE_SERVER="ec2-54-201-232-148.us-west-2.compute.amazonaws.com"
REMOTE_USER="ubuntu"

# Create credentials directory on remote server if it doesn't exist
ssh -t "$REMOTE_USER@$REMOTE_SERVER" "mkdir -p ~/.credentials || true"

# Define file patterns to upload
UPLOAD_FILES=(
  "~/.credentials/https_www_googleapis*"
  "~/.credentials/client_secret*"
  "~/.credentials/aws-sdk*"
)

# Define private key file
PRIVATE_KEY="~/.credentials/selenium.pem"

# Upload files to remote server
for file_pattern in "${UPLOAD_FILES[@]}"; do
  scp -i "$PRIVATE_KEY" "$file_pattern" "$REMOTE_USER@$REMOTE_SERVER:~/.credentials/"
done

# TODO: Consider using a more secure way to handle private keys and credentials
#       Such as using a key management service or encrypting the credentials

Code Breakdown

Command 1: Create directory and execute command if it doesn't exist

npm run ssh -t "mkdir ~/.credentials || true"

Command 2: Copy files using SCP

scp -i ~/.credentials/selenium.pem \
  ~/.credentials/https_www_googleapis* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/

Command 3 and 4: Copy files using SCP (similar to Command 2)

scp -i ~/.credentials/selenium.pem \
  ~/.credentials/client_secret* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/

scp -i ~/.credentials/selenium.pem \
  ~/.credentials/aws-sdk* ubuntu@ec2-54-201-232-148.us-west-2.compute.amazonaws.com:~/.credentials/

These two commands are identical to Command 2, but copy files with different patterns: client_secret* and aws-sdk* respectively.