Solved

need a batch file to add a line of text to the host file

Posted on 2006-10-30
10
4,307 Views
Last Modified: 2007-12-19
I'm a web developer and have to have each client edit their host file so they can see their website BEFORE they change their dns. Having to walk them through it each time is insane. I can't send them through the ip for our site catchs that and sends them to the domian , sending them back to the old website.


So what I need is a batch file that once clicked on from the desktop opens and ask for ip (enter here) hit enter, then domain name (enter here) hit enter. Then takes that info and enters it at the end of their hosts file.

And then away for this same file to remove the entry after we change their dns would be nice!!
0
Comment
Question by:Caiapfas
  • 5
  • 4
10 Comments
 
LVL 2

Author Comment

by:Caiapfas
Comment Utility
maybe a small .exe
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
TEST THIS FIRST.  It should work, but I'm most uncertain about the "restore" part.  Also, if the user does not have admin rights, this won't work.

---------------8<---------------------------------
@echo off
set /p SiteLive=Is the site live? (y/N):
if "%sitelive%"=="Y" Goto RestoreHosts
set /p IP=Enter IP Address:
set /p DomName=Enter Domain Name:
copy "%windir%\system32\drivers\etc\hosts" "%windir%\system32\drivers\etc\hosts.%date:~-4%%date:~4,2%%date:~7,2%.live"
Echo %ip%   %domname%>>"%windir%\system32\drivers\etc\hosts"
Goto End
:RestoreHosts
REM Backup current hosts file
copy "%windir%\system32\drivers\etc\hosts" "%windir%\system32\drivers\etc\hosts.%date:~-4%%date:~4,2%%date:~7,2%.dev"
REM Restore previously backed up hosts file
copy "%windir%\system32\drivers\etc\hosts.*.live" "%windir%\system32\drivers\etc\hosts"
:End
---------------8<---------------------------------

0
 
LVL 4

Expert Comment

by:mctigue
Comment Utility
try this.  I don't know where the host file would be located, but just put the path where the parentheses are right now. for example if the host file was called hostfile.txt and was in the windows directory, delete the parentheses and everything between them, and put: c:\windows\hostfile.txt
of course that isn't really where it is or what it is called, but you get the idea.



@echo off
set /p ip=Enter ip here:
set /p domain=Enter domain here:
echo %ip%>>(the name & path of host file)
echo %domain%>>(the name & path of host file)
0
 
LVL 4

Expert Comment

by:mctigue
Comment Utility
if you want it to look a little more fancy, you could also do this:


@echo off
set /p ip=Enter ip here:
echo %ip%>>(the name & path of host file)
echo IP copied to host file
pause
cls
set /p domain=Enter domain here:
echo %domain%>>(the name & path of host file)
echo Domain copied to host file
pause
cls
echo Done!
pause
0
 
LVL 4

Expert Comment

by:mctigue
Comment Utility
did that work?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:mctigue
Comment Utility
actually, that won't work because that will put the ip and domain at separate lines of the host file.  here is what will work:


@echo off
set /p ip=Enter IP here:
set /p Domain=Enter domain here:
echo %ip%    %domain%>>%systemroot%\System32\drivers\etc\HOSTS
cls
echo Done!
pause






and if you want to be able to check the host file and see if it was copied correctly, you could add this line to the bottom:

notepad %systemroot%\System32\drivers\etc\HOSTS

0
 
LVL 4

Accepted Solution

by:
mctigue earned 500 total points
Comment Utility
@echo off
set /p ip=Enter IP here:
set /p domain=Enter Domain here:
echo %ip%    %domain%>>%systemroot%\System32\drivers\etc\HOSTS
cls
echo Done!
pause





just fixed some capitalization errors.  that should work.  Good luck.
0
 
LVL 2

Author Comment

by:Caiapfas
Comment Utility
it worked GREAT mctigue , but it needs to put it at the VERY end on a new line. it just put it after another entry i had, NICE work
0
 
LVL 2

Author Comment

by:Caiapfas
Comment Utility
PLEASE help it's very urgent!!!
0
 
LVL 2

Author Comment

by:Caiapfas
Comment Utility
500 more points here, please help me with the last part!!
http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_22089973.html
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now