117

I need to execute ssh from windows command line by providing password in a non interactive manner. I could implement the key based authentication and able to execute the ssh commands just like

ssh <user>@<host> <command>

Is there any commands like

ssh <user>@<host> -P <password> <command>

enter image description here

I don't know if it is feasible. However, there can be some work around for the same. Throw me some ideas to accomplish the same.

3
  • 2
    Using key-based authentication is a much better idea. Commented Aug 25, 2012 at 0:53
  • 1
    Yeah i have a requirement for password based authentication too. Commented Aug 25, 2012 at 2:32
  • 19
    @GregInozemtsev while that the case, sometimes the need arises for a quick-and-dirty script to do something like this, especially in a testing or other environment where pure security isn't required. Commented Aug 21, 2013 at 3:08

7 Answers 7

142

The sshpass utility is meant for exactly this. First, install sshpass by typing this command:

sudo apt-get install sshpass

Then prepend your ssh/scp command with

sshpass -p '<password>' <ssh/scp command>

This program is easiest to install when using Linux.

User should consider using SSH's more secure public key authentication (with the ssh command) instead.

Sign up to request clarification or add additional context in comments.

10 Comments

Not entirely sure how a linux utility (that can be make'd for Cygwin, but that is a whole different level of sysadmining) gets 64 up-votes... and Plink which does exactly what the OP asked and doesn't require any additional work (and is probably already installed on their system in the first place) gets 1. I tend to trust Stack on these things, so if there is a good reason to jump through the generally annoying and occasionally maddening hoops of make... er... I am genuinely curious why it got so much love.
Because the title does not include "windows" and it shows high in the list when search for this for Linux/Unix/Mac. So, answering this question here saves time.
You should be aware that executed shell commands get stored (for example in '.bash_history') ..
Anish - The author asked for windows, you gave option for linux.
How is this an acceptable answer to a question that's related to windows ssh?
|
30

PuTTY's plink has a command-line argument for a password. Some other suggestions have been made in the answers to this question: using Expect (which is available for Windows), or writing a launcher in Python with Paramiko.

2 Comments

The author was asking how to do this with ssh.exe. People know PuTTy can do this, but the question isn't "what do I install that can do this", it's "how can I do this with SSH.exe".
@sprong: This question and the answers are from 2012, 5 years before Windows got a proper built-in ssh client. Back then the only option was plink, or using expect. Both options are given in my answer.
30

Windows Solution

  1. Install PuTTY
  2. Press Windows-Key + R
  3. Enter putty.exe -ssh [username]@[hostname] -pw [password]

1 Comment

The author was asking how to do this with ssh.exe. People know PuTTy can do this, but the question isn't "what do I install that can do this", it's "how can I do this with SSH.exe".
22

What about this expect script?

#!/usr/bin/expect -f
spawn ssh root@myhost
expect -exact "root@myhost's password: "
send -- "mypassword\r"
interact

2 Comments

It says I do not have the expect script
this is a LINUX solution the question is about WINDOWS!!!
8

PowerShell solution

Using Posh-SSH:

New-SSHSession -ComputerName 0.0.0.0 -Credential $cred | Out-Null
Invoke-SSHCommand -SessionId 1 -Command "nohup sleep 5 >> abs.log &" | Out-Null

1 Comment

Only works with PowerShell 6 (not Core versions)
7

This post is a valid solution for your issue.

  1. Install PuTTY on your Windows Machine
  2. Execute 'plink your_username@yourhost -pw your_password'

If you use the Windows Terminal and you want your shell to log into a remote machine when opening a new tab, you may configure the command line params (Settings -> Shell -> Command line):

C:\Users\USERNAME\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell.ID\pwsh.exe
-Command "plink [email protected] -pw PASSWORD"

1 Comment

PowerShell Core must be installed from app store, or using .msixbundle from GitHub releases page github.com/PowerShell/PowerShell/releases
2

I agree with @Owain Esau, but I suggest using sshsessions module

try{
    import-module sshsessions -ea stop
    $continue =  $true
    }
catch{
    $continue =  $false
    }

[string][ValidateNotNullOrEmpty()]$ComputerName = 'cvmbox-cmx.mydomx.com'   
[string][ValidateNotNullOrEmpty()]$username = "root"
[string][ValidateNotNullOrEmpty()]$password = 'Super$ecretP@ssw0rd'
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$securePassword

function Nu-Connect($Credentials, $computerName) {
    remove-SshSession -RemoveAll -WarningAction SilentlyContinue
    $varsession = New-SshSession -ComputerName $computerName -Credential $Credentials
    $IsConnected = Get-SSHSession
    Write-Output "Connected to $($IsConnected.computername)"     
    return $varsession
}
if($continue){
    Nu-Connect -ComputerName $ComputerName -Credentials $credential

    $command = "df"
    $df = (Get-SshSession -ComputerName $ComputerName | Invoke-SshCommand -Command $command -Quiet).result 
    $df
}
else {
    write-host "SSH module not found in Host : $($env:computername)"
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.