die when prompted for password in perl
I have a script that is using a ssh key for connecting to a 开发者_开发百科remote server for a specific user. However, id the user information is changed and the keys are not updated, the script will hang waiting for a password.
How can I trap this and throw DIE when prompted for a password?
For example, if I use:
system("ssh -C USER@someserver.com -i /.ssh/USER.key ...");
and USER is not the same USER in USER.key, it hangs waiting for a password. I would rather have it die.
What is the best way to handle this without using a perl module?
Try this:
system('ssh -o BatchMode=yes -C USER@someserver.com -i /.ssh/USER.key ...');
The BatchMode option will prevent ssh from prompting for passwords, and will simply terminate if a password is required.
When you use system
like that, you relinquish control to ssh
. I recommend you look at the options for ssh
.
Also, you should use warnings.
Have you considered using any of the SSH modules availables from CPAN as Net::SSH2 or Net::OpenSSH?
精彩评论