Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I change my IP Address with a Perl script? 1

Status
Not open for further replies.

GeneralDzur

Technical User
Jan 10, 2005
204
US
I've been Googling and searching on the web, but I can't find anything, so I came here.

Is it possible to change my IP address using a Perl script? I change between wireless networks pretty often, and some need DHCP, while some require a static IP, and I have about 5 batch files to change IP's, but I'd like to combine them into a script, or at least migrate them to Pscript.

Can I do that?

- stephan
 
That depends on how you actually change IP addresses in the batch files that you have already.
I would guess though that it's perfectly possible.
Maybe if you could post a snippet of batch file that does this switch then we might be able to show you the equivilant Perl.


Trojan.
 
Hello again,

These are a couple of the .bat's I use

Code:
:: Apartment
@echo off
netsh interface ip set address name="Wireless" static 192.168.1.50 255.255.255.0 192.168.1.1 1
netsh interface ip set dns name="Wireless" static 192.168.1.1 primary
netsh interface ip add dns name="Wireless" 4.2.2.1 index=2
netsh interface ip add dns name="Wireless" 4.2.2.2 index=3

Code:
:: Set Wireless adapter for DHCP
@echo off
netsh interface ip set address name="Wireless" source=dhcp

- stephan
 
Perl cannot change Windows IP information either natively or through a module. It could make the changes through the system command. Example:

#!c:/perl/bin
system("netsh interface ip set address name=\"Wireless\" source=dhcp);


But doing that is basically writing your batch files with a lot extra baggage added on to it. I would recommend sticking with the batch files. Perhaps you could consolidate all of the networks into a single batch file and use a menu to offer you a choice of networks. I've created a simple file for you that offers you the choice of Apartment or DHCP. The batch is simple enough you should be able to add more of your own networks to it.

Code:
@echo off
cls
:start

:: Setup the menu
echo Please choose a wireless network.
echo ---------------------------------
echo 1. Apartment
echo 2. DHCP

:: Setup menu processing
set choice=
set /p choice=Choice: 
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto apartment
if '%choice%'=='2' goto dhcp
echo  "%choice%" is not valid please try again
echo
goto start

:: Setup menu choice executions
:apartment
echo Setting network to "Apartment"
netsh interface ip set address name="Wireless" static 192.168.1.50 255.255.255.0 192.168.1.1 1
netsh interface ip set dns name="Wireless" static 192.168.1.1 primary
netsh interface ip add dns name="Wireless" 4.2.2.1 index=2
netsh interface ip add dns name="Wireless" 4.2.2.2 index=3
goto end

:dhcp
echo "Setting network to DHCP"
netsh interface ip set address name="Wireless" source=dhcp
goto end

:end
 
Raklet,

Thanks a lot - that's really helpful. I'm making a master batch file right now.

- stehan
 
Perl cannot change Windows IP information either natively or through a module. It could make the changes through the system command.

raklet, Could you not use a COM object? I know this can be done in VB, and CScript.

 
a) I have no idea what a COM object is, so I would not have thought to address that topic.

b) From what little research I did on COM objects in perl, you have to have ActiveState PDK in order to build the executable. PDK costs $$$$.

c) The complexity of such a task for so simple a program isn't justifiable in my opinion.
 
There is a Win32::IPConfig module out there, but I cannot get to the documentation in CPAN right now to see what it can actually handle. Might be worth a Google search though.

- George
 
a) I have no idea what a COM object is, so I would not have thought to address that topic.

b) From what little research I did on COM objects in perl, you have to have ActiveState PDK in order to build the executable. PDK costs $$$$.

c) The complexity of such a task for so simple a program isn't justifiable in my opinion.

a: After mucho research, a COM object is a DLL of code which can be used by asp, perl, VB, C, does not matter.

b: What you are refering to is if you want to write a COM object. These libs are aready present, and one uses the Win32::OLE class to execute methods in them.

c: Agreed, I cannot believe what you have to go through to use this stuff. And now they add .NET. You would think that the OS could figure out what type of DLL that you have and provide the linkage. Trying to create one of these in C++ is a nightmare, all kinds of different linkage code depending on what you want to do. Just plain stupid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top