Task Chains : Common Automation Tasks : Cleaning Up Open Sessions

Cleaning Up Open Sessions

A task chain that includes a task like bruteforce, exploit, or module run may open a session on the target system. An open session enables you to interact with the exploited system. For example, you can do things like collect screenshots of the system or pivot to other hosts on the network.
When you are done with a session, you should close the connection with the session. This is known as a session clean up.
To clean up active sessions, you should add a clean up task to the task chain. As a rule of thumb, the clean up task should be one of the last tasks in the task chain. This ensures that Metasploit Pro has the opportunity to collect system information and take advantage of open sessions before it closes them.
Generally, a report task should be the only task that you add after a clean up task.
The following image shows a typical task chain that uses the clean up task: