Batch/vbs script to add a new entry in the hosts file when run

Hi,

Batch/vbs script to add a new entry in the hosts file when run

Can anyone help me with a script.

regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
need more info on this one, but essentially:

echo 12.34.45.67  hostname >> %systemroot%\system32\drivers\etc\hosts

should work.

If you want it parameterized:

@echo off
if [%1]==[] goto :usage
if [%2]==[] goto :usage

set ip=%1
set host=%2

echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts

goto :eof
:usage
echo.  Must supply the IP and hostname as parameters
echo.  Example:  UpdateHost  11.22.33.44  MyNewHost

Open in new window

bsharathAuthor Commented:
Hi Sirbounty
Should the code look as this
In the code at the end should i mention the host name and ip as below?
@echo off
if [%1]==[] goto :usage
if [%2]==[] goto :usage

set ip=%1
set host=%2

echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts

goto :eof
:usage
echo.  Must supply the IP and hostname as parameters
echo.  Example:  UpdateHost  11.22.33.44  MyNewHost
exchangeserver name ipaddress

Open in new window

Steve KnightIT ConsultancyCommented:
To save repeating you may want to check with a find if it is there already too:

@echo off
set host=hostname
set ip=1.1.1.1
find "%host" "%systemroot%\system32\drivers\etc\hosts"
if errorlevel 1 (
 echo %Host% not found so adding to HOSTS file:
echo %ip% %host% >> %systemroot%\system32\drivers\etc\hosts
)

Any reason why using hosts and not DNS, or is this for when people are connected to different networks etc?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

bsharathAuthor Commented:
dragon it you are right for other users
I used your code the command prompt does not close it goes on reading
bsharathAuthor Commented:
It gets stuck in this line
find "systemroot\system32\drivers\etc\hosts"
Steve KnightIT ConsultancyCommented:
sorry I missed a % it should be %host% instead.

steve
yehudahaCommented:
hey

just addded the vbs version
doesn't check if the supply arguments all redy in the hosts file
Set objfso = CreateObject("Scripting.FileSystemObject")
strHosts = "C:\WINDOWS\system32\drivers\etc\hosts"
If WScript.Arguments.Count = 0 Then
WScript.Echo  "Must supply the IP and hostname as parameters"
wscript.echo  "Example: " & WScript.ScriptName & " 11.22.33.44  MyNewHost"
WScript.Quit
Else
set objhosts = objfso.OpenTextFile(strHosts,8)
objhosts.WriteLine WScript.Arguments.Item(0) & "    " & WScript.Arguments.Item(1)
objhosts.Close
WScript.Echo "Done"
End If

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Looks like Steve missed a % sign, change the corresponding line to the following:

find "%host%" "%systemroot%\system32\drivers\etc\hosts"

Be aware though that in some versions of Windows the hosts file is protected from update unless running with elevated privileges.

~bp
Steve KnightIT ConsultancyCommented:
Thanks Bill, yes it was a typo, I mentioned that above but was holding baby at the time so didn't type much on mobile!

You could run this as a startup script, or other methods but generally speaking users won't have access to change the hosts table themselves (e.g. from a login script) if they aren't admins.

Steve
Steve KnightIT ConsultancyCommented:
Cool, glad if it helped.

"Windows Batch Scripting: 497,692 (2,308 more points until Sage)"... so close and yet v quiet today!

Steve
Bill PrewIT / Software Engineering ConsultantCommented:
Well, some of us are just trying to claw our way into the Wizard tier.  Hope you get some viable questions shortly.

~bp
bsharathAuthor Commented:
WOW... :-)
bsharathAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.