Skip to content

Instantly share code, notes, and snippets.

Last active June 19, 2019 06:51
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Use mycli ( through ssh
$ mycli-ssh yourhost [extra args passed to mycli]
import subprocess
import sys
import atexit
import socket
host = sys.argv[1]
cfg = {}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0))
ip, port = s.getsockname()
port = str(port)
p = subprocess.Popen(['ssh', '-L', '%s:localhost:3306' % port, '-Nf', host],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print('SSH tunnel process is: %s' %
cnf = subprocess.check_output(['ssh', host, 'cat', '~/.my.cnf'],
for line in cnf.split('\n'):
if '=' in line:
k, v = line.split('=')
cfg[k.strip()] = v.strip()
cmd = [
'mycli', '-u', cfg['user'], '-p', cfg['password'],
'-h', 'localhost', '-P', port, '-R', r'\t %s:\d> ' % host
] + sys.argv[2:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment