Created
April 22, 2019 20:56
-
-
Save kapitanluffy/64a95685e61dabcc1eabc267b5b654a2 to your computer and use it in GitHub Desktop.
Mount directory to specified drive letter with fallback to achieve ubiquity across networks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: Mount directory to specified drive letter with fallback to achieve ubiquity across networks | |
:: Checks existence of host by the following order | |
:: - Check host in LAN | |
:: - Check host in Remote | |
:: - Check host in Local | |
:: Since Windows does not have native support for sftp/ssh we use sshfs (FUSE) instead | |
:: sshfs protocol support using WinFSP (https://github.com/billziss-gh/winfsp) | |
:: config | |
set driveletter=Z: | |
set lanhost=192.168.0.3 | |
set lanhome=\\192.168.0.3\home\jim | |
set lanuser=jim | |
set lanpass=password | |
set remoteport=2222 | |
set remotehome=\\sshfs\jim@localhost!2222 | |
set remoteuser=jim | |
set remotepass=password | |
set localhome=\\localhost\c$\%homepath%\home | |
:: config | |
call :lan_host_check %lanhost% | |
IF %ERRORLEVEL% EQU 0 goto :map_lan | |
for /F %%i in ("%remoteport%") do call :open_port_check %%i | |
IF %ERRORLEVEL% EQU 0 goto :map_remote | |
IF EXIST %localhome% goto :map_local | |
goto :eof | |
:map_local | |
echo mapping %driveletter% to %localhome% | |
net use %driveletter% %localhome% | |
goto :eof | |
:map_remote | |
echo mapping %driveletter% to %remotehome% | |
net use %driveletter% %remotehome% "%remotepass%" /USER:%remoteuser% | |
goto :eof | |
:map_lan | |
echo mapping %driveletter% to %lanhome% | |
net use %driveletter% %lanhome% "%lanpass%" /USER:%lanuser% | |
goto :eof | |
:lan_host_check | |
ping -n 1 %1>nul | |
goto :eof | |
:open_port_check | |
netstat -an | find "LISTENING" | find ":%1">nul | |
goto :eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment