output only certain results in a log file from batch file - windows

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

Related

Windows routes - add many lines to routes table at once

I have to add many lines (rules) to the routes table in Windows.
The regular way is to add it with:
route add 10.0.0.0 mask 240.0.0.0 192.168.0.1
However, if I want to add many lines at once (belive me I have a good reason to do it) - how can I do it quickly (running "route add" is slow)?
I tried running it in some threads and it still slow.
Option 1:
You can use this MS tool: netsh
Netsh is a command-line scripting utility that allows you to, either
locally or remotely, display or modify the network configuration of a
computer that is currently running.
Build a file with all the lines you want to add, like this:
[For more information on "add route" command and its parameters, see here].
# ----------------------------------
# IPv4 Configuration
# ----------------------------------
pushd interface ipv4
add route prefix=10.0.0.0/4 interface="Ethernet" nexthop=192.168.0.1 metric=1 publish=Yes
add route prefix=240.0.0.0/4 interface="Ethernet" nexthop=192.168.0.1 metric=1 publish=Yes
[and so on...]
popd
# End of IPv4 configuration
Save it in a *.dat file, and run this command:
netsh exec file.dat
Option 2:
[As Harry Johnston commented]: You can use CreateIpForwardEntry function to add route (example code in the link).
The CreateIpForwardEntry function creates a route in the local
computer's IPv4 routing table.

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 !!?

windows get IP dynamically

i want to implement the following logic. Does it possible to have such implementation using batch or power shell ? please share with me script for that.
Let say I have a configuration file with the following "config.propertis":
BOOTPRORO=statis or dhcp
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS=8.8.8.8
I want that at startup the system will check that file and configure network accordingly:
OS: Windows
if in BOOTPROTO=dhcp, when use DHCP in network configuration and ignore all another in config file, except DNS
if in BOOTPROTO=static, then use all variables from config file to configure IP as static.
So, I have such logic under the Linus, using shell. The script in configured in rc.d and execute before network service. Does it possible to implement such over the Windows ? Guys, please share the script !
We can definitely do this.
First things first, because a lot of systems have more than one network interface, you'll need to determine what the ifIndex is of the adapter that we want to change. Do that by running Get-NetIPInterface. You should see results like this:
In my example and going forward, I'll be using this index, 41. You should change this to match what you find on your own computer.
OK, now to read from the text file. Since you've provided the data in a key=value pair format, commonly called a hashtable, we can easily grab the data from there using ConvertFrom-Stringdata. This will give us a PowerShell hashtable, and we can pull the needed line out like this.
$values = get-content T:\config.properties | ConvertFrom-StringData
$values.BootProro
>statis
We can us this to set the PC in Dynamic IP mode, or to set static addresses. Now, for you to use this in your environment, you need to find the ifIndex, as I mentioned before. replace my index of 41 with your own, and then give it a shot. I've added -WhatIf to every line, so you will see what would happen when you run it. If you're happy with the changes it woudl make, remove -Whatif to make the script actually change the settings.
$values = gc T:\config.properties | ConvertFrom-StringData
if ($values.BOOTPRORO -eq "dhcp"){
Write-Output "---DHCP mode detected in 'config.properties' file"
Write-Output "---Setting Set-NetAdapter -DHCP Enabled"
Set-NetIPInterface –InterfaceIndex 41 –Dhcp Enabled -WhatIf
}
else{
Write-outPut "---static mode detected in 'config.properties' file"
Write-Output "---Removing network configuration"
Remove-NetIPAddress -InterfaceIndex 41 -whatif
Write-Output "---Setting new network configuration equal to"
$values
New-NetIPAddress -DefaultGateway $values.GATEWAY -IPAddress $values.IPADDR -PrefixLength 24 -InterfaceIndex 41 -WhatIf
Set-DnsClientServerAddress -ServerAddresses $values.DNS -InterfaceIndex 41 -WhatIf
}
The output looks like this:
in Windows we can set ip address via batch file or powershell script but when you use dhcp address your ip is Dynamic not static I Imposition you want static ip address
BAtch-file
netsh interface ip set address name=”Local Area Connection” static 192.168.10.10 255.255.255.0 192.168.10.1
netsh interface ip set dns name=”Local Area Connection” static 8.8.8.8
if you want be dhcp You should set
netsh interface ip set address name=”Local Area Connection” source=dhcp
note I Imposition you nic name is Local Area Connection
In powershell V3.0 and Later we Used
New-NetIPAddress –InterfaceAlias “Local Area Connection ” –IPv4Address “192.168.10.10” –PrefixLength 24 -DefaultGateway 192.168.10.1
Set-DnsClientServerAddress -InterfaceAlias “Local Area Connection” -ServerAddresses 8.8.8.8
and for startup you can put script .bat and .ps1 in startup windows but attention you should Set-ExecutionPolicy bypass before U run any script of powershell
for startup any script see link

IP based 'hosts' file

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"

Determining proxy server/port

I apologize if this is a simplistic question, I am not familiar with this kind of thing.
I am trying to determine my proxy server ip and port number in order to use a google calendar syncing program. I downloaded the proxy.pac file using google chrome. The last line reads:
return "PROXY proxyhost:1081";
I believe that means the port number is 1081, but for the proxy server, I was expecting something with the format "proxy.example.com" Any advice?
Thank you
Are you using the Windows operating system?
You can press Win+R, input "cmd" in the run box, you will get the "command Prompt", then input the following command and press Enter.
command 1
ipconfig /all | find /i "Dns Suffix"
it will show something like this.
Primary Dns Suffix . . . . . . . : xxx.xxx.xxx
command 2
ping proxyhost
it will show something like this.
Pinging proxyhost.xxx.xxx.xxx [yyy.yyy.yyy.yyy] with 32 bytes of data:
Reply from yyy.yyy.yyy.yyy: bytes=32 time=1ms TTL=60
Then maybe "proxyhost.xxx.xxx.xxx" is what you want.
Enter the following into windows commandline
netsh winhttp show proxy
It will return
Current WinHTTP proxy settings:
Proxy Servers(s) : your.proxy.address.com:port

Resources