Link to home
Start Free TrialLog in
Avatar of softbless
softbless

asked on

Batch file, net use

Dear All,

I need batch file(s) that :
# in the beginning wait for 10 seconds
# connecting to a mapped drive : \\192.168.2.5\data to z drive by : net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
# and after 60 seconds if the "net use" command is not working it will terminate the net use.

We plan to make 2 files : connect.cmd and checkconnection.bat

connect.cmd will consist :
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd

checkconnection will consist :
sleep 10
start connection.cmd
timeout 60
IF NOT EXIST Z:\ GOTO FAILURE
GOTO END
:FAILURE
net stop "workstation"
net start "workstation"
PAUSE
:END

the problem is :
# the batch file in the beginning doesn't wait for 10 seconds. Do i use the wrong command?
# net stop workstation requires me to enter "y" manually, could i bypass this? so the batch file will run "net stop workstation" without the user enter the key "y"
# we need to terminate the net use. Cause it the server (192.168.2.5) hangs, the computer that connect to it also hangs. will the net stop workstation will do that? to terminate the net use?

Please help. Thank you.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Sleep is not a built in command.  The easiest way without an extra EXE is to use a PING.  PING to an address that responds multiple times takes around 1 second each so:

ping -n 11 127.0.0.1 >NUL

will wait about 10 secs in place of your sleep command.

The NET STOP by itself won't ask for a Y/N unless it is having to stop another service too, this is probably "computer browser"?

if so do:

NET STOP "Computer Browser"
NET STOP "Workstation"
NET START "Workstation"
NET START "Computer Broswer"

The NET USE will just stop  when it does and I don't know how long it will take, running a test now to an address that doesn't respond here it seems it takes about 25 secs on a W2K box.

So perhaps try it with a PING -n 61 127.0.0.1 >NUL for your "timeout 60" for starters.

Other than that we can use TASKLIST or TASKILL to look for the other process being open potentially and kill it, though it will timeout in the end anyway of course.

Steve
Avatar of softbless
softbless

ASKER

hi steve,

it's working, the "PING -n 61 127.0.0.1 >NUL" give me +/- 10 seconds waiting.

and it's working to for the net stop workstation.

Could you solve the 3rd question about the net use?

The problem is if the server is off, then the net use will terminate by itself. But the problem that we have is : the server hangs, and the net use cause hangs in the client computer.

Please help. Thank you.
Change connection.cmd by adding
exit
to the end.
then we can use

tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
which will return %errorlevel% 0 when net.exe is running and 1 if not

Then we can do a
taskkill /IM net.exe /F

Frankly you could just do a taskkill anyway I suppose so either:

ping -n 61 127.0.0.1 >Nul
tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
if %errorlevel%==0 taskkill /IM net.exe /F

or just

taskkill /IM net.exe /F 2>NUL

Steve


The other way, to make sure it is the RIGHT net.exe you are closing in case somehow there is another, change the START command to include a title, e.g.

START "killme" connection.cmd

then use

TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

hth

Steve
Another way to use ping as a timer, and get a more accurate time - is to ping a non-existent address, but set a time limit for the ping attempts in milliseconds.  So for 10 seconds you would use something like:

PING 1.1.1.1 -n 1 10000 >NUL
Very true and I use that sometimes too, though PING by default waits 1 second so ping -n 31 for insatnce to localhost will bring:

C:\Documents and Settings\stephen>echo %time% & ping 127.0.0.1 -n 31
 8:40:48.85

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
<snip>
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 31, Received = 31, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms

C:\Documents and Settings\stephen>echo %time%
 8:41:18.92


Which is near enough 30 secs for me :-)

It does one ping then waits a sec, then the other 30 with a second inbetween which is why you do 31.

Steve
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Interesting Qlemo....  though I couldn't get it to find the other process, surely there isn't a window with the other title without the other box?
Steve
Oh hang on... you used netuse%random% again so it gets another random number.  Try

set id=netuse%random%
start /b "%id%" connection.cmd
ping -n 11 127.0.0.1 >nul
taskkill /FI "WINDOWTITLE EQ %id%*" /F
but still :

C:\>p
C:\>set id=netuse14163
C:\>start /b "netuse14163" connection.cmd
C:\>ping -n 11 127.0.0.1  1>nul
C:\>net use r: \\128.127.1.23\c$
C:\>taskkill /FI "WINDOWTITLE EQ netuse14163*" /F
INFO: No tasks running with the specified criteria.
C:\>System error 53 has occurred.
The network path was not found.
C:\>exit

but works fine without the /b

Steve
Steve,
You are right, that can't work, there is no Windows title when using start /b. Stupid me. And that double %random% use, tsss. Shouldn't post before I had several litres of coffee.

Know the feeling :-)
Hi Steve and Qlemo,

I'm a bit confused with your explanations. My english is not too well.

Could I conclude that, the connection.cmd will be like this :
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
end

and the checkconnection.bat will be like this :
ping -n 11 127.0.0.1 >Nul
start "killme" connection.cmd
ping -n 61 127.0.0.1 >Nul
tasklist /fi "IMAGENAME EQ net.exe"  find "net.exe"
if %errorlevel%==0 TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

Is it correct?

And btw, Qlemo suggestion would be great, could we hide to eliminate the pop up window? How? Is it using :

set id=netuse%random%
start /b "%id%" connection.cmd

Kindly advise. THank you.
Regarding hiding:
If we use start /b (which would really hide), you do not have a window title to search for, and hence nothing to kill that easy.
As a replacement I recommend to start the net use minimized (see below).

The code is not correct, it is mixing some statements up. That's the correct one (and I will not use a second batch file, instead I issue the net use directly). Further checking for the running task is not needed, we will just force kill and ignore any error message telling us it is not there any more:

@echo off
ping -n 11 127.0.0.1 >Nul
start /b "killme" net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd& exit
ping -n 61 127.0.0.1 >Nul
taskkill /FI "IMAGENAME net.exe" /F >nul 2>nul

Open in new window

Nearly.... exit not end in the first batch and you have a | missing which pipes the output of the tasklist into find.  hth

Steve

connection.cmd:
net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
exit

checkconnection.bat:
@echo off
REM Wait 10 seconds
ping -n 11 127.0.0.1 >Nul
REM Try to map drive in different process
start "killme" connection.cmd
REM Wait 60 seconds
ping -n 61 127.0.0.1 >Nul
REM If The other process is still running then errorlevel is set to 0 and need to kill it
tasklist /fi "IMAGENAME EQ net.exe" | find "net.exe"
if %errorlevel%==0 TASKKILL /FI "WINDOWTITLE EQ killme - connection.cmd"

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Steve, my code is correct, but I forgot to show how to use the /min switch as alternative. That would have looked almost like your last post, but without the &exit:

@echo off
ping -n 11 127.0.0.1 >Nul
start /MIN "killme" net use Z: "\\192.168.2.5\data" /user:michael michaelpasswd
ping -n 61 127.0.0.1 >Nul
TASKKILL /FI "WINDOWTITLE EQ killme*"

Open in new window

True.... think I prefer your running the net use directly from START command anyway, not sure why I didn't do it myself!