SIP unreachable but has IP address

Hello!

I have Asterisk 13.11.1 and a lot of IP-phones. When phone logged out (via “Agent” menu), the sip (peer) in Asterisk gets UNREACHABLE mark and has an old IP address. Same with all phones. Then when phone logged in to another account, Asterisk “log in” this account and an OLD account, which even not exists in phone config. And because of that, there are 2 records for 1 phone with same Address. And, of course, it makes a lot of troubles. If I make “sip unregister” old account, all becomes ok.

So I would like ask - how could I make Asterisk able make any unregistered sip account to be “UNKNOWN” instead of “UNREACHABLE” and drop exists IP address to “(Unspecified)” (which is most important thing)?

Sip config is simple:

[agent]
context=agent
host=dynamic
dtmfmode=rfc2833
type=friend
port=5060
qualify=yes
nat=no
allow=alaw
secret=********
call-limit=50
accountcode=123
dial=SIP/123
callerid="agent 123"

Or, maybe, there is a way to force “sip unregister” command as peer becomes UNREACHABLE automatically?

There is no built in mechanism in chan_sip to do this. You could potentially do it by monitoring AMI events and issuing a CLI command to unregister. If the phone unregistered in that scenario as well then you would have no problem.