Reverse Engineering Team Board

Reverse Engineering Team Board (http://www.reteam.org/board/index.php)
-   General Forum (http://www.reteam.org/board/forumdisplay.php?f=21)
-   -   I want to create a TCP/IP Batch file (http://www.reteam.org/board/showthread.php?t=4616)

aladdin 02-14-2012 09:11 AM

I want to create a TCP/IP Batch file
 
Hello everybody !!!

I need to create a batch file where I can change my TCP/IP settings, i.e., IP addresses.

When I run that batch file it should ask in the following order one by one:

1. IP Address
2. Subnet Mask
3. Default gateway
4. Preferred DNS
5. Alternate DNS

My OS is WinXP SP3

I read the following thread but it couldn't solve my problem.

http://forums.techarena.in/software-...nt/1130548.htm

I want to ask that when I copied the commands in notepad from the above link and ran the batch file then it asks LAN name. But I tried almost all names like Local Area Connection or LAN but it said "Local Area Connection, Lan" is wrongly specified or something like this. I want to know the name of LAN command.

So I want a batch file and also the name of LAN which I should enter during running of batch file.

Please someone help me.

my email: aladdin2006@ymail.com

Thanks.

Git 02-14-2012 12:21 PM

Open a Cmd window and type

NET HELP

Git

zementmischer 02-14-2012 12:43 PM

You can use the network command shell 'netsh.exe' for such tasks:
Code:

@ECHO OFF
ECHO Enter IP Address
SET /P ipaddr=
ECHO Enter Subnet Mask
SET /P subnet=
ECHO Enter Default gateway
SET /P gateway=
ECHO Enter Primary DNS
SET /P pridns=
ECHO Enter Secondary DNS
SET /P secdns=
netsh interface ip set address "Local Area Connection" static %ipaddr% %subnet% %gateway% 1
netsh interface ip set dns "Local Area Connection" static %pridns%
netsh interface ip add dns "Local Area Connection" %secdns% index=2
ipconfig /all

You can get the names of your ethernet adapters with 'ipconfig' or 'ipconfig /all'.
Just look for 'Ethernet adapter' or 'Wireless LAN adapter', eg.
Code:

ipconfig/all

Windows IP Configuration

  Host Name . . . . . . . . . . . . : YourHostName
  ....

Wireless LAN adapter Wireless Network Connection:

  Connection-specific DNS Suffix  . : Home
  ....

Ethernet adapter Local Area Connection 1:

  Media State . . . . . . . . . . . : Media disconnected
  ....

Ethernet adapter Bluetooth Network Connection:

  Media State . . . . . . . . . . . : Media disconnected
  ....

Ethernet adapter Local Area Connection 2:

  Connection-specific DNS Suffix  . :
  ....

This example output contains 4 network interfaces: 'Wireless Network Connection', 'Local Area Connection 1', 'Bluetooth Network Connection' and 'Local Area Connection 2'

aladdin 02-16-2012 02:26 AM

OK! I am using the following commands atonce:


Code:

@ECHO OFF
 
set varip=10.0.83.67
set varsm=255.255.255.224
set vargw=10.0.83.66
set vardns1=10.0.1.21
set vardns2=10.0.83.66
 
REM ***** You don’t need to change anything below this line! ******
 
ECHO This fanciness is brought to you by Saman Sadeghi!

ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
 
ECHO Setting Gateway
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric = 1
 
ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
 
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
 
ECHO Here are the new settings for %computername%:
netsh int ip show config
 
pause

And it works fine.................100% WORKING.......

zementmischer 02-16-2012 05:03 PM

Sorry aladdin, but your statement somewhat implies that my batch file isn't working at all.

Did you even bothered to try and see if it works?

Furthermore your 'googled' batch file doesn't even meet your posted requirements ("it should ask in the following order") - hence, it's definitely NOT 100% WORKING. Or did I missed a RFC (Request for Change) somewhere?

Nevertheless you can improve the perfomance of the batch file by combining the first and second call to netsh.exe.

Here's the code - just in case that you don't have the faintest idea of CLI (did you ever made a batch file yourself?)
Code:

@ECHO OFF
SET adapter="Local Area Connection"
SET ipaddr=10.0.83.67
SET subnet=255.255.255.224
SET gateway=10.0.83.66
SET pridns=10.0.1.21
SET secdns=10.0.83.66
netsh interface ip set address %adapter% static %ipaddr% %subnet% %gateway% 1 >NUL
netsh interface ip set dns %adapter% static %pridns% >NUL
netsh interface ip add dns %adapter% %secdns% index=2 >NUL
netsh interface ip show config %adapter%
PAUSE


Git 02-16-2012 06:05 PM

This is why it is better to lead and guide people towards learning how to solve their problems and also promote searching. Just giving potted answers will teach nobody anything, except maybe teach the giver how ungrateful some people can be.

Remember people, if you are answering a question try to guide the asker rather than giving solutions, and if you are seeking answers remember to search and to make an attempt to learn. Come back here with specific questions when you get stuck but you can exhibit some attempt at having tried.

Git

aladdin 02-20-2012 05:24 AM

[Please DO NOT reply to yourself, use the Edit button to edit your post]

I'm really sorry for that zementmischer.

Yeah I was asking for a batch file which should ask in that particular order but when I googled it then I found my mentioned codes that 100% work.

Technically I was asking for an automatic IP changer in batch form which can either ask IPs/Gateway or these can be defined in a batch file at a particular place. Therefore, I got the latter.

Thanks.

Please consider the following codes:

Code:

@ECHO OFF
 
set varip=10.0.83.88
set varsm=255.255.255.224
set vargw=10.0.83.65
set vardns1=10.0.1.21
set vardns2=10.0.83.66
 
REM ***** You donít need to change anything below this line! ******
 
ECHO This fanciness is brought to you by Saman Sadeghi!

ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
 
ECHO Setting Gateway
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric =

ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
 
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
 
ECHO Here are the new settings for %computername%:
netsh int ip show config
 
pause

Now I want to ask one more thing after running the above codes in batch file.

I go to TCP/IP Properties > Advanced and I found 10.0.83.65 already entered in Default Gateway field.

Now the problem is: I want to add one more gateway in Default Gateway field which is 10.0.83.66 with Metric 50

What additional lines should I add in my batch file in order to do that?

My OS is WIN XP SP3

Thanks in advance.

zementmischer 02-20-2012 08:37 AM

aladdin, my previous post may sound a bit offensive, but my sole intention was to show you what will happen if you completely ignore replies from other forum members. But rest assured, I'm not upset in any way because of your post ;)

Git, I fully agree that showing someone how to fish is better than just giving them the fish. But guiding someone is a tedious and time-consuming task - especially if the solution is right at your finger tips (I already used such scripts in the past to switch between different intranets).

But back to the topic:
You can also add additional gateways with netsh.
Just add a new variable at the top of your script, eg.
Code:

set vargw=10.0.83.65
set vargw2=10.0.83.66
set vardns1=10.0.1.21

and insert the following lines:
Code:

ECHO Setting Gateway
netsh int ip set address name="Local Area Connection" gateway=%vargw% gwmetric=1

ECHO Setting Gateway 2
netsh int ip add address name="Local Area Connection" gateway=%vargw2% gwmetric=50


ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%


aladdin 02-21-2012 05:15 AM

I combined your lines in my batch file which is given below:

Code:

@ECHO OFF
 
set varip=10.0.83.88
set varsm=255.255.255.224
set vargw=10.0.83.65
set vargw2=10.0.83.66
set vardns1=10.0.1.21
set vardns2=10.0.83.66
 
REM ***** You don’t need to change anything below this line! ******
 
ECHO This fanciness is brought to you by Saman Sadeghi!

ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
 
ECHO Setting Gateway
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric =

ECHO Setting Alternate Gateway
netsh int ip add address name="Local Area Connection" gateway = %vargw2% gwmetric=50


ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%
 
ECHO Setting Secondary DNS
netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
 
ECHO Here are the new settings for %computername%:
netsh int ip show config
 
pause

Things are getting more complicated :D
Few things happened after running the final batch fille:

1. An error occurred at the end after running the batch file:
Network Command Shell has encountered a problem and needs to close. We are sorry for the inconvenience. Send Error report/Don't Send

2. It assigns Default Gateway field in the following order:
10.0.83.66 Metric = Automatic
10.0.83.65 Metric = 50

I go to Local Area Connection Status > Support and I found Default Gateway = 10.0.83.66 whereas when I go to TCP/IP Properties then I find the following information:
IP Address: 10.0.83.88
Subnet Mask: 255.255.255.224
Default Gateway: 10.0.83.65

Strange??? And sometimes it says which is as follows:

Code:

Setting IP Address and Subnet Mask
Ok.

Setting Gateway
Ok.

Setting Alternate Gateway

A default gateway with this IP Address already configured on this interface.


Setting Primary DNS
Ok.

Setting Secondary DNS
Ok.

Now I manually add 10.0.83.66 with Metric 50 & 10.0.83.65 with Automatic Metric then I export the settings using the following command:
Code:

netsh -c interface dump > c:\location1.txt
And when I open location1.txt then I find the following information:
Code:

#========================
# Interface configuration
#========================
pushd interface

reset all


popd
# End of interface configuration

#========================
# Interface configuration
#========================
pushd interface ipv6

uninstall


popd
# End of interface configuration



# ----------------------------------
# ISATAP Configuration
# ----------------------------------
pushd interface ipv6 isatap



popd
# End of ISATAP configuration



# ----------------------------------
# 6to4 Configuration
# ----------------------------------
pushd interface ipv6 6to4

reset



popd
# End of 6to4 configuration

#========================
# Port Proxy configuration
#========================
pushd interface portproxy

reset


popd
# End of Port Proxy configuration



# ----------------------------------
# Interface IP Configuration       
# ----------------------------------
pushd interface ip


# Interface IP Configuration for "Local Area Connection"

set address name="Local Area Connection" source=static addr=10.0.83.88 mask=255.255.255.224
set address name="Local Area Connection" gateway=10.0.83.65 gwmetric=0
add address name="Local Area Connection" gateway=10.0.83.66 gwmetric=50
set dns name="Local Area Connection" source=static addr=10.0.1.21 register=PRIMARY
add dns name="Local Area Connection" addr=10.0.83.66 index=2
set wins name="Local Area Connection" source=static addr=none

popd
# End of interface IP configuration


zementmischer 02-21-2012 06:34 AM

I could reproduce the crash, but it's caused by YOUR empty gwmetric.
You must always assign a value to gwmetric. The only exception is if you set gateway to none (in this case you must leave gwmetric empty).
I've tested your script with gwmetric=1 and everything seems to work fine!

Quote:

A default gateway with this IP Address already configured on this interface.
This isn't an error. You'll get this message if you run the script multiple times.


All times are GMT -4. The time now is 01:22 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.