?
Solved

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

Posted on 2006-10-30
10
Medium Priority
?
4,335 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
ID: 17836613
maybe a small .exe
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 17836675
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:Stephen McTigue
ID: 17889238
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 4

Expert Comment

by:Stephen McTigue
ID: 17889318
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:Stephen McTigue
ID: 17925963
did that work?
0
 
LVL 4

Expert Comment

by:Stephen McTigue
ID: 17968458
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:
Stephen McTigue earned 1500 total points
ID: 17968520
@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
ID: 18097480
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
ID: 18105721
PLEASE help it's very urgent!!!
0
 
LVL 2

Author Comment

by:Caiapfas
ID: 18119843
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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

807 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