FTP를 통해 휴대폰을 PC에 연결하고 cmd를 사용하여 Windows 탐색기에서 FTP를 열려고 합니다.
다음은 코드입니다:
@echo off
echo Setup connection to ftp 192.168.43.1
ipconfig | findstr /I "192.168.43.1"
:: Opens ipconfig and searches for string "192.168.43.1" in the output of ipconfig to check if 192.168.43.1 is connected
if %errorlevel% == 0 (
echo Connection set
%windir%\explorer.exe ftp://192.168.43.1:8888/
) else (
echo Connection not set
echo Setting up connection...
netsh wlan connect name="sdc-yayjg"
:: "sdc-yayjg" is the host name of 192.168.43.1
timeout 3 > NUL
echo Opening ftp 192.168.43.1
ipconfig | findstr /I "192.168.43.1"
if %errorlevel% == 0 (
echo Connection set
%windir%\explorer.exe ftp://192.168.43.1:8888/
) else (
echo FAIL: Connection not set.
echo Press any key to exit.
pause > NUL
해당 코드를 배치 파일에 저장하고 배치 파일을 실행하면 cmd가 약 밀리초 동안 열린 다음 종료됩니다. 즉, cmd 창이 몇 밀리초 동안 깜박이고 아무 일도 일어나지 않는다는 의미입니다. 연결이 설정되지 않았습니다.
코드는 "findstr" 명령 없이도 잘 작동합니다. 네트워크 상태를 확인하는 것이 매우 중요하기 때문에 해당 명령을 제거할 수 없습니다.
답변1
다음 코드를 사용해 보십시오(향후 사용자를 위해 모듈성을 개선하도록 편집되었으며 더 나은 사용자 경험을 위해 약간 개선된 코드).
@echo off
goto setup
:setup
title FTP Connection w/ network support
SET "FTP=192.168.43.1"
:: Location of the FTP server
SET "FTPPORT=8888"
:: The FTP server's port
SET "WIRELESS=sdc-yayjg"
:: The network where the FTP server resides
SET "TRIES=0"
:: Please do not tamper with.
SET "MAXTRIES=3"
:: How many tries before failure
SET "TIMETOCONNECT=3"
:: How much time to give to connect to the network
echo Setting up connection to FTP %FTP%...
goto check1
:check1
if %TRIES% LSS %MAXTRIES% (
SET /A "TRIES=TRIES+1"
goto check2
) else (
goto Fail
)
:check2
echo Checking for FTP Server presence...
ipconfig | findstr /I "%FTP%"
:: Opens ipconfig and searches for %FTP%. (in this case it's "192.168.43.1") in the output of ipconfig to check if 192.168.43.1 is connected
if %errorlevel% EQU 0 (
goto 0
) else (
echo Presence not found. Assuming connection not set...
goto 1
)
:1
echo Connection not set after %TRIES% times.
echo Setting up connection...
netsh wlan connect name="%WIRELESS%"
:: This script assumes that WIRELESS (sdc-yayjg) houses the IP (192.168.43.1)
timeout %TIMETOCONNECT% > NUL
goto check1
:Fail
echo FAIL: Connection not set after %MAXTRIES% tries.
echo Press any key to exit.
pause > NUL
exit
)
:0
echo Internet Connection set - Connecting to FTP via Windows Explorer...
%windir%\explorer.exe ftp://%FTP%:%FTPPORT%/
echo Connection Sucessful. Windows Explorer should now open...
echo Press any key to close this window.
pause > NUL
exit
위의 코드가 작동하지 않는 경우 원본 코드(일부 외관 변경으로 OP에 따라 작동)는 다음과 같습니다.
@echo off
echo Setting up connection to FTP 192.168.43.1...
goto Check
:check
ipconfig | findstr /I "192.168.43.1"
:: Opens ipconfig and searches for 192.168.43.1 in the output of ipconfig to check if 192.168.43.1 is connected
if %errorlevel% == 0 (
goto 0
) else (
goto 1
)
:: Close your if statements!
:1
echo Connection not set
echo Setting up connection...
netsh wlan connect name="sdc-yayjg"
:: "sdc-yayjg" is the host name of 192.168.43.1
timeout 3 > NUL
echo Opening ftp 192.168.43.1
ipconfig | findstr /I "192.168.43.1"
if %errorlevel% == 0 (
goto 0
) else (
echo FAIL: Connection not set after three tries.
echo Press any key to exit.
pause > NUL
:: Close your if statements!
exit
)
:0
echo Internet Connection set - Connecting to FTP via Windows Explorer...
%windir%\explorer.exe ftp://192.168.43.1:8888/
exit