I'm looking to get computers names from my network, so i decided to use this following script :
for /L %%N in (1,1,10) do nslookup 132.147.160.%%N
PAUSE
With this command everything is displaying correctly on the command prompt.
But with this last one not so well :
for /L %%N in (1,1,256) do nslookup 132.147.160.%%N >nslookup.txt
PAUSE
First of all, the command prompt is displaying wrong things (there's a non-desired "1" added and i don't know why):
C:\Users\Toshiba\Desktop>nslookup 132.147.160.1 1>nslookup.txt
C:\Users\Toshiba\Desktop>nslookup 132.147.160.2 1>nslookup.txt
*** serveur1.mycompany.fr ne parvient pas à trouver 132.147.160.2 : Non-exi
stent domain
C:\Users\Toshiba\Desktop>nslookup 132.147.160.3 1>nslookup.txt
*** serveur1.mycompany.fr ne parvient pas à trouver 132.147.160.3 : Non-exi
stent domain
C:\Users\Toshiba\Desktop>nslookup 132.147.160.4 1>nslookup.txt
*** serveur1.mycompany.fr ne parvient pas à trouver 132.147.160.4 : Non-exi
stent domain
[ ... etc]
And also in nslookup.txt
i've got NO MORE THAN this output :
Serveur : serveur1.mycompany.fr
Address: 132.147.160.1
Nom : 132.147.160.256
Address: 60.200.60.100
Please, what am i doing wrong ?
Thank you
try this:
#ECHO OFF &SETLOCAL
for /L %%N in (1,1,10) do nslookup 132.147.160.%%N >>nslookup.txt 2>&1
TYPE nslookup.txt
To remove the error messages from nslookup.txt, simply delete 2>&1.
Related
I was wondering how to make script which takes Prefix and txt file in which I have full name(/fullname), comment(/comment) and work days(/times)
For example:
I call this script like this: F:\Destination>UserScript.cmd Profile Log.txt Where Profile - prefix for all users and Log.txt with additional parameters like these below!
"Bob Truman", "Test Comment", "M"
"Sarah Hanks", "Test Comment", "M-S"
Should be created
Profile1 /fullname:"Bob Truman" /comment="Test comment" /times="M"
Profile2 /fullname:"Sarah Hanks" /comment:"Test comment" /times:"M-S"
Here what I have tried so far: (%1 Profile, %2 Log.txt)
For /F "delims= " %%i in (%2) do (
net user "%1%%i" "%1%%i" /logonpasswordchg:yes /add /fullname:"%%b" /comment:"%%c" /times:"%%d"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Good afternoon BASH.
How can I find the line between the word Address and the word Parameters in the command output:
Address 2001:0:284a:364:3869:30e6:4fc4:c8f8 Parameters
---------------------------------------------------------
Interface Luid : Teredo Tunneling Pseudo-Interface
Scope Id : 0.0
Actual lifetime : infinite
Preferred lifetime : infinite
DAD state : Preferred
Address Type : Public
Skip as source : false
Address fe80::3869:30e6:4fc4:c8f8%11 Parameters
---------------------------------------------------------
Interface Luid : Teredo Tunneling Pseudo-Interface
Scope Id : 0.11
Actual lifetime : infinite
Preferred lifetime : infinite
DAD state : Preferred
Address Type : Other
Skip as source : false
$ sed -n 's/Address \(.*\) Parameters/\1/p' file
2001:0:284a:364:3869:30e6:4fc4:c8f8
fe80::3869:30e6:4fc4:c8f8%11
You can do it in two steps :
1- Find the lines where we have the word "Address" and the word "Parameters" : How to find lines containing a string in linux
grep 'Address.*Parameters'
>>> Address 2001:0:284a:364:3869:30e6:4fc4:c8f8 Parameters
>>> Address fe80::3869:30e6:4fc4:c8f8%11 Parameters
2- Extract the string between the two words : How to use sed/grep to extract text between two words?
sed -e 's/Address\(.*\)Parameters/\1/'
>>> 2001:0:284a:364:3869:30e6:4fc4:c8f8
>>> fe80::3869:30e6:4fc4:c8f8%11
3 - Voila !!!
This answer, unlike those currently submitted, assumes, (based upon your other two tags, windows and cmd), that your bash tag, is supposed to be batch-file.
It also makes a best guess at the initial command used to wite the output you've submitted, so that it can do it all in one go.
#Echo Off
SetLocal EnableExtensions
(Set LF=^
% 0x0A %
)
For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "CR=%%G"
For /F "Delims=" %%G In ('%SystemRoot%\System32\cmd.exe /V/D/C
"%SystemRoot%\System32\netsh.exe interface ipv6 show addresses interface="Teredo Tunneling Pseudo-Interface" level=verbose | %SystemRoot%\System32\findstr.exe /RC:".*!CR!!LF!----" 2>NUL"
') Do For %%H In (%%G) Do Set /P "=%%H" 0<NUL | %SystemRoot%\System32\find.exe ":"
Pause
Expected output:
2001:0:284a:364:3869:30e6:4fc4:c8f8
fe80::3869:30e6:4fc4:c8f8%11
Press any key to continue . . .
I am making an experimental program in batch for a simple chatting interface. In this one, I made a function where if there is the word r placed in chat, it ignores it and just redisplays the text file again. It works fine if I put r and it just refreshes, and if I put one word it works fine, but if I put a word and a space and another word, it breaks and shows the following error:
Chat(Put r for refresh):hey hi
hi was unexpected at this time.
Does anyone know how to fix this? Thanks.
Code:
#echo off
cls
cd %USERPROFILE%\Desktop\Chat
for /f "delims=" %%A in (chat.txt) do (
set %%A
)
echo %chatt%
echo %chatp%
echo %chatn%
cd %USERPROFILE%\Desktop\Chat\Servers\%chatt%
:1
cls
type %chatn%.chat
set /p in=Chat(Put r for refresh):
if %in% == r goto 1
echo %chatp%: %in%>>%chatn%.chat
goto 1
The usual way to deal with spaces in a string variables contents is to wrap it in quotes. This is the case here. When you use the variables contents with %in% the contents are inserted verbatim, so the suspect line would look like this:
if hey hi == r goto 1
It starts off okay if hey but then instead of seeing a comparison operator like == it sees hi and chokes. So wrap it all in quotes:
if "%in%" == "r" goto 1
That way it will be interpreted like
if "hey hi" == "r" goto 1
and the bat engine will know that "hey hi" should be treated as one entity.
I work on a company which has many Shop. Each shop has his own IP address :
SHOP 1 = 192.168.1.0/24, SHOP 2 = 192.168.2.0/24, SHOP 3 = 192.168.3.0/24, etc...
I have a number of Windows computers (between 1 and 9) on the same subnet (255.255.255.0) on each shop.
Each computer has a static IP address : 1st = 192.168.1.10, 2nd = 192.168.1.20, 3rd = 192.168.1.30, 4th = 192.168.1.40, etc.
The HQ of my company can deploy softs ONLY on the first computer on each shop (licences issue).
Here, we will try to install Xerox drivers on ALL windows computer on all shop.
I made 3 batch script :
The first, launched as the superadmin of the computer, install a new user with admin rights to install drivers
The second, launched as the new user, install Xerox drivers and set as default the printer.
The third, launched as the superadmin of the computer, try to install Xerox drivers on other computers of the same shop :
Loop over my 9 ip address (for /l %%G in (...) do (...)
For each IP address, test if it ping.
Do a ftp transfer between the computer 1 and the IP address (drivers packages + scripts)
PsExec64.exe to run the first script
PsExec64.exe to run the second script
The first and the second script works perfectly, we'll not speak about them.
Where I need help is on the thrid.
I try 3 scripts :
The first :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if %errorlevel%==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_errorlevel.txt with 2 computers on my subnet :
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I tried to surround %errorlevel% and 0 by simple quote but I get the same result.
Here an extract of the output if I run the batch in cmd window :
C:\Temp\Xerox_Install>(
ping -n 1 192.168.1.90 | findstr "TTL="
if '0'=='0' (echo "Ping 192.168.1.90 : OK" 1>>log_ping_errorlevel.txt ) el
se (echo "Ping 192.168.1.90 : KO" 1>>log_ping_errorlevel.txt )
)
It seems that the errorlevel is always = 0 in my script, IDK why.
The second :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
)
Here the log_ping_set.txt with 2 computers on my subnet :
Ping 192.168.1.20 : OK
Ping 192.168.1.30 : OK
Ping 192.168.1.40 : OK
Ping 192.168.1.50 : OK
Ping 192.168.1.60 : OK
Ping 192.168.1.70 : OK
Ping 192.168.1.80 : OK
Ping 192.168.1.90 : OK`
I think the ping command result is always 0 then the setter pingtest can be executed.
The third :
Because I'm stuck with the ping command, I tried the if exist folder on remote computer :
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
if exist \\%subnet%%%G\C$\Temp\ (
echo Remote folder reachable >> log_folder_reachable.txt
) else (
echo ERROR 404 - Remote folder not found >> log_folder_reachable.txt
)
)
Here the log_folder_reachable.txt with 2 computers on my subnet :
Remote folder reachable
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
ERROR 404 - Remote folder not found
It works BUT the if exist timeover is about 160 seconds and I don't want to wait for 160 seconds before going to the next computer...
Can someone help me with any method but not using external tools ?
You should read SO items about delayedexpansion.
if %errorlevel%==0 (
in the first script should be
if not errorlevel 1 (
[obviously, reverse the logical condition and remove the "not"]
if errorlevel 1 means "if errorlevel is 1 or greater than 1" and is interpreted on the run-time value of errorlevel.
Next:
ping -n 1 %subnet%%%G && set pingtest=ok
if '%ping_test%'=='ok' (
should be
set "ping_test="
ping -n 1 %subnet%%%G && set "ping_test=ok"
if defined ping_test (
Notes
you are setting pingtest in your ping command, then you are attempting to test ping_test
Even had the variablename been correct, the purpose for the quotes in an if statement is to combine the contents as a single string. Single-quotes don't cut the mustard, double-quotes are required.
With the code you have, ping_test once set will retain its value for the next loop, you need to clear it before theping.
The replacement code first sets the value of ping_test to nothing. The quotes delimit the variable name and value so that trailing spaces on the line are not included in the value assigned. The ping command then may set the value of ping_test to (ok, but any value other than nothing (ie something) is valid). The if defined command interprets the run-time value of the variable.
The third part - well, getting desperate, but no need to go this far as the changes I've outlined should solve the problem.
In regards to your first set of code where you said you tried to use delayed expansion it should look like this. Tested and works on my network.
#echo off
setlocal enabledelayedexpansion
set subnet=192.168.1.
for /L %%G in (20, 10, 90) do (
ping -n 1 %subnet%%%G | findstr "TTL="
if !errorlevel!==0 (
echo Ping %subnet%%%G : OK >> log_ping_errorlevel.txt
) else (
echo Ping %subnet%%%G : KO >> log_ping_errorlevel.txt
)
I have one batch file MyTest.bat
MyTest.bat
call "first.bat"
call "second.bat"
call "third.bat"
Now while executing MyTest.bat I will pass comma separated parameters like
call MyTest.bat first.bat,second.bat
now inside MyTest.bat I want to check which parameters are passed and based on those using if else condition
I want to execute internal statements.
for example something like it
MyTest.bat first.bat,second.bat
now inside
I will check
get all parameters list param[] = {first.bat,second.bat}
if param[i] == "first.bat"
{
call "first.bat"
}
else if param[i] == "second.bat"
{
call "second.bat"
}
else if param[i] == "third.bat"
{
call "third.bat"
}
// this assures that what parameters I passed only those statements gets executed not other.
The above code is just a pseudo code how can write actual MyTest.bat?
Next script requires another parameter order (list of batch names down to end all other parameters):
#ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749.bat name address "first script", second, third
:loop
if "%~3" == "" goto :next
if exist "%~n3.bat" (
call "%~n3.bat" %1 %2
) else (
echo can't found file; failed call "%~n3.bat" %1 %2
)
shift /3
goto :loop
:next
For debugging purposes, prepare sample files "first script.bat" and second.bat; ensure that third.bat does not exist:
==> >"first script.bat" echo #echo %~nx0 parameters: %%*=%*
==> >second.bat echo #echo %~nx0 parameters: %%*=%*
==> 2>NUL del third.bat
Output (shows independency on used delimiters):
==> 33955749 name address "first script", second, third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address
==> 33955749 name address "first script" second; third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address
Another approach: fist parameter = list of comma-separated batch names surrounded with a pair of double quotes:
#ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749b "first script,second,third" name address
rem no spaces surrounding commas or names
rem wrong: 33955749b " first script , second, third" would fail
set "_names=%~1"
set "_names=%_names:,=","%"
rem debug echo _names="%_names%"
for %%G in ("%_names%") do (
if exist "%%~dpnG.bat" (
call "%%~dpnG.bat" %2 %3
) else (
echo can't found script; failed call "%%~dpnG.bat" %2 %3
)
)
Output (shows responsivity to used delimiters):
==> 33955749b "first script,second,third" name address
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\third.bat" name address
==> 33955749b "first script, second,third" name address
first script.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\ second.bat" name address
can't found script; failed call "D:\bat\SO\third.bat" name address
Note that both 33955749.bat and 33955749b.bat scripts
accept (partially or fully qualified) paths to called scripts, even with space(s);
on the other hand, they both ignore file extension(s) even if supplied and force .bat.
For instance, 33955749 name address first.cmd second.cmd would attempt to call first.bat and second.bat.
Resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G, %~1 etc. special page) Command Line arguments (Parameters)
(%variable:StrToFind=NewStr% etc.) Variable Edit/Replace