Skip to content

Commit

Permalink
Add sshfsmounts
Browse files Browse the repository at this point in the history
  • Loading branch information
athomason committed Sep 20, 2010
1 parent a8f75a0 commit 5591a34
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ perlwhich
sizes
Pretty-printing analog of du(1)

sshfsmounts
Attempt to load sshfs mounts listed in a config file.

su-with-authsock
Opens a subshell as another user (a la su(1)), after making the SSH_AUTH_SOCK
and/or .Xauthority files be usable by the target user. This allows ssh/X
Expand Down
61 changes: 61 additions & 0 deletions sshfsmounts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/perl

# Attempt to load sshfs mounts listed in a config file. Unless --no-need-key
# is given, script will silently bail if public-key auth is not available
#
# Config format:
# remotehost:/remote/path /local/path

use strict;
use warnings;

use Getopt::Long;

GetOptions(
'mounts=s' => \(my $mounts), # list of desired mounts
'need-key!' => \(my $need_key = 1), # bail unless 'ssh-add -l' reports available keys
'sshfs=s' => \(my $sshfs = '/usr/bin/sshfs'), # path to sshfs
'mtab=s' => \(my $mtab = '/proc/mounts'), # path to list of current mounts
'verbose!' => \(my $verbose = 0), # more output
);

sub verbose { return unless $verbose; $_[0] .= "\n"; warn @_ }

exit if $need_key && system "ssh-add -l >/dev/null 2>&1";

$mounts ||= shift;
unless ($mounts) {
my $home = (getpwuid $<)[7];
$mounts = "$home/.ssh/sshfs-mounts";
}

# get list of desired mounts
verbose "loading mounts from $mounts";
my %mounts;
open my $mounts_fh, '<', $mounts or die "could not open $mounts: $!\n";
while (<$mounts_fh>) {
chomp;
s/^\s+//;
next if /^#/;
next if /^$/;
my ($mount, $dir) = split;
$mounts{$mount} = $dir;
}

# ignore any that are already mounted
open my $mtab_fh, '<', $mtab or die "could not open $mtab: $!\n";
while (<$mtab_fh>) {
chomp;
next unless /sshfs#(\S+)/;
my $live = $1;
if (exists $mounts{$live}) {
verbose "alive: $live\n";
delete $mounts{$live};
}
}

# mount the remaining unmounted ones
for my $mount (sort keys %mounts) {
verbose "dead: %s --> %s\n", $mount, $mounts{$mount};
system $sshfs, $mount, $mounts{$mount};
}

0 comments on commit 5591a34

Please sign in to comment.