Skip to content

Instantly share code, notes, and snippets.

@bkreider
Created June 14, 2014 22:10
Show Gist options
  • Save bkreider/9d0168ad63daff13a0ad to your computer and use it in GitHub Desktop.
Save bkreider/9d0168ad63daff13a0ad to your computer and use it in GitHub Desktop.
remote ipython
#!/bin/bash
#
# remote ipython:
#
# This starts a remote ipython kernel. Downloads the kernel connection file,
# and starts a local ipython console connected to the remote machine.
#
# Features:
# - Automatically starts remote kernel
# - Copies remote file into ~/.ripython/<hostname.json>
# - Will re-use exising kernels -- you must delete the connection file
# if the kernel is killed. It is not smart about remote state.
#
# Todo:
# - Add real arg parsing
# - Add options
# - kill: kill remote kernel and remove connection file
# - allow multiple connections to a machine "-c 1" => <hostaname>-1.json
# - List remote kernels
# $1 user@hostname
if [ $# -lt 1 ]; then
echo "Usage: $0 [user@]hostname"
exit 1
fi
sep='@'
case $1 in
(*"$sep"*)
user=${1%%"$sep"*}
host=${1#*"$sep"}
;;
(*)
# Username not found
user=
host=$1
;;
esac
config_file=~/.ripython/${host}.json
function start_kernel
{
# Start remote kernel
ssh $1 -f 'nohup ipython kernel &' > /dev/null
# Grab config file name
sleep 1 # give ipython time to create file
config_path=$(ssh $1 'ls -rt ~/.ipython/profile_default/security/kernel-* | tail -n 1')
#filename=$(basename $config_path)
# download config file
mkdir -p ~/.ripython
scp $1:${config_path} ${config_file} > /dev/null
}
if [[ -e ${config_file} ]]
then
echo "Connection file already exists here: ${config_file}"
else
echo "Starting new remote kernel"
start_kernel $1
fi
# connect ipython to remote machine
cmd="ipython console --existing ~/.ripython/${host}.json --ssh $1"
echo $cmd
$cmd
if [[ $? -ne 0 ]]
then
echo "Remote ipython kernel must be dead. Please delete ${config_file} and try again"
else
echo "No problems."
fi
@ijstokes
Copy link

That is really nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment