system ('ssh -O exit localhost 2> /dev/null;'); use Expect; print Expect::version(); $su=new Expect; $su->log_user(1); $su->spawn ("ssh localhost echo aa"); $su->soft_close(); # this step hang 15 seconds # $su->expect(undef); exit 0; #### system ('ssh -O exit localhost 2> /dev/null; ssh -M localhost -fN 2> /dev/null'); use Expect; $su=new Expect; $su->log_user(1); $su->spawn ("ssh localhost echo aa"); $su->soft_close(); # this step now will not take more than 1 second #$su->expect(undef); exit 0;