Skip to content

Instantly share code, notes, and snippets.

@parthpower
Last active October 17, 2018 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parthpower/91b49f66c37019ecd82354a129ae416a to your computer and use it in GitHub Desktop.
Save parthpower/91b49f66c37019ecd82354a129ae416a to your computer and use it in GitHub Desktop.
Pip install for the servers that don't allow outgoing connection.

remote_pip

It's a pain when you have an isolated server with tons of network restrictions (for good) and your code requires 10 python packages with 10 dependenices of each package!

This script addresses that issue by downloading the packages on your machine and SCPing them to the remote machine and installing them there. Not the greates way to do things but this works.

It keeps asking for remote password for ssh and scp, so it's better if you authenticate with public key instead of password and it's safer than password so why not!

Install

mkdir -p ~/.local/bin
curl https://gist.githubusercontent.com/parthpower/91b49f66c37019ecd82354a129ae416a/raw/62caf27dac39c9e47294d8f4adbafaa7cb0f503c/remote_pip > ~/.local/bin/remote_pip
chmod +x ~/.local/bin/remote_pip
echo 'export PATH=~/.local/bin:$PATH'
exec bash

Example

remote_pip root@server jupyter sox 
#!/bin/bash
if [[ $# -lt 2 ]] || [[ $1 = "-h" ]]; then
echo 'Usage: remote_pip <user@host> <package(s)>';
exit 1;
fi
host=$1
packages=${*:2}
temp_dir=$(mktemp -d)
echo "Created Local Temporary Dir at $temp_dir"
echo "Downloading packages locally"
pip download -d $temp_dir $packages
echo $'\n\n\n\n'
echo "Creating Remote Temporary Directory"
echo '====================================='
remote_temp_dir=$(ssh $1 'mktemp -d')
echo $host:$remote_temp_dir
echo $'\n\n\n\n'
echo "Copying Packages to Remote Host"
echo '====================================='
scp $temp_dir/* $host:$remote_temp_dir
echo $'\n\n\n\n'
echo "Installing Packages to Remote Host"
echo '====================================='
# TODO: Refactor this!!!!
ssh $host "cd $remote_temp_dir"'&& for i in $( ls ); do pip install $i; done;'"printf 'You want to run rm -rfi $host:$remote_temp_dir?(y/n)';read yn; if [[ "'$yn'" = 'y' ]]; then echo 'Removing Temporary files at $host:$remote_temp_dir'; rm -rf $remote_temp_dir; fi;"
###
echo '====================================='
echo "Clearing Local Temporary Files at $temp_dir"
rm -rf $temp_dir
echo '====================================='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment