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 [email protected]
[email protected]'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).

4
  • stackoverflow.com/a/43526842/13317 Commented Jul 6, 2019 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 Commented Jul 6, 2019 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 Commented Jul 8, 2019 at 6: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 '<Linux_command>'. So in my Jenkins file it could be like sh 'ssh -i aws_keypair.pem [email protected] "dir" ' then it should connect to the remote WINDOWS machine and execute dir command over there and show me the output. Commented Jul 8, 2019 at 8:04

2 Answers 2

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  
Sign up to request clarification or add additional context in comments.

Comments

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 [email protected] -pw abc123  "dir"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.