Chuck Burgess
Chuck Burgess

Reputation: 11574

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 [email protected] -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?

Upvotes: 2

Views: 265

Answers (3)

salva
salva

Reputation: 10234

Have you considered using any of the SSH modules availables from CPAN as Net::SSH2 or Net::OpenSSH?

Upvotes: 0

cdhowie
cdhowie

Reputation: 168958

Try this:

system('ssh -o BatchMode=yes -C [email protected] -i /.ssh/USER.key ...');

The BatchMode option will prevent ssh from prompting for passwords, and will simply terminate if a password is required.

Upvotes: 7

Sinan Ünür
Sinan Ünür

Reputation: 118118

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.

Upvotes: 4

Related Questions