cmdを使用してワイヤレスネットワークに接続できない

cmdを使用してワイヤレスネットワークに接続できない

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 が 1 ミリ秒ほど開いてから終了します。つまり、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

関連情報