I was recently compiling a script which would act as a wrapper to invoke commands depending on the hosts operating system (which was read from a file) and passes a set of parameters. If the operating system was UNIX, the plan was to invoke a SSH session remotely using PuTTY Plink and to invoke the command and pass a set of parameters.
The first obstacle was that when connecting using SSH to connect to a server for the first time you will prompted to trust the host key of the server as below:
The server’s host key is not cached in the registry. You have no guarantee that the server is the computer you think it is. The server’s rsa2 key fingerprint is: ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx If you trust this host, enter “y” to add the key to PuTTY’s cache and carry on connecting. If you want to carry on connecting just once, without adding the key to the cache, enter “n”. If you do not trust this host, press Return to abandon the connection. Store key in cache? (y/n)
This would force the commands invoked to halt until user input had been received. Therefore, this required me to modify the commands invoked from the wrapper to first connect and accept to store the servers host key in cache. This was simply achieved by using echo to pipe the user input to the selected command and then exit the SSH session, the wrapper will then invoke the plink executable a second time to run the required command and pass a set of variables as below;
echo y | plink.exe -ssh $line.Server -l $Username -pw $Password exit
plink.exe -ssh $line.Server -l $Username -pw $Password <insert remote command here>