Using UNC paths and ssh with perl

  • Thread starter Thread starter amaccormack
  • Start date Start date
A

amaccormack

I'm trying to set up various XP machines (all with cygwin installed and
sshd running) on our network so that I can run a bunch of regression
tests networked across them. Now, I dont want to have to bother logging
in to each one by hand and adding networked drives that match my own
machine, so I though I could use UNC paths. Here's what I came up with:

sub uncpath {
my $orig_dir = shift;

$orig_dir = `cygpath -m $orig_dir`;
chomp $orig_dir;
if ($orig_dir =~ /^([A-Z])\:/i) {
my $drive = $1;
open GETUNC, "net use|";
NETUSE: while (<GETUNC>) {
if (/^OK\s+$drive\:\s+(\S+)/i) {
my $unc = $1;
$unc =~ s|\\|/|g;
$orig_dir =~ s|$drive\:|$unc|i;
last NETUSE;
}
}
}
return $orig_dir;
}

Maybe there's a better way, but this does seem to work. However, when I
ssh to another machine, it cannot find the UNC path until there is at
least one drive mapped to the server... e.g.

me@mybox ~ ls //serv1/x/y/z
....runs OK...
me@anotherbox ~ ls //serv1/x/y/z
ls: reading directory //serv1: bad file descriptor
me@anotherbox ~ net use z: \\\\serv1\\some\\other\\path
me@anotherbox ~ ls //serv1/x/y/z
....now runs OK...

So, I wondered if there was an easy way to force the other machine to
know abour serv1? I can always put a net use command in my startup
script, but that seems too hacky (although the hackiness is already way
more than I'd like)
 
(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
I'm trying to set up various XP machines (all with cygwin installed and
sshd running) on our network so that I can run a bunch of regression
tests networked across them. Now, I dont want to have to bother logging
in to each one by hand and adding networked drives that match my own
machine, so I though I could use UNC paths. Here's what I came up with:

sub uncpath {
my $orig_dir = shift;

$orig_dir = `cygpath -m $orig_dir`;
chomp $orig_dir;
if ($orig_dir =~ /^([A-Z])\:/i) {
my $drive = $1;
open GETUNC, "net use|";
NETUSE: while (<GETUNC>) {
if (/^OK\s+$drive\:\s+(\S+)/i) {
my $unc = $1;
$unc =~ s|\\|/|g;
$orig_dir =~ s|$drive\:|$unc|i;
last NETUSE;
}
}
}
return $orig_dir;
}

Maybe there's a better way, but this does seem to work. However, when I
ssh to another machine, it cannot find the UNC path until there is at
least one drive mapped to the server... e.g.

me@mybox ~ ls //serv1/x/y/z
...runs OK...
me@anotherbox ~ ls //serv1/x/y/z
ls: reading directory //serv1: bad file descriptor
me@anotherbox ~ net use z: \\\\serv1\\some\\other\\path
me@anotherbox ~ ls //serv1/x/y/z
...now runs OK...

So, I wondered if there was an easy way to force the other machine to
know abour serv1? I can always put a net use command in my startup
script, but that seems too hacky (although the hackiness is already way
more than I'd like)

you might try using ls \\serv1\x\y\z or maybe \\\\serv1\\x\\y\\z
depending on quoting/escaping
(i.e. use backslashes - depending on your implementation of perl, this
may work)
On my machine right now, using forward slashes fails, but backslashes
works. I'm not sure why - maybe the backslashes are passed on to XP
differently than forward slashes. I suspect that slashes are converted
to backslashes when passing the path to XP. Perhaps the difference is
that perl uses native functions if forward slashes are used, but using
backwards slashes makes it pass the job on to the OS (I'm just guessing
here)?
 
Back
Top