Entries in host file at particular line - windows-7

I am looking for a script that can add host entries in a windows 7 host files at particular lines or just after the default text in host file.
I came across this thread in your forum here that can add/remove the lines perfectly but it adds the lines at the end of the file not at the top. can you please help with the same?

hey thanks for the link.
Since i have limited exposure to batch files, I wanted to check that method offline first
1) created a txt file dns.txt with some DNS entries
1.2.3.4 abcd.com
2.3.4.5 bcde.com
3.4.5.6 cdef.com
4.5.6.7 defg.com
2) created a batch file with one line code in it:
for /F %%i in (c:\temp\dns.txt) do echo %%i >> "D:\hosts"
3) but when i check the host file, it only has IP entries and not the hostnames against it.
==================
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
1.2.3.4
2.3.4.5
3.4.5.6
4.5.6.7
====================
what is wrong !!?

Related

Why does changing a hostname in the hosts file not change my computer name?

I have to run some servers with different names. So I changed the hostname in Windows 10 inside C:\Windows\System32\drivers\etc\hosts. I changed the name from localhost to brandcil.local:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 brandcil.local
# ::1 brandcil.local
Now when I am running nslookup 127.0.0.1 in powershell terminal, it is showing this:
Server: UnKnown
Address: 192.168.43.1
Name: localhost
Address: 127.0.0.1
I restarted my machine but it does not take effect. How do I change the hostname then?
Changing the hosts file will not change the hostname. It is only used for hostname-IP-resolution. Besides that, each line that begins with # is a comment and not an active configuration. You can rename a computer with the following PowerShell cmdlet:
Rename-Computer -NewName brandcil -Restart
First of all, any line in the hosts file that begins with # is a comment and will be ignored. The file itself tells you this if you read it. Second, the hosts file has nothing to do with your computer's hostname. All you're doing is creating aliases that will only work on the machines you modify the hosts file on. You're not changing the hostname at all. Third, localhost -> 127.0.0.1 is a static mapping no longer controlled by the hosts file so it will always map to 127.0.0.1. Again, the hosts file tells you this if you read it.
To rename your computer go into Settings --> System --> About. There's a "Rename this PC" button.
If you don't want to use that, then I recommend using the Rename-Computer Powershell command.

Windows routes - add many lines to routes table at once

I have to add many lines (rules) to the routes table in Windows.
The regular way is to add it with:
route add 10.0.0.0 mask 240.0.0.0 192.168.0.1
However, if I want to add many lines at once (belive me I have a good reason to do it) - how can I do it quickly (running "route add" is slow)?
I tried running it in some threads and it still slow.
Option 1:
You can use this MS tool: netsh
Netsh is a command-line scripting utility that allows you to, either
locally or remotely, display or modify the network configuration of a
computer that is currently running.
Build a file with all the lines you want to add, like this:
[For more information on "add route" command and its parameters, see here].
# ----------------------------------
# IPv4 Configuration
# ----------------------------------
pushd interface ipv4
add route prefix=10.0.0.0/4 interface="Ethernet" nexthop=192.168.0.1 metric=1 publish=Yes
add route prefix=240.0.0.0/4 interface="Ethernet" nexthop=192.168.0.1 metric=1 publish=Yes
[and so on...]
popd
# End of IPv4 configuration
Save it in a *.dat file, and run this command:
netsh exec file.dat
Option 2:
[As Harry Johnston commented]: You can use CreateIpForwardEntry function to add route (example code in the link).
The CreateIpForwardEntry function creates a route in the local
computer's IPv4 routing table.

output only certain results in a log file from batch file

relatively new to batch files so please help me out;). What I'm trying to do is create a customized log file. Simple example:
ipconfig >> ip.log
this will display all the TCP/IP related info.
Let's say I only need the user to see the IP and Default Gateway, so the result in the output ip.log should look like this:
IPv4 Address: xxx.xxx.xxx.xxx
Default Gateway: xxx.xxx.xxx.xxx
How do I do this?
Generally you would use a filter for that purpose. You want to filter on certain strings, so findstr command is natural choice:
ipconfig | findstr /c:"IPv4 Address" /c:"Default Gateway" >>ip.log

How to make a duplicated target work in Xcode 4?

I've duplicated a target in Xcode 4, but when I hit run, the application starts and gets stuck in the launching screen and I've got an error in the Xcode console:
error: failed to attach to process ID 3957
I've already searched on the internet for some possible solutions but all of them didn't seem to solve this issue.
Any other clues?
When you duplicate the target, the project will try to use the same bundle ID. Which in some cases is not what you want. Try changing it if you don't need to use the same bundle identifier.
Step1: ping
$ ping localhost
This should return something like
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.048 ms
...
If this works, this answer won't fix your problem, try something else.
If ping return something else for example: ping: cannot resolve localhost: Unknown host something is screwed up with your /etc/hosts file, go to Step 2
Step2: Check /etc/hosts
Check that the top of your /etc/hosts file looks like this
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
If it doesn't have these entries in the file, enter them at the top of the file, flush the dns cache using $ dscacheutil -flushcache and go back to Step 1, otherwise continue to Step 3.
Step3: Correct File Format: It should be unix or LF *
$ file /etc/hosts
This should return: /etc/hosts: ASCII English text
If it returns something like /etc/hosts: ASCII English text, with CR line terminators then the file is in the wrong format and is likely being ignored.
Change the file line endings to unix or LF using your favorite text editor.
In Sublime Text 2 this can be done throught the view menue: View > Line Endings > Unix
Flush the dns cache ($ dscacheutil -flushcache) and go back to step 1

How to edit hosts file via CMD? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Hello I'd like to block some websites directly from the command prompt.
echo like this:
0.0.0.0 websitename.com
How can I do this? (OS: Windows 7)
echo 0.0.0.0 websitename.com >> %WINDIR%\System32\Drivers\Etc\Hosts
the >> appends the output of echo to the file.
Note that there are two reasons this might not work like you want it to. You may be aware of these, but I mention them just in case.
First, it won't affect a web browser, for example, that already has the current, "real" IP address resolved. So, it won't always take effect right away.
Second, it requires you to add an entry for every host name on a domain; just adding websitename.com will not block www.websitename.com, for example.
Use Hosts Commander. It's simple and powerful. You can download it here.
Examples of using
hosts add another.dev 192.168.1.1 # Remote host
hosts add test.local # 127.0.0.1 used by default
hosts set myhost.dev # new comment
hosts rem *.local
hosts enable local*
hosts disable localhost
...and many others...
Help
Usage:
hosts - run hosts command interpreter
hosts <command> <params> - execute hosts command
Commands:
add <host> <aliases> <addr> # <comment> - add new host
set <host|mask> <addr> # <comment> - set ip and comment for host
rem <host|mask> - remove host
on <host|mask> - enable host
off <host|mask> - disable host
view [all] <mask> - display enabled and visible, or all hosts
hide <host|mask> - hide host from 'hosts view'
show <host|mask> - show host in 'hosts view'
print - display raw hosts file
format - format host rows
clean - format and remove all comments
rollback - rollback last operation
backup - backup hosts file
restore - restore hosts file from backup
recreate - empty hosts file
open - open hosts file in notepad

Resources