-
Notifications
You must be signed in to change notification settings - Fork 3
/
scpp
executable file
·91 lines (79 loc) · 2.87 KB
/
scpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl
# Copyright 2014, Dee Newcum. License: GPL v3.
# This is a helper script for copying a file from one computer to another.
#
# It outputs the path to the specified file, in a form that's useful for scp'ing from another computer.
#
# "SCPP" stands for "SCP path".
#
#
# Example:
#
# On computer you're copying the file FROM:
#
# $ cd ~/some/directory
#
# $ ls
# local_file.1
# local_file.2
# local_file.3
#
# $ scpp ./local_file.2
# myuser@somehost.com:/home/myuser/some/directory/local_file.2
#
#
# Then, paste that string to the computer you're copying the file TO:
#
# $ scp myuser@somehost.com:/home/myuser/some/directory/local_file.2 .
#
use strict;
use warnings;
use File::Spec;
@ARGV or die "Please specify a file\n";
############### hostname ################
my $host;
if (qx|uname| =~ /^SunOS$|^AIX$/) {
($host = qx[hostname]) =~ s/\s//g;
} else {
($host = qx[hostname --long]) =~ s/\s//g;
}
if (-e "$ENV{HOME}/.hostname_override") { # 'hostname' doesn't always return the correct name/address for other computers to be able to contact it
if (-x "$ENV{HOME}/.hostname_override") {
$host = qx($ENV{HOME}/.hostname_override);
} else {
open FIN, "$ENV{HOME}/.hostname_override";
$host = do {local $/=undef;<FIN>}; # File::Slurp
close FIN;
}
chomp $host;
}
############### username ################
my $username = (getpwuid $<)[0];
if (-e "$ENV{HOME}/.username_override") { # sometimes we want to override the username (if we 'sudo' a lot, for instance)
open FIN, "$ENV{HOME}/.username_override";
($username = do {local $/=undef;<FIN>}) =~ s/[\n\r]$//s;
close FIN;
}
foreach my $file (@ARGV) {
-e $file or warn "Specified file '$file' not found\n";
my $abs = File::Spec->rel2abs($file);
$abs .= "/" if -d $file;
my $path = '';
if (exists $ENV{CLEARCASE_ROOT} && $abs =~ m#^/vobs/#) {
$path = "$ENV{CLEARCASE_ROOT}$abs";
} else {
$path = "$abs";
}
die "scpp can't currently handle single-quotes or backslashes in filenames, due to problems with shell double-quoting.\n"
if ($path =~ /[\\']/);
if ($path =~ /[ \$\\]/) {
# The string gets shell-expanded on the far side of scp (this is what allows us to pass "*" to the far side -- it gets expanded on the REMOTE side, not the local side)
#
# Since this string is usually pasted onto the command-line, it actually needs to be DOUBLE escaped.
$path = qq[\\''$path'\\'];
}
print "$username\@$host:$path\n";
print "scp://$username\@$host//$path\n\n";
# ^^^^ outputs a string that works with Vim for remote-editing
# http://vim.wikia.com/wiki/Editing_remote_files_via_scp_in_vim
}