Simulate key presses in C
For example, I want to run:
ssh root@127.0.0.1 -p 2222
in C (via system
) command. But right afte开发者_开发技巧r I run that, it asks for input:
root@127.0.0.1'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?
There are better ways to do this, such as key-based SSH authentication.
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
You should run:
ssh-keygen -t rsa
ssh-copy-id root@127.0.0.1 -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.
精彩评论