IP based 'hosts' file - windows

I'm new to this, so bear with me a bit.
I'd like to write a batch file that can replace the etc/hosts file, depending on which VLAN I' m in when I log in.
ie: When I log in and I've got the xxx.xxx.102.xxx local IP, I'd like to use the hosts1 file. When I log in and I've got the xxx.xxx.103.xxx local IP, I'd like to use the hosts2 file.
So basically based on the output of an ipconfig, scan it for a "102" and a "103" and when that number is in the output, it could replace the hosts file in the Windows directory.
Ideas?

for %%a in (102 102 103) do (
ipconfig | find "xxx.xxx.%%a.xxx" >nul 2>&1 && copy /y hosts%%a "%SystemRoot%\system32\drivers\etc\hosts"
)
will work if you have admin permissions and your hosts files are anemd hosts101 hosts102 ...

you might try this. I use findstr's Regex capabilities here:
ipconfig | findstr /er "[0-9][0-9]*.[0-9][0-9]*.102.[0-9][0-9]*" && copy hosts1 "%windir%\system32\drivers\etc\hosts"
ipconfig | findstr /er "[0-9][0-9]*.[0-9][0-9]*.103.[0-9][0-9]*" && copy hosts2 "%windir%\system32\drivers\etc\hosts"

Related

Batch create files containing the name of the folder in which they are located and other data

I have a network drive (mapped locally) where the backups of each user of my organisation are stored (more than 2000).
The tree is in this form :
username0001\archive
...
username0456\archive
...
username0789\archive
...
username2000\archive
I would like from a batch script, create a file called "backup.conf" in each backup folder of my users, which would contain 2 lines :
[username] >> 1st line
ArchivePath=archive >> 2nd line
With the real username between [  ].
Do you have any idea of code that could lead to this ?
I tried the following batch script :
cd mynetworkmappeddriveletter: (e.g. X:)
for /d %%a in (*) do (echo %%a > %%a\backup.conf)
How to add the second line and [ ] between the username ?
Thank you for your help.
No enough rep to comment.
Here's some ideas for your code from a junior :)
list directory name (dir)
admin
tech
user1
user2
user3
voucher
add a filter to only get users' folder name
user1
user2
user3
store the output in a text file: f.txt data is
user1
user2
user3
I guess you already know where users' folder are, so path should be known. So store it in a variable
"c:\mypath\"
for each line in the text file, (create a file that contains what you want at the specific location
For user1,
echo "[user1]" > backup.conf
echo "ArchivePath=c:\mypath\user1\archive" > backup.conf
For user2,
...
For user3,
...
It might contains batch syntax error as I'm not an expert but that the basic approach I would start with.
I tried the following batch script :
cd mynetworkmappeddriveletter: (e.g. X:)
for /d %%a in (*) do (echo %%a > %%a\backup.conf)
How to add the second line ArchivePath=archive and add [ ] between the username ?

How to use the fast ping with alot of ip addressess in cmd to send alot of packets for every hop?

What have I tried
I wanted to create a way to make a faster pinging method for cmd than the normal '-t' command. So I came up with an idea to make two files and link them together:
The first st1.bat file contains
ping 10.10.10.10 -n 1
st2.bat
And the second st2.bat file contains
ping 10.10.10.10 -n 1
st1.bat
This creates a loop so it goes on forever.
The '-n' command is the number of packets you send. So by sending just one packet and then opening the second .bat file makes it a lot faster (30 milliseconds).
The command for extracting just the IP addresses is:
(#for /f "tokens=8" %a in ('tracert -4 -d wikipedia.org^|find "ms"') do #echo start ping %a)>ips.txt
What do I need
I want to combine these two codes and make 2 files for each IP to create a loop just like I showed you above. Every IP loop should have its own cmd window because if everything is in one window every packet should wait for the previous one to be done:
And finally link all the files together in one .bat file that I think will look like that:
Start st1.bat
Start st3.bat
Start st5.bat
Try to use: ping -n 1 -l 1
if you doesn't want
Firing with 1 bytes of data: try this command:
#echo off
:1
ping -n 1 -l 1 | find "TLL="
goto 1

VeraCrypt Windows commandline - encrypt whole USB

I'm trying to format whole USB from windows commandline.
In Linux it's quite easy - veracrypt --create diskPath
But when i'm trying to do that in windows it's impossible.
I'm trying to use this command
"VeraCrypt Format.exe" /create \\?\Volume{ad4200bf-2236-11e8-9b79-bcee7b594766}\ /password test /size 100M
Also this:
"VeraCrypt Format.exe" /create F: /password test /size 100M
Also - i want to encrypt whole partition, not only 100M, but i can't execute /craete without /size
VeraCrypt format command goes like this:
"VeraCrypt Format.exe" [/n] [/create] [/size number[{K|M|G|T}]] [/p
password] [/encryption {AES | Serpent | Twofish | AES(Twofish) |
AES(Twofish(Serpent)) | Serpent(AES) | Serpent(Twofish(AES)) |
Twofish(Serpent)}] [/hash
{sha256|sha-256|sha512|sha-512|whirlpool|ripemd160|ripemd-160}]
[/filesystem {None|FAT|NTFS}] [/dynamic] [/force] [/silent]
Now what you were trying should go like the below. You are missing the path or the subset of the parameters.
Create a 100 MB file container using the password test and formatted using FAT:
"C:\Program Files\VeraCrypt\VeraCrypt Format.exe" /create c:\pathofthevolume\testvolume.hc /password test /hash sha512 /encryption serpent /filesystem FAT /size 100M /force
And if you want to mount that automatically, then it should go like this:
Mount a volume called testvolume.tc using the password test, as the drive letter X.
veracrypt /v testvolume.tc /l x /a /p test /e /b
Refer the source LINK for details.
Hope it helps.

Entries in host file at particular line

I am looking for a script that can add host entries in a windows 7 host files at particular lines or just after the default text in host file.
I came across this thread in your forum here that can add/remove the lines perfectly but it adds the lines at the end of the file not at the top. can you please help with the same?
hey thanks for the link.
Since i have limited exposure to batch files, I wanted to check that method offline first
1) created a txt file dns.txt with some DNS entries
1.2.3.4 abcd.com
2.3.4.5 bcde.com
3.4.5.6 cdef.com
4.5.6.7 defg.com
2) created a batch file with one line code in it:
for /F %%i in (c:\temp\dns.txt) do echo %%i >> "D:\hosts"
3) but when i check the host file, it only has IP entries and not the hostnames against it.
==================
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
1.2.3.4
2.3.4.5
3.4.5.6
4.5.6.7
====================
what is wrong !!?

output only certain results in a log file from batch file

relatively new to batch files so please help me out;). What I'm trying to do is create a customized log file. Simple example:
ipconfig >> ip.log
this will display all the TCP/IP related info.
Let's say I only need the user to see the IP and Default Gateway, so the result in the output ip.log should look like this:
IPv4 Address: xxx.xxx.xxx.xxx
Default Gateway: xxx.xxx.xxx.xxx
How do I do this?
Generally you would use a filter for that purpose. You want to filter on certain strings, so findstr command is natural choice:
ipconfig | findstr /c:"IPv4 Address" /c:"Default Gateway" >>ip.log

Resources