git set remote path to git executable files over ssh - linux

I connect over ssh to a distant machine using this in my ~/.ssh/config:
Host myserver
User myusername
ProxyCommand ssh myserver2 exec nc myserver 22
But when I try to pull the remote git repository, I obtain this error:
% git pull myserver:~/mygitrepository
zsh:1: command not found: git-upload-pack
fatal: The remote end hung up unexpectedly
The problem is that my git executable files are not in standard path defined by $PATH. On the remote machine:
% which git-upload-pack
/c5/shared/git/1.7.6/bin/git-upload-pack
and on the local machine:
% ssh myserver "env | grep PATH"
PATH=/usr/local/bin:/bin:/usr/bin
ssh doesn't read the remote PATH definition in .zshrc or .bashrc. How can I define the PATH to git executable files over ssh?

Actually I've found my answer here. One solution is to set the upload-pack path in the remote using :
git pull --upload-pack=/c5/shared/git/1.7.6/bin/git-upload-pack myserver:~/mygitrepository
in the client.

Related

Permission denied when cloning git repo through shell script

From a server (A) I launch a script called test.sh which contains this code :
#!/bin/bash
ssh login#server-b.com -p 22 'bash $HOME/tmp/git.sh'
exit 0
So from server (B) another script is launched (called git.sh) and contains :
#!/bin/bash
cd $HOME/tmp
git clone ssh://repo_login#my-repo.com:22/home/scripts
exit 0
But the git clone does not work and I get this error message :
Cloning into 'scripts'...
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
But from server (B) If I launch the git.sh script manually, it works.
Do you have an idea why?
Thanks
L.
When running a script through ssh, you need to start the ssh-agent, and add your keys before you can connect to another machine.
Script git.sh should be modified as below (assuming your key is in file ~/.ssh/id_rsa):
#!/bin/bash
cd $HOME/tmp
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
git clone ssh://repo_login#my-repo.com:22/home/scripts
exit 0

finding the name of git branch from remote server

i am trying to find the name of git branch on remote server using a shell script. I put the following command in a script under the bin directory.
git symbolic-ref --short HEAD
When I execute the script using ssh from another machine
ssh -i keyfile.pem user#ipaddress 'bash -s' /path/to/the/script
I get an error
fatal: Not a git repository (or any of the parent directories): .git
Not sure where I am doing wrong. Any help would be appreciated.
Thanks.
Your call to git is using the wrong working directory (likely your home directory).
In your script, either cd to the path containing the git directory or specify the -C option with git:
git -C /path/to/git/checkout symbolic-ref --short HEAD
The -C option allows you to overwrite the working directory:
-C <path>
Run as if git was started in <path> instead of the current working directory. When
multiple -C options are given, each subsequent non-absolute -C <path> is interpreted
relative to the preceding -C <path>.

How can I copy file from local server to remote with creating directories which absent via SSH?

I can copy file via SSH by using SCP like this:
cd /root/dir1/dir2/
scp filename root#192.168.0.19:$PWD/
But if on remote server some directories are absent, in example remote server has only /root/ and havn't dir1 and dir2, then I can't do it and I get an error.
How can I do this - to copy file with creating directories which absent via SSH, and how to make it the easiest way?
The easiest way mean that I can get current path only by $PWD, i.e. script must be light moveable without any changes.
This command will do it:
rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v" root#192.168.0.19:"$PWD/"
I can make the same directories on the remote servers and copy file to it via SSH by using SCP like this:
cd /root/dir1/dir2/
ssh -n root#192.168.0.19 "mkdir -p '$PWD'"
scp -p filename root#192.168.0.19:$PWD/

Script for root to git pull as another user

I have a script that I would like to have do a git pull inside another user's git directory. This script is run by the root user. For example:
cd /home/username/GitProject
sudo -u username -i git pull
When I run this, I get:
fatal: Not a git repository (or any of the parent directories): .git
Is there a way to have my script do a git pull as username?
Try without the -i option to sudo. That option is documented as first changing to the target user's home directory, which undoes the directory change you so carefully do before that. Alternatively, use the appropriate options to git to specify the directory, something like this:
sudo -u username -i git --git-dir=/home/username/GitProject/.git --work-tree=/home/username/GitProject pull
This can be done without sudo. This assumes you have password-less ssh keys since you are talking about a script. Here's the failure:
# git clone <user>#<host>:/path/to/repo
Cloning into 'repo'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
This shows that ~ properly expands to the user's homedir:
# MYUSER=somebody
# su - $MYUSER -c "echo ~"
/home/somebody
And here's the actual command used to clone into the home directory along with some extra proofs:
# su - $MYUSER -c "git clone <user>#<host>:/path/to/repo"
Cloning into 'repo'...
remote: Counting objects: 13, done.
<..>
# ls -l /home/$MYUSER/repo/.git/config
-rw-r--r-- 1 somebody somebody 275 Nov 8 23:55 /home/somebody/repo/.git/config
# su - $MYUSER -c "cd ~/repo; git remote -v"
origin <user>#<host>:/path/to/repo (fetch)
origin <user>#<host>:/path/to/repo (push)
# su - $MYUSER -c "cd ~/repo; git pull"
Already up-to-date.

execute code in remote linux machine

I need to execute shell script on my remote linux machine. Do you know any tools that can help me doing that?
Thanks,
You can connect through ssh passing a command as a parameter:
ssh user#remote.ip.address "~/myscript.sh"
To connect without password, use ssh keys. To use keys, you have to generate a pair at your machine, with the command:
ssh-keygen
Then take the contents of the file ~/.ssh/id_rsa.pub (or id_dsa.pub if you use parameter -t dsa in ssh-keygen) and put in the file ~/.ssh/authorized_keys of the remote_machine. The .ssh dir must have permission 700.
For Python you can use Paramiko to run commands on the remote computer over SSH.
Passwordless remote execution without waiting for remote script to complete before terminating the ssh connection:
ssh -i ~/.ssh/id_rsa user#remote nohup sh /path/to/script.sh > /dev/null 2>&1 &
If you want to execute a local script on remote host
ssh remotePassword#remoteHost < localScript.sh
If you want to invoke a script on remote host
ssh remotePassword#remoteHost "remoteScript.sh"

Resources