sshfs-mount

sshfs-mount

Its often needed to edit a bunch of files on a lot of servers. One of the simplest methods to do this is to mount them via sshfs and then edit one by one. Here is handy set of scripts to mount and umount many of them.

 

(file ~/.avk-desktop/zsh/rc/sshfs\aliases.rc which is run from ~/.zsh)

#!/bin/zsh

IDMAP=" -o idmap=user -o uid=1001 -o gid=1001"
setopt extendedglob
REMOTE_ACCOUNTS="
alex@hosting:
vhtest@hosting:
-p 22 root@hosting:/
-p 22 root@db:/
-p 22 root@db2:/
-p 22 root@176.9.42.4:/
-p 2011 root@176.42.4.6:/
-p 2012 root@176.42.4.6:/
-p 2013 root@176.42.4.6:/
-p 2014 root@176.42.4.6:/
"

accounts=("${(f)REMOTE_ACCOUNTS}")

for account in $accounts
do
    hostdir=("${(s/ /)account}")
    port=$hostdir[-2]
    if test -z "$port"
    then
    port="22"
    fi
    hostdir=$hostdir[-1]
    mount_to=("${(s/:/)hostdir}")
    mount_to=$mount_to[1]
    mount_to="${mount_to}_${port}"
    fullpath="$HOME/sshfs/$mount_to/"
    cmd="sshfs ${account} ${fullpath} $IDMAP"
    #print $cmd
    alias sshfs_$mount_to=$cmd
    if test ! -d "$fullpath"; then
        mkdir "$fullpath"
    fi
done

When you need global action: just uncomment "print" option and copy/paste it. Another handy script would do the unmount of all founds sshfs volumes:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import subprocess
import os

SSHFS_LOC = "~/sshfs/"
SUCCESS = 0


def main():
    sshfs_dir = os.path.expanduser(SSHFS_LOC)
    if os.path.exists(sshfs_dir):
        for d in os.listdir(sshfs_dir):
            folder = sshfs_dir + d
            if os.path.isdir(folder):
                if os.path.ismount(folder):
                    cmd = ['fusermount', '-u', folder]
                    p = subprocess.Popen(cmd)
                    sts = os.waitpid(p.pid, 0)[1]
                    print("Command: {} Status: {}".format(
                        cmd,
                        'Success' if sts == SUCCESS else 'Failure'))
    else:
        print("sshfs folder on path: {} does not exist".format(sshfs_dir))


if __name__ == '__main__':
    main()

Comments

Comments powered by Disqus