MSFN Forum: script or command to modify hosts file - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

script or command to modify hosts file Rate Topic: -----

#1 User is offline   Mman 

  • Group: Members
  • Posts: 8
  • Joined: 06-August 04

Posted 14 October 2004 - 10:50 AM

Hi.
I need a script or a command that can be used in a script that will add a line to the hosts file of a computer with the user's permission.

any ideas?

thanks.
M.


#2 User is offline   prathapml 

  • Follow the rules please :-)
  • Group: Patrons
  • Posts: 6,791
  • Joined: 14-November 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 14 October 2004 - 11:45 AM

Do you specifically need the user to say "yes" ? Because if not, then it'd be simple to directly add what you want, into the host file.

But if you do want user to say yes, this will do the task you want, copy/paste into notepad, and save as .BAT:
@echo off
TITLE Modifying your HOSTS file
COLOR 85
ECHO.


:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged >> %systemroot%\Temp\yourinstall.log
ECHO Finished.
GOTO END

:ACCEPTED
ECHO Carrying out requested modifications to your HOSTS file
echo 127.0.0.1   local host >> %WinDir%\system32\drivers\etc\hosts
ECHO Finished.

GOTO END

:END
ECHO.
EXIT


Its quite self-explanatory. While the rest of the command-line script will do your yes/no part, the below line is what does the actual modification.
echo 127.0.0.1   local host >> %WinDir%\system32\drivers\etc\hosts
That is, you can "echo" whatever line you want into the HOSTS file, using the double-forward symbol. The location of the hosts file above, is given for win2k/XP/2k3. For win95/98/ME, its located at a different place.

#3 User is offline   Mman 

  • Group: Members
  • Posts: 8
  • Joined: 06-August 04

Posted 15 October 2004 - 09:18 AM

thanks alot!

the scripy wroks great. I only had to remove the @echo off at the beginning of the code.

GREAT!

NOW - is there any way to run this script automatically through html / xml code so that everyone browsing a certain website will receive the option to run the code?

M.

#4 User is offline   babai93 

  • Group: Members
  • Posts: 1
  • Joined: 14-January 12
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 14 January 2012 - 03:07 AM

Hi, Mr.Prathapml,

Nice script, thanks.

in my scenario I logged in is USER Profile, so how could I run the batch file without having the Admin rights of Admin Password. if i have the Password than I can run it as Administrator. if not than how to do this with the help of Command line which can be embed within the BATCH (.bat) file?

Please let us.


Regards,

Babai Dutta :lol: | 9903010393

#5 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,266
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 14 January 2012 - 12:04 PM

The HOSTS file is a protected file and can not be modified by a non-administrator user. You would have to run the script as an administrator user in order to modify it.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy