Mohit Deshpande
Mohit Deshpande

Reputation: 55257

Simulate key presses in C

For example, I want to run:

ssh [email protected] -p 2222

in C (via system) command. But right after I run that, it asks for input:

[email protected]'s password:

Then I'm expected to type in the password. How can I do this in C code? Could I please get some code on how I could simulate key presses? Or is there a better way to do this?

Upvotes: 0

Views: 593

Answers (3)

Yuriy Vikulov
Yuriy Vikulov

Reputation: 2499

there is a beautiful command expect

This is a common used tool. If you need only for a ssh, you'd better look at other posts for generating a key

Upvotes: 3

Bacon Bits
Bacon Bits

Reputation: 32230

There are better ways to do this, such as key-based SSH authentication.

Upvotes: 4

BMitch
BMitch

Reputation: 265130

You should run:

ssh-keygen -t rsa
ssh-copy-id [email protected] -p 2222

This will copy an authorized key to perform ssh commands from your account to root on your own machine. Future ssh requests won't prompt for a password. This also means you don't have to include the root password in your shell script.

Another option would be the sudo command. You can add approved commands into the sudoers file, including an option to ignore the password requirement for those commands.

Upvotes: 1

Related Questions