Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a small script I used to ping all the hostnames in my known_hosts file and pipe the output to a file called badhosts. I want to use the badhosts file as a reference to remove old hostname entries from my known_hosts file that ssh uses. I am attempting the following and not having any luck:
LIST=/badhosts
while read line; do
echo $line
sed -e "/\${line}/d" known_hosts >> new_known_hosts
done < $LIST
I have tried different quoting and escaping, etc. but nothing is working. Any help would be appreciated.

That helps, but am still having issues. If the file badhosts contains:
test1
test2
test3
and known_hosts contains:
test1
test5
test4
test2
test3the new_known_hosts file will end up with the following:
test1
test5
test4
test2As you can see, test3 did go away, but new_known_hosts should only have the following:
test5
test4

The reason sed will not work in this case is because each time the loop executes, one line may be deleted, but the rest of the lines are printed. This happens each loop.
Maybe somebody smarter with sed can get this to work, but not me, so I changed the design. I read each line of the known_hosts file.and if it does not exist in the badhosts file (count = 0), I echo it:
#!/bin/ksh
LIST=./badhosts while read line; do if [[ $(grep -c ${line} $LIST) -eq 0 ]] then echo "$line" >> new_known_hosts fi done < known_hosts

I tested your idea and unfortunately, that didn't work either.
Received the error: ./badhosts:0: bad number
I am open to any ideas, not just sed if anyone else has an idea as to how to remove old hosts from the known_hosts file.

The problem is probably that the script can not find the badhosts file. Your original post had badhosts in the root directory:
/badhosts
I do not like to put data files in root. This:
./badhosts
means search the current directory. Fix the location of badhosts and you will probably be OK

![]() |
Redirect path in csh scri...
|
awk getline issue
|
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |