2

I am trying to SSH to a remote Windows machine (an AWS ec2 instance) by using an username and password of the remote machine. I need to automate this connection to run some remote commands from my script (either shell or Power shell) without prompting me for a password, My script shouldn't fail by expecting a password to be prompted

I don't want to use sshpass or any generated keys (by using ssh-keygen). Since the source machine where I run this command/script is not a dedicated machine, I may run it on a different machine everytime. I also gave a try to connect using the .PEM file provided by AWS as below (thought it could be easy while using it the script).

$ssh -i aws_keypair.pem Administrator@10.10.10.10
Administrator@10.10.10.10's password:

It is still expecting me for a password even if I used the .PEM file, I also tried to created an file 'authorized_keys' in the remote Windows machine under the path "C:\Users\Administrator.ssh\". Still it is prompting me for a password.

Expectation :

Connect to remote Windows machine using PEM file and run some remote commands.

(or)

It shouldn't prompt me for a password while I try for the connection from some script (shell/power shell).

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
phani
  • 65
  • 1
  • 6
  • https://stackoverflow.com/a/43526842/13317 – Kenster Jul 06 '19 at 19:20
  • @Kenster, The above link you shared is still talking about sshpass (or) generate keys (or) use Putty.exe But none of the answers covered my exact question of using PEM file to connect Remote Windows machine (which is running on AWS) or script should't fail prompting for password. SSH command should accept the username and passowrd – phani Jul 06 '19 at 19:41
  • What application are you using? SSH is not a native Powershell command so ~(I assuem you're using Putty though? You're probably better off tagging the question with the application name but you could also try using the Powershell module that interfaces with it - POSH-SSH – Scepticalist Jul 08 '19 at 06:35
  • I am trying this approach on Jenkins Pipeline code, which could be written in Groovy. So I use to run the SHELL commands with sh ''. So in my Jenkins file it could be like sh 'ssh -i aws_keypair.pem Administrator@10.10.10.10 "dir" ' then it should connect to the remote WINDOWS machine and execute dir command over there and show me the output. – phani Jul 08 '19 at 08:04

2 Answers2

1

Can be done without any 3rd party tools like this:

$env:TMPPW=Get-Content -Path 'secure_file.txt' ; $un='MyUserName'  
$j=Start-Job -ScriptBlock{Start-Sleep -Seconds 1  
(New-Object -ComObject wscript.shell).SendKeys("$env:TMPPW{ENTER}")}  
& ssh.exe -q -4 -l $un 127.0.0.1 'whoami'  
$env:TMPPW=([guid]::NewGuid()).Guid ; $env:TMPPW=$null  
Phill
  • 11
  • 2
0

I am able to achieve this using Plink command (Installation of Putty is required on source machine to execute this command). So now, I am able to successfully pass the username and password with in the script (Shell script) and everything is working as expected.

FYI, I am pasting the exact command which worked for me

$echo y | plink -ssh Administrator@10.10.10.10 -pw abc123  "dir"
phani
  • 65
  • 1
  • 6