109

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.

msanford
  • 11,803
  • 11
  • 66
  • 93
Balachandar
  • 1,538
  • 3
  • 16
  • 25
  • 2
    Using key-based authentication is a much better idea. – Greg Inozemtsev Aug 25 '12 at 00:53
  • 1
    Yeah i have a requirement for password based authentication too. – Balachandar Aug 25 '12 at 02:32
  • 17
    @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. – TheJosh Aug 21 '13 at 03:08
  • I don't figure out why is missing that basic option. I was looking for -pw superputty (putty) command :( I guess i will move to ssh keys instead. – m3nda May 03 '15 at 14:00

7 Answers7

153

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
Anish Bhatt
  • 1,775
  • 2
  • 10
  • 3
  • 19
    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. – OhkaBaka Apr 23 '14 at 23:50
  • 17
    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. – Frobbit May 02 '14 at 19:10
  • It sure saved me time. On OS X, you can't get this with Homebrew except from an unofficial repo, which is easy to find. – sudo Jul 08 '14 at 06:55
  • 1
    Worked great in Mac OS X. I installed sshpass via `sudo port install sshpass`. Though, if there is a problem logging in due to something else, sshpass tended to fail silently (no error message). So debug the command without `sshpass` first; then add `sshpass -p blah` (etc.). – RedRedSuit Jul 29 '14 at 17:54
  • 6
    You should be aware that executed shell commands get stored (for example in '.bash_history') .. – Benjamin Feb 19 '15 at 08:44
  • 9
    Anish - The author asked for windows, you gave option for linux. – arka.b Nov 19 '15 at 12:48
  • In order to run sshpass in Linux CentOS you must `yum -y install epel-release` and then `yum -y install sshpass` – Junior Mayhé Sep 28 '16 at 18:12
  • @arka.b, As Frobbit said, this shows high in the search results for "specify password in ssh command" for example, and the OP didn't state for a Windows answer. – foxtdev Feb 28 '19 at 21:25
  • 1
    @Benjamin Then prepend a space to a command, it won't be stored then. – Cromax Sep 01 '21 at 13:32
  • How is this an acceptable answer to a question that's related to windows ssh? – Ricky Mar 27 '23 at 17:51
28

What about this expect script?

#!/usr/bin/expect -f
spawn ssh root@myhost
expect -exact "root@myhost's password: "
send -- "mypassword\r"
interact
Illarion Kovalchuk
  • 5,774
  • 8
  • 42
  • 54
25

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.

Community
  • 1
  • 1
Greg Inozemtsev
  • 4,516
  • 23
  • 26
21

Windows Solution

  1. Install PuTTY
  2. Press Windows-Key + R
  3. Enter putty.exe -ssh [username]@[hostname] -pw [password]
optimiertes
  • 4,002
  • 1
  • 22
  • 15
6

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
Owain Esau
  • 1,876
  • 2
  • 21
  • 34
5

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 USERNAME@192.168.0.1 -pw PASSWORD"
Bin4ry
  • 652
  • 9
  • 34
  • PowerShell Core must be installed from app store, or using .msixbundle from GitHub releases page https://github.com/PowerShell/PowerShell/releases – Alfishe Jul 05 '22 at 19:13
1

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)"
    }