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>
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.6k10 gold badges67 silver badges92 bronze badges
asked Aug 25, 2012 at 0:50
4
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.3k9 gold badges91 silver badges120 bronze badges
answered Jan 23, 2013 at 21:53
Anish BhattAnish Bhatt
1,7732 gold badges10 silver badges3 bronze badges
9
What about this expect script?
#!/usr/bin/expect -f
spawn ssh root@myhost
expect -exact "root@myhost's password: "
send -- "mypasswordr"
interact
answered Sep 8, 2014 at 13:01
2
Windows Solution
- Install PuTTY
- Press
Windows-Key + R
- Enter
putty.exe -ssh [username]@[hostname] -pw [password]
answered Jul 9, 2018 at 11:37
optimiertesoptimiertes
3,9221 gold badge21 silver badges15 bronze badges
0
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
answered Mar 8, 2019 at 4:06
Owain EsauOwain Esau
1,8662 gold badges20 silver badges32 bronze badges
1
This post is a valid solution for your issue.
- Install PuTTY on your Windows Machine
- 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:UsersUSERNAMEAppDataLocalMicrosoftWindowsAppsMicrosoft.PowerShell.IDpwsh.exe
-Command "plink USERNAME@192.168.0.1 -pw PASSWORD"
answered Nov 7, 2021 at 20:34
Bin4ryBin4ry
5908 silver badges34 bronze badges
1
From the terminal I type: ssh user@ip
and then it prompts for a password.
Is there a way to specify the password in the ssh command itself?
asked Nov 26, 2020 at 9:26
0
Use sshpass
, one of two forms:
sshpass -ffilename ssh user@ip # prefer this
sshpass -pPa5sw0rd ssh user@ip # avoid this
where your password is literally Pa5sw0rd
or it is in the first line of the file filename
. Notes:
- In the manual there is no space after
-p
or-f
, but at leastsshpass
1.06 in my Debian 10 allows it; yoursshpass
may or may not. - If your password contains characters your shell will interpret (like
$
,'
or;
) then you should quote it properly in the command line (but not in the file). - Avoid
-p
. Usechmod 600 filename
to make the file private (root will still be able to access it though). Read about security considerations in the manual.
answered Nov 26, 2020 at 10:51
Kamil MaciorowskiKamil Maciorowski
64.8k22 gold badges118 silver badges175 bronze badges
3
The correct way to do this, is to switch from password authentication to a public/private key pair. This typically needs no reconfiguration at all and is quite easy.
Step 1: If you do not have a key, create one: ssh-keygen
will do that for you
Step 2: Authorize this key on the remote host: Run ssh-copy-id user@ip
once, using your password
Step 3: From now on ssh user@ip
will no longer ask for your password
answered Nov 26, 2020 at 9:57
Eugen RieckEugen Rieck
19.6k5 gold badges49 silver badges45 bronze badges
3
If you ever need to securely connect to a remote machine and reach a command-line interface, SSH is the tool for the job. The Secure Shell Protocol (SSH) has been used for decades as a means to remotely interface with Unix / Linux / BSD machines over insecure connections.
SSH operates on a traditional client — server principle where a remote user (client) can connect to a server and run commands as if they were sitting at the machine. SSH is particularly useful if you have a server (perhaps on one of the best cheap web hosts) or Raspberry Pi you need to control remotely. As these systems primarily use Linux, it’s easy to do things like edit scripts, change key configuration files or even install software remotely.
In this how to, we will cover how to create basic SSH connections coming from Windows, macOS and Linux machines.
Before You Open SSH: Credentials and Access
To initiate an SSH connection coming from any operating system, you first need proper access to the remote device and you need its address, in the form of either an IP address or a valid hostname. You will need to enter the address to establish your connection and then you will be hit with a request for a username and password.
If you have a web hosting account with SSH enabled, you will likely be able to use the domain name as the address and then the same username and password you employ for reaching FTP or cPanel to log in. If those don’t work, check the instructions on the host’s site. If you are logging into a server on your local network, you will need the computer name / IP address and to make sure that the remote device has an account for you on it.
Using SSH in Windows
Though you can invoke SSH from the command line, most folks much prefer to use PuTTY, a free GUI app that makes connecting easy.
1. Download and install PuTTY. Windows does have its own command prompt, which can be used with SSH, but PuTTY is by far the most accessible means to open an SSH connection.
2. Open PuTTY. PuTTY’s user interface has a Host Name / IP address section which is where we would type in the details of the machine that we wish to SSH into.
3. Enter the appropriate address into the Hostname or IP address field and click Open.
4. Answer Yes if prompted to confirm your connection. If this is the first time connecting to that system, you will be asked.
You will now be connected to the remote system.
5. Enter your username and password as prompted. The password is not echoed to the screen, this is normal behavior and is a security feature.
You are now in control of the remote machine, any commands will be executed using the remote machine.
6. When finished with the SSH session, type exit and press Enter or simply close the window. It is important to close an SSH session when not in use so you don’t accidentally send commands to it or leave it open should your computer be hacked.
Using SSH in Linux
In all major versions of Linux, SSH is built into the command line and easy to access that way. Note that we used Ubuntu for this tutorial but these instructions would work with almost any Linux flavor.
1. Open the terminal if you’re not already at the command line. Hitting CTRL + ALT + T does this or you can go to the apps menu and search for “terminal” to find its app.
2. Use SSH to log in to the remote machine using your username, and the IP address / hostname. For example our user was “pi” and the hostname was “smartipi,” this is what we’d use:
ssh pi@smartipi.local
3. Type yes if you are given a warning that the SSH connection’s key is not known. This may happen on first connection and you may need to reconnect after entering it.
4. Enter your password if prompted. You won’t see the characters as you type them.
5. Press CTRL + D or type “logout” to finish your session.
Using SSH in macOS
1. Launch the terminal by clicking the terminal icon in the dock.
The terminal is ready for use.
2. Enter the SSH command followed by username@server. In the example below, our user was “pi” and the hostname was “raspberrypi.local”.
ssh pi@raspberrypi.local
3. Type Yes if prompted to confirm the connection for the first time.
4. Enter your password when prompted.
5. When finished with the SSH session, exit using CTRL+D or type exit and press Enter. It is important to close an SSH session when not in use so you don’t accidentally send commands to it or leave it open should your computer be hacked.
How to run a command and close an SSH connection
Sometimes we just need to run a single command on a remote machine. For example this could be a Raspberry Pi that we need to shutdown. Using the -t argument we can log in, run a command, wait for the output and then logout.
1. Open a terminal / command prompt.
2. Use SSH to log in to the remote machine, add “-t” to the end of the command, then insert the command that you wish to run and press Enter. In our example we will login to a Raspberry Pi then run the poweroff command to power down the Pi.
ssh pi@raspberrypi.local -t "sudo poweroff"
3. Enter a password as needed
The remote computer should now log your off and then perform the task.
Get instant access to breaking news, in-depth reviews and helpful tips.