Skip to content

Instantly share code, notes, and snippets.

@annalinneajohansson
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save annalinneajohansson/b8d39c699db2a4de1d1f to your computer and use it in GitHub Desktop.

Select an option

Save annalinneajohansson/b8d39c699db2a4de1d1f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from paramiko.config import SSHConfig
cfgdir = os.getenv("HOME")+"/.ssh"
cfgfile = cfgdir+"/config"
try:
config_file = file(cfgfile)
except IOError:
if not os.path.exists(cfgdir):
os.makedirs(cfgdir, 0700)
f = open(cfgfile, 'w')
o = '# SSH config file, \'man ssh_config\' for more details.\n\n'
o += '#Host example\n'
o += '# hostname example.com\n'
o += '# user joebloggs\n'
f.write(o)
f.close()
os.chmod(cfgfile, 0600)
config_file = file(cfgfile)
config = SSHConfig()
config.parse(config_file)
hosts = config._config
else:
config = SSHConfig()
config.parse(config_file)
hosts = config._config
print '<openbox_pipe_menu>\n'
if len(hosts) >= 2:
for h in hosts[1:]:
if 'host' in h and 'hostname' in h['config']:
conf = h['config']
user = ''
if 'user' in conf:
user = '-l ' + conf['user'] + ' '
port = ['', '']
if 'port' in conf:
port[0] = '-p ' + conf['port'] + ' '
port[1] = ':' + conf['port']
print '<menu id="ssh-'+h['host'][0]+'" label="'+h['host'][0]+'">'
print ' <item label="Start terminal session">'
print ' <action name="Execute">'
print ' <command>'
print ' x-terminal-emulator -e "ssh '+h['host'][0]+'"'
print ' </command>'
print ' </action>'
print ' </item>\n'
print ' <item label="Browse with File Manager">'
print ' <action name="Execute">'
print ' <command>'
print ' exo-open --launch FileManager ssh://' + conf['hostname'] + port[1]
print ' </command>'
print ' </action>'
print ' </item>\n'
print '</menu>\n'
print '<separator/>\n'
print '<item label="Edit ~/.ssh/config">'
print ' <action name="Execute">'
print ' <command>'
print ' exo-open ~/.ssh/config'
print ' </command>'
print ' </action>'
print '</item>\n'
print '</openbox_pipe_menu>'
@annalinneajohansson
Copy link
Author

Remember to replace '/home/user/.ssh/config' with the correct path for you

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