automation script for git cmds - bash

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.

Related

How to ssh into remote linux server using username / ip address and password from window using shell script?

What I am trying to do is to login to remote Linux server using SSH from my Windows machine by running a shell script via git bash.
I would like to write a script which will be used by an user with basic IT knowledge. This script will execute a bunch of commands on the remote machine, so it does need to establish a SSH connection.
What I have tried to write in this script so far is:
ssh username#ip <password>
EDIT: You should consider installing Jenkins on the remote system.
Running a command on a remote system can be done via:
ssh user#host command arg1 arg2
If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login. https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login
Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):
Start a ssh connection with a remote
Execute some commands, possibly written in another script (namely script2.sh)
Close the connection / Keep it open for additional command line commands
If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server. If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine. You may copy it in a temporary folder.
In this case, my suggestion is to write script1.sh as follows:
Copy of script2.sh with
scp /path/to/local/script2.sh user#host:/path/to/remote/script2.sh
Switch bash sheel to remote shell with
ssh user#host
Execution of script2.sh
sh /path/to/remote/script2.sh
Instead, if you prefer to list everything in just one script file, you may want to write:
Switch bash sheel to remote shell with
ssh user#host
Execution of commands
echo "This command has been executed on the remote server"
echo "This command has also been executed on the remote server"
..
Possibly closing the SSH connection to prevent that the user execute additional commands
You may consider to copy the ssh-keys on the remote server so to avoid password prompts. More information here.

how to answer to runtime questions with shell script and expect

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

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

Using expect to migrate multiple repositories

I have multiple repositories , which I want to convert to git from svn.
I try to write a simple expect script that convert one repo.
#!/usr/bin/expect
spawn svn2git svn://svn-server/repo --username $username --verbose
expect {Password for '$username'}
send "$password\r"
He puts password but then exit by timeout.
Git launches SSH internally to connect to a remote repository, and SSH detects when it is launched from another program, and disallows to enter passwords from scripts as a security feature.
You will need a specialized program like sshpass to enter passwords into SSH from scripts, or set up SSH keys.
I presume svn2git takes more than a few seconds to complete. Add this after your send command
set timeout -1
expect eof

Shell script to automate SonicWall firewall SSH session not working

I'm trying to write a shell script (Bash) to log into a SonicWall firewall device and issue a command to perform automated backups of the devices ruleset. I prefer to do this in Bash but I will accept a python, perl, except, or applescript solution. If it cannot be done in bash please mention that.
Problems:
1.) SSH server on firewall is custom, a user name and password has to be specified after issuing a
$ ssh server.com
so no matter what username you issue e.g.
$ ssh admin#server.com
the SSH server still presents a username and password box after
2.) The SSH server is minimal and I cannot use public-keys
I tried using a here-document but it isn't working and it results in an immediate "connection closed by remote host".
The command I need to execute takes the form of this:
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
Connecting gives me this:
$ ssh admin#server.com
Copyright (c) 2010 SonicWALL, Inc.
User:
After a username is issued it brings up the password prompt:
User:user1
Password:
I tried a here-document to no avail.
$ ssh server <<+
user1
mypassword
export preferences ftp "ftp.server.com" "user1" "mypassword" "output.exp"
exit
+
Pseudo-terminal will not be allocated because stdin is not a terminal.
Connection to 10.1.1.1 closed by remote host.
I tried using echo to pipe in commands too but that doesn't work either.
Typing the commands in manually works just fine.
Any help on this would be greatly appreciated.
As others have suggested, expect is probably what you want to use here.
Here's a short example of how to work with it from bash to get you started:
login=root
IP=127.0.01
password=helloworld
# +whatever variables you need to use
# Run the expect script from bash
expect_sh=$(expect -c "
spawn ssh $login#$IP
expect \"password:\"
send \"$password\r\"
expect \"#\"
send \"cd $dest_dir\r\"
expect \"#\"
send \"chmod +x $server_side_script $other_script\r\"
expect \"#\"
send \"./$device_side_script\r\"
expect \"#\"
send \"cat results_file\r\"
expect \"#\"
send \"exit\r\"
")
# Output or do something with the results
echo "$expect_sh"
You can automate the ssh session using the original expect, here is a nice article discussing it in detail: http://solar1.net/drupal/automating%20SSH%20with%20expect or the Python module pexepect: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
I'm not a BASH expert but i had to do something where interactive password prompts was causing me a problem.
Basically your script needs to wait to be asked to enter login credentials, and pass them when prompted in order to login, once logged in you can issue the command.
I recommend looking at spawning "expect" sessions. Basically in your script you use expect to basically say "i expect to see password: in the response, when i do, i need to pass in the following data".
Here's the wiki page which helps explain it http://en.wikipedia.org/wiki/Expect
and if you google around you will find lots of help.
that didn't work for me.
I had to pass the variables to the script at launch.
Example launch script login2.sh, with three arguments:
-bash-4.1$ ./login2.sh Jan2**** HIE_SUPER 10.244.112.182

Resources