Caching RSA key fingerprint in plink using bash script

846 Views Asked by At

I am running an automation script to automate the login and some other commands to be run on a remote target using plink. I used following approach to do a automatic login and saving the RSA key:

user@ubuntu~$ echo -e 'y\n' | plink root@<target ip> -pw <password> "pwd"

This command saves the key when run through command line, but when run using script, is inconsistent in saving the RSA key. Consider username and password being passed as correct, it prompts the error message as the Connection refused, as 'y' is not fetched in the prompt input.

Many times, it will prompt for accepting the key again and again as I have many simultaneous consecutive plink commands used in my script. Ideally, it shouldn't ask for user input more than once. I checked, 'sshhostkeys' file which was not present in ~/.putty folder, which is the cause for prompt for user input each time plink is run.

Has anyone faced this problem earlier? Any fix for this , or any hack/workaround for this?

P.S: Using expect scripts, or manually saving a profile using putty, or manually running the plink command and saving the key for once, is being ruled out (not to be considered).

1

There are 1 best solutions below

0
On

Got the solution, actually the issue was with permission assigned to the $HOME/.putty directory. The ownership information for the folder was also root, thus when I was trying to run

user@ubuntu~$ echo -e 'y\n' | plink root@<target ip> -pw <password> "pwd"

I was getting the prompt for '(y/n)' repeatedly as the key was not getting saved in .putty folder due to the permission issues. Above command when once run was not able to create file sshhostkeys file due to which it was asking again and again for saving the key, each time it tries to save the key but was not able to save as it didn't have root permission. This issue is resolved by assigning rwx permission for all other (sudo chmod 707 ~/.putty) or other approach can be changing the ownership information to the user running the script by 'chown'.