Solved

multiple net commands

Posted on 2013-12-09
16
222 Views
Last Modified: 2013-12-17
Is there any easy way to do a bulk list of net commands in one single go? Without having to execute the commands one-by-one?
0
Comment
Question by:pma111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 39705618
Use a text file containing all commands as you would enter them in the netsh prompt, and provide that file: netsh -f c:\Scripts\NetShCmds.txt

Or create the commands on the fly:
@echo off
( echo interface show interface
  echo ras ip show config
) | netsh

Open in new window

This is handy if you need to build the commands depending on conditions, have to insert e.g. interface names you got earlier from another netsh, and the like.
0
 
LVL 1

Expert Comment

by:BlogITman
ID: 39710413
The answer above will work, if you are not very familiar with programming there are other ways to do similar net commands. For example three of the most use net commands are:
Net use
Net share
Net view

It is a straightforward process to add any of the net commands in a *.cmd or *.bat file.
For example:

Copy and paste the following into Notepad then run it in  command window aka dos prompt

:: ***************************************************
:: Filename: Netcmds.cmd
:: Purpose: to execute Net commands one at a time
:: (to loop use a call or other method)
:: ***************************************************
@echo off
@echo Following is list of shared drives for this PC
net use
::
@echo Following is list of netbios named sharable PC's
net view
pause
Exit
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39710733
Oh, I misread the question to be about NETSH, which is different (has a prompt mode).
NET doesn't have such a mode, and there is no other way than to note each net cmd one after another. Maybe you should tell more about what you are targeting at, so we might be able to taylor something.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 3

Author Comment

by:pma111
ID: 39710914
by: BlogITman

How do I split the commands, is it just one command per line?

How do you execute the actual *cmd file within the command prompt, once I have done it?

Essentially I have about 200

net view \\servername /all >servername.txt

commands to run.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39710946
There are better ways to ask for the available shares on servers, like with WMI queries.
What you can do if you want to stay with net view is this:
@echo off
for %%S in (server1 server2 server3) do net view \\%%S /all > %%S.txt

Open in new window

or
@echo off
for /F "tokens=*" %%S in (servers.txt) do net view \\%%S /all > %%S.txt

Open in new window

(Put one of both into a .cmd file, and to execute, just type the name of that .cmd file).
The first one requires you to enumerate all servers in the batch file.
The second one reads a file "Servers.txt" with one server per line.
You might have to add paths to file names, or, better, put the .cmd file into the same folder as the input and output files.
0
 
LVL 3

Author Comment

by:pma111
ID: 39711045
I will opt for option number 2, however, in what format do you supply the "server" parameter? i.e. is it just \\server or IP, or domain\server
0
 
LVL 3

Author Comment

by:pma111
ID: 39711059
And where exactly do you need to save "servers.txt", and how does it need to be delimited, i.e. line space, comma etc?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39711178
The servers.txt file just contains the server name, each one at an own line:
server1
server2

Open in new window

To make sure the paths used for all files are correct, add them:
@echo off
for /F "tokens=*" %%S in (c:\Scripts\servers.txt) do net view \\%%S /all > c:\Scripts\Results\%%S.txt

Open in new window

That way it doesn't matter where the cmd file is, and how you start it. The files will always be searched for and stored in those folders you noted in the cmd file.
0
 
LVL 3

Author Comment

by:pma111
ID: 39711186
Ok thanks, so your code above it going to create one txt file per server?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39711203
Yes. It would be very difficult with cmd.exe batches to get the output into something more useful, like a table containing server name, share name, and description. But if you like that more, we could collect ALL output into a single file:
@echo off
copy nul c:\Scripts\NetView.txt
for /F "tokens=*" %%S in (c:\Scripts\servers.txt) do net view \\%%S /all >> c:\Scripts\NetView.txt

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 39711393
As i am new to cmd/batch files, could you give a bit of a laymans guide of what

@echo off --  is actually doing?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39711508
@ prevents the following command from being echoed.
echo off switches off the default of echoing all commands as they are executed.

If you omit that line, all commands can be seen on "stdout", which is by default the console, but a file if using the ">" operator. Usually you do not want to see the commands, only specific output, and so @echo off is the first command in a cmd file you will see on most occasions.
0
 
LVL 3

Author Comment

by:pma111
ID: 39723665
A bit lost. I have saved the code

"@echo off
copy nul c:\Scripts\NetView.txt
for /F "tokens=*" %%S in (c:\Scripts\servers.txt) do net view \\%%S /all>> c:\Scripts\NetView.txt "


as 123.cmd

What when I open the command prompt do I enter to run this file/script?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39723736
Just enter
   c:\Scripts\123
if you have saved the batch file in the Scripts folder.
0
 
LVL 3

Author Comment

by:pma111
ID: 39723812
And save it as .bat or .cmd, or doesnt it matter?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39723817
Doesn't matter. .bat is the "old style" name, .cmd says that you use advanced features of NT cmd.exe. But nothing changes if you change the extension from one to the other.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question