venerdì 3 maggio 2013

Cambiare IP al volo con Netsh + HTA

Quando scendo nei vari impianti dove lavoro mi capita di dover collegare il mio portatile su reti diverse (senza DHCP). Cambiare ogni volta l'indirizzo ip cliccando in giro per le varie voci di windows 7 non è abbastanza geek per me, per cui ho deciso di trovare un sistema più diretto.

Netsh

Questo tool, presente di serie sui sistemi windows dai tempi di xp, permette di configurare diversi aspetti della rete via shell (netsh -> net shell), tra cui ovviamente anche l'indirizzo ip.
Il netsh lavora per contesti, quindi prima di dare il comando vero e proprio si deve dichiarare in quale si lavora, nel nostro caso  interface ip. Per impostare l'indirizzo vero e proprio si utilizza set address con questa sintassi:
set address "nome interfaccia" static x.x.x.x y.y.y.y z.z.z.z
completando con il nome dell'interfaccia (di solito Local Area Network) e la famosa terna indirizzo ip, netmask e gateway.
Il comando completo quindi è:
netsh interface ip set address "Local Area Network" 192.168.1.10 255.255.255.0 192.168.1.1

HTA

Dato che comunque sono pigro e non ho voglia di scrivere un intero comando nella shell, ho pensato bene di accorciare il lavoro facendo una applicazione HTA (html application) che richiede le informazioni e lancia il comando di conseguenza.
L'HTA non è altro che una pagina web con un po' di VBScript dentro un file con estensione .hta.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

 <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
 <title>Change IP Address</title>
 <HTA:APPLICATION ID="ChengeIP"
 APPLICATIONNAME="ChangeIP"
 CAPTION="yes"
 BORDER="thick"
 SCROLL="no"
 SHOWINTASKBAR="yes"
 SINGLEINSTANCE="yes"
 SYSMENU="yes"
 MINIMIZEBUTTON="yes"
 MAXIMIZEBUTTON="no"
 >
 <script language="VBscript">
 Sub Window_onLoad
 window.resizeto 380,300
 end Sub
 function ChangeIP(lsNetworkName, lsIp, lsNM, lsGateway)
 DIM objShell
 if lsIp <> "" then
 set objShell = CreateObject("wscript.shell")
 iReturn = objShell.Run("CMD /S netsh interface ipv4 set address """ & lsNetworkName & """ static " & lsIp & " " & lsNM & " " + lsGateway , , True)
 set objShell = Nothing
 end if
 end function
 </script>
</head>
<body bgcolor="#008040">
<p style="text-align:center">
Cambio IP
</p>


<table style="text-align: left; margin-left: auto; margin-right: auto; height: 100px; width: auto;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

 <tr>

      <td style="width: 100px; height: 30px;">Network Name:</td>

      <td style="text-align: center; height: 30px; width: 212px;"><input style="width:200" name="NetworkName" value="Local Area Connection"><br></td>

    </tr>

    <tr>

      <td style="width: 100px; height: 30px;">IP:</td>

      <td style="text-align: center; height: 30px; width: 212px;"><input style="width:200" name="IP"><br></td>

    </tr>

    <tr>

      <td style="width: 100px; height: 30px;">Netmask:</td>

      <td style="text-align: center; height: 30px; width: 212px;"><input style="width:200" value="255.255.255.0" name="Netmask"></td>

    </tr>

    <tr>

      <td style="width: 100px; height: 30px;">Gateway:</td>

      <td style="text-align: center; height: 30px; width: 212px;"><input style="width:200" value="" name="Gateway" ></td>

    </tr>

  </tbody>

</table>

<br>

<div style="text-align: center;"><button onclick="call ChangeIP(NetworkName.value, IP.value, Netmask.value, Gateway.value)" name="Cambia IP" type="button" style="CURSOR: hand">Cambia IP</button><br>

</div>
</body>
</html>

Nessun commento:

Posta un commento