Computing.Net > Forums > Unix > Remove old hosts from known_hosts

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove old hosts from known_hosts

Reply to Message Icon

Name: sunsysadm2003
Date: March 11, 2009 at 11:50:01 Pacific
OS: Solaris 10
CPU/Ram: n/a
Product: N/a / N/A
Subcategory: Configurations
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 11, 2009 at 12:13:37 Pacific
Reply:

Embedding shell variables in sed scripts can be tricky:

sed -e '/'"${line}"'/d' known_hosts


0

Response Number 2
Name: sunsysadm2003
Date: March 11, 2009 at 14:59:57 Pacific
Reply:

That helps, but am still having issues. If the file badhosts contains:
test1
test2
test3
and known_hosts contains:
test1
test5
test4
test2
test3

the new_known_hosts file will end up with the following:
test1
test5
test4
test2

As you can see, test3 did go away, but new_known_hosts should only have the following:
test5
test4


0

Response Number 3
Name: nails
Date: March 11, 2009 at 20:58:51 Pacific
Reply:

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


0

Response Number 4
Name: sunsysadm2003
Date: March 12, 2009 at 10:04:18 Pacific
Reply:

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.


0

Response Number 5
Name: nails
Date: March 12, 2009 at 10:24:08 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Redirect path in csh scri... awk getline issue


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Remove old hosts from known_hosts

Remove old files from Dir > 60 days www.computing.net/answers/unix/remove-old-files-from-dir-60-days/7847.html

Removing old users from /etc/passwd www.computing.net/answers/unix/removing-old-users-from-etcpasswd/7674.html

to remove duplicate lines from file www.computing.net/answers/unix/to-remove-duplicate-lines-from-file/7011.html