I have a large number of Linux devices that i want to be able to SSH into and change the netmask. I wanted to create a batch file to do this so that i can export a list of the IP addresses and then run a batch to change the netmask.
I expected my script to be something along the lines of this:
$user = "username" $pass = "password" dir /b cmd.exe -arp -a>List.txt for /f "Tokens=1 Delims= " %%a in (List.txt) do ( echo SSH <IP ADDRESS from List.txt> $user $pass echo sudo ifconfig eth0 netmask 255.255.255.192 echo exit )
How can i get this to work? am i going along the correct lines?
Advertisement
Answer
You’re very close. However, batch has some nuances that may seem counterintuitive to people used to *nix shell scripting.
Variables need to be set with the set
command, and there can’t be spaces on either side of the =
sign. This is because you are allowed to include spaces in variable names in batch. Seriously.
Variables are called like %var%
instead of $var
, but you don’t use the symbols when you’re setting the values.
You don’t need to use cmd.exe
to call arp
; it’s a perfectly valid batch command. Because of the way arp -a
output is formatted, you’re going to want to narrow things down. Find
(or findstr
) is about as close as you’re going to get to grep
.
set user="username" set pass="password" arp -a|find "Interface">List.txt for /f "tokens=1" %%a in (List.txt) do ( SSH %user%@%%a 'sudo ifconfig eth0 netmask 255.255.255.192' )
It should also be noted that Windows has no SSH client natively installed, so you’re going to have to find a third-party solution for that.