0

I've created a bash script to push the git branch to remote (local server). The bash script works perfectly but I've to give password for 3 major operations.

#!/bin/bash

for ARGUMENT in "$@"
do
    KEY=$(echo $ARGUMENT | cut -f1 -d=)
    VALUE=$(echo $ARGUMENT | cut -f2 -d=)   

    case "$KEY" in
            API)              API=${VALUE} ;;
            SERVICE)    SERVICE=${VALUE} ;;  
            MIGRATION)    MIGRATION=${VALUE} ;;     
            *)   
    esac    


done
if [ -z "$API" ]
then
    API=$(cd /var/www/html/folder1 && git describe --contains --all HEAD)
    # TERMINAL PROMPTS FOR PASSWORD FOR PUSH
    cd /var/www/html/folder1 && git push local $API
fi

if [ -z "$SERVICE" ]
then
    SERVICE=$(cd /var/www/html/folder2 && git describe --contains --all HEAD)
    # TERMINAL PROMPTS FOR PASSWORD FOR PUSH
    cd /var/www/html/folder2 && git push local $SERVICE
fi

# TERMINAL PROMPTS FOR PASSWORD FOR PUSH
ssh user@192.168.0.247 <<-ENDSSH
    cd /home/user/clear/folder1
    echo "Changing branch to: $API"
    git checkout $API
    #sbt publishLocal
    cd /home/user/clear/folder2 
    echo "Changing branch to: $SERVICE"
    git checkout $SERVICE
    #sbt "runMain project"
ENDSSH

Currently I need to give password on:

git push local $API
git push local $SERVICE
ssh user@192.168.0.247

Is there any way I could pass the password once and work for all operations.

Here,

local - Repository is the same server with IP 192.168.0.247

phd
  • 82,685
  • 13
  • 120
  • 165
Sujit Baniya
  • 895
  • 9
  • 27
  • 1
    Why can't you prompt for the password once and feed it to all the operations? – Inian May 10 '19 at 12:06
  • How could I do that? Currently If I run the script it ask me the password 3 times – Sujit Baniya May 10 '19 at 12:08
  • Use the `read` command with `-s` flag that hides the password entered in the terminal. Store the password in the variable and find a way to pass it to to your `git` and `ssh` command without manual intervention – Inian May 10 '19 at 12:10
  • What is the protocol for `local`? `ssh://` or `https://`? – phd May 10 '19 at 12:55
  • @phd It's ssh:// – Sujit Baniya May 10 '19 at 14:17
  • @Inian I'm looking for the same ` find a way to pass it to to your git and ssh command without manual intervention ` – Sujit Baniya May 10 '19 at 14:18
  • https://stackoverflow.com/search?q=%5Bgit%5D+%5Bssh%5D+authentication – phd May 10 '19 at 14:21
  • Generate an ssh key pair, send the public key to the server (to account `user`), verify with `ssh user@192.168.0.247`. To protect the private key at your computer you can use a passphrase but then you need to use `ssh-agent` and `ssh-add` to avoid typing the passphrase every time. – phd May 10 '19 at 14:23
  • Maybe [this](https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html) will help? – Paul Hodges May 10 '19 at 15:11
  • Or [this](https://help.github.com/en/articles/connecting-to-github-with-ssh). There are millions articles all over SO and the web. – phd May 10 '19 at 15:23
  • @phd You're not getting the issue! I want to have solution in bash script that would suffice me to enter password only once for all operation. I know how to setup SSH, using SSH for Git – Sujit Baniya May 10 '19 at 16:10
  • With properly configured ssh keys you don't need any password at all. – phd May 10 '19 at 16:35

0 Answers0