how to answer to runtime questions with shell script and expect - bash

When I do
git push
I get the command prompt like
Username for 'https://github.com':
then I enter my username manually like
Username for 'https://github.com': myusername
and then I hit Enter and I get prompt for my password
Password for 'https://myusername#github.com':
I want the username to be written automatically instead of manually having to type it all the time.
i tried expect but could not use it
can anyone help me to write a shell script for it?plz:)

Well here is a very small script that would do what you want
#!/bin/expect
spawn git push origin master
expect "Username for 'https://github.com': "
send "MY_NAME_IN_GITHUB"
interact
This way you just have to type your password. You can bind this script to some git command and it can take in extra parameters so that it changes your branches.
But I would advise you to create yourself ssh keys and use them. Here is a great tutorial how to do that :) ssh_tutorial and connect GH to ssh

Related

automation script for git cmds

i'm trying to write a bash script that will automate some of the operation that we are doing on daily basic:
1. clone different repos
2. checkout some specific commits
3. compile
4. etc.
my challenge is, that I want this script to be used by different users and I don't want the script to prompt for password for each git cmd.
I thought to get the username + pwd as arguments to the script and then
git clone https://username:pasword#gitlab.../repoName/usename/proj.git
but I always get an error of "TCP connection reset by peer".
obviously running ssh command or https command without the username builtin working for me fine
any idea?
If you must use "https" instead "ssh" try with expect script.
#!/usr/bin/expect -f
spawn git pull
expect "Username"
send "put here your username\r"
expect "Password"
send "put here your password\r"
interact
You can use bash and expect in the same script as well.

bash script to get pull from local github

I wrote a bash script to get pulled automatically from master repository After it run that means auto enter the username and password. But it's not working. After executing fist command it again asked for a username.
my script is like this
#!/usr/bin/expect
git pull origin master
expect "Username for 'http://192.168.2.xx:xxx':"
send "myUsername\r"
expect "Password for 'http://192.168.2.xx:xxx':"
send "myPassword\r"
interact
Please help me out. Where I am mistaken

Enter password for git pull in script

I would like to create a script/tool which runs through my repositories and makes git pull. Script asks me a pass from GitLab/Bitbucket/whatever, I enter it and script synchronizes all repos hosted there. The problem I have faced: I have no idea how to enter password automatically.
For example, options like these do not work:
yes ${STORED_PASSWORD} | git pull
echo ${STORED_PASSWORD} | git pull
From googling and reading Stack Overflow, I have understood:
Passing password as a part of url is not safe;
Credentials helper needs to enter password once for each remote repo and it will continue to store passwords after script will finish its work.
I use Windows's git shell, and there is no expect tool. Are there any options or workarounds?
As others have pointed out, using SSH instead of HTTPS makes perfect sense in your use case, as it is less unwieldy and won't have you accidentally reveal your password to other users of your machine.
If you insist on using HTTPS and passing your password, this approach will work:
git pull https://myusername:${STORED_PASSWORD}#mygithost.com/my/repository

URI Save Password inside the link

Is there a way to save the password of a ssh-connection inside an uri-link. AFAIK a uri can look like this username:password#domain/path. But the following example doesn't work on ubuntu:
ssh user:pass#domain/path
I always receive a "please enter password"-question. I know that it is not a quite secure way to save the password in plain text inside a link, but I have to work with other developers and what should I say... they are ex-Windows user, they don't like terminals and therefore I want to write a tiny shell script. this script should clone a remote git repo and create some specific stuff.
One click and I should do some magic!
You should use a ssh-key generated with ssh-keygen (man ssh-keygen). This is also available on the windows platform within the putty environment.
eval $(ssh-agent)
ssh-add ssh./yourkeyfilewithoutpassphrase
ssh user#sshserver "your remote command"
Befor you can use your ssh-key in the remotehost, you must insert the public key to the authorized_keys file. A convenient way is the command
ssh-copy-id -i ssh./yourkeyfilewithoutpassphrase.pub user#sshserver
or, if the key is already loaded by the ssh-agent
ssh-copy-id user#sshserver
After this point, you dont need any password for ssh connection to established remote hosts. You should use per user a different ssh-key, so you are able to enable and disable keys without bothering the other users.
You can't login with input password using ssh.
Another alternate way is setup a pair of ssh-keys, and login using ssh-key.
I follow the guide here: http://www.softwareprojects.com/resources/programming/t-ssh-no-password-without-any-private-keys-its-magi-1880.html

SSH in shell script with password

I want to write one shell script like
command1
ssh vivek#remotehost
fire command on remote host
Now I have password in pass.txt . But when I change stdin with file. It is not reading password from file.
script.sh < password.txt
It is prompting for the password in place of reading password from the file.
What I am doing wrong ?
Second problem is that shell script don't shows the command fired. Is there a way , I can show fired command from it ?
Note :
I don't have key based access on remote system. I can only use password based login for ssh.
You can use ssh-agent or expect (the programing language) to do this.
OpenSSH ssh does not reads the password from stdin but from /dev/tty. That's why you have to use Expect or some other similar tool to automate it.
plink is another client, also available for Linux/Unix that accepts the password as a parameter on the command line... though that has some ugly security implications.
Okay, just to mention yet another option: sshpass is a tool developed for exactly the task of "fooling" regular openssh client to accept password non-interactively.

Resources