"sshpass is not recognized" on Windows - linux

I want to run sshpass command from my Windows to remote Linux server. I use this command:
sshpass -p 'password' ssh ldap.nextstep4it.com -l root -o StrictHostKeyChecking=no
But my cmd return below error statement:
'sshpass' is not recognized as an internal or external command,
operable program or batch file.
I think this is because Windows don't have sshpass package as Linux do. From Linux I have to install sshpass package to be able to run this command.
Is there anybody know how to run sshpass command through Windows command line?

You cant run sshpass in windows.
You can however use putty via the windows command line, to achieve the same thing.
putty -load "host" -l username -pw password
Also you can upload files via command line (with a password) using WinSCP
winscp /command "option batch abort" "option confirm off" "open sftp://user:password#example.com/" "put examplefile.txt /home/user/" "exit"

Instead of OpenSSH ssh, you can use PuTTY plink. It's command line equivalent of PuTTY and has very similar command-line syntax as OpenSSH ssh. But on top of it, it has -pw switch for providing a password.
The plink equivalent of your ssh call is:
plink ldap.nextstep4it.com -l root -pw password
You absolutely should not use -o StrictHostKeyChecking=no to blindly accept all host keys. That is a security flaw. You lose a protection against MITM attacks. Instead, with plink, you can use -hostkey switch to set the fingerprint of the expected host key.
Similarly:
instead of OpenSSH scp, use PuTTY pscp;
instead of OpenSSH sftp, use PuTTY psftp.
Both have the -pw switch.
Alternatively, both for SCP and SFTP, you can use my WinSCP SFTP/SCP client. WinSCP also supports providing the password on command-line/in script. And there's a guide for converting OpenSSH sftp script to WinSCP script.
No matter, if you use OpenSSH, PuTTY or WinSCP, it is always better is to use public key authentication than the password.

Using Windows Subsystem for Linux (WSL); first go to Settings->Turn Windows Features On or Off->Enable Windows Subsystem for Linux, then download and install Ubuntu or another Linux from the Microsoft Store.
To access WSL from cmd.exe type bash or wsl, I recommend hyper for terminals because it has tabs.
sudo apt-get install sshpass
sshpass -p passxxxx ssh user#ip.address 'touch newfileonserver.txt'
To invoke from a windows environment, eg. cmd.exe or Process.Start()
bash -c "sshpass -p passxxxx ssh user#ip.address 'touch newfileonserver.txt' "

You can install QtdSync for Windows.
This will contain the sshpass.exe

Just install cygwin in your Windows, then download sshpass and compile/install it, the whole process is quick and easy, it works great in my environment.

You can rather try putty.
Download it from here
Or you can install cygwin and install sshpass, but that would be a larger task.
So, if you need a passwordless login then with putty you can use puttygen, which you can install from the same link provided above.

Related

Run command over remote ssh virtual machine and store response locally

I am calling a remote ssh through putty and hitting some commands say 'ls' now I want to be able to store its response into a text file into my local system from which I am making remote ssh call ? is this possible ? if so plz share
plink remotessh -l user -pw password
ls
You can do this but not with Putty as far as I am aware. If you can run a cygwin environment and use ssh directly you could do the following:
ssh user#host -- ls > output.txt
With plink as per #Ferrybig's comment (thanks!)
plink user#host -- ls > output.txt

"Sudo" fails with "sudo requires a tty" when executed from PuTTY command line

I'm trying to run some commands on a remote CentOS machine using PuTTY. I'm using the following command:
putty.exe -ssh [IP] -l [user] -pw [password] -m [Script]
Where [Script] is a .txt file containing the commands I want to run. The issue is that one of the commands requires sudo, and when PuTTY tries to run it I get an error:
sudo requires a tty
The thing that's confusing me is that if I start the session without giving a script, then run the commands from the script manually, it works fine. I've tried using -load instead of -ssh, and it made no difference.
I can't change the requiretty setting in my sudoers file for security reasons, which is the only solution I've been able to find. Is there another option?
The sudo requires TTY/interactive session.
On the contrary the PuTTY/Plink -m switch uses non-interactive session by default.
Use the -t switch to override that.
putty.exe -ssh [IP] -l [user] -pw [password] -t -m [Script]
Read the error: sudo requires a tty. That is, an interactive shell. You have to find an other way of doing those privileged instructions. For example, you could login as root with a key-based authentication.

Run Windows Script with several tunnels for Linux

I'm trying to create a Script in Linux with several tunnels for several servers and run a script in that servers.
Basically i've a DailyCheck.sh in 8 machines RedHat and i've a tunnel for each one in windows with:
"putty.exe user#xxx.xxx.xxx.xxx -pw <password> -L port:127.0.0.1:port"
And i open each one and run the command DailyCheck.sh.
What i want is one file in Windows with:
"putty.exe user#xxx.xxx.xxx.xxx -pw <password> -L port:127.0.0.1:port"
sudo su -
./dailyCheck.sh
<delay if necessary>
"putty.exe user#xxx.xxx.xxx.xxx -pw <password> -L port:127.0.0.1:port"
sudo su -
./dailyCheck.sh
(....)
we have any way to do this?
Thanks & Best Regards,
André.
I recommend you would - instead of tunnels - set up proxy command for each host. Then use remote command execution to run the script on remote.
Also, to get better answers, I recommend tags ssh and putty.

execute code in remote linux machine

I need to execute shell script on my remote linux machine. Do you know any tools that can help me doing that?
Thanks,
You can connect through ssh passing a command as a parameter:
ssh user#remote.ip.address "~/myscript.sh"
To connect without password, use ssh keys. To use keys, you have to generate a pair at your machine, with the command:
ssh-keygen
Then take the contents of the file ~/.ssh/id_rsa.pub (or id_dsa.pub if you use parameter -t dsa in ssh-keygen) and put in the file ~/.ssh/authorized_keys of the remote_machine. The .ssh dir must have permission 700.
For Python you can use Paramiko to run commands on the remote computer over SSH.
Passwordless remote execution without waiting for remote script to complete before terminating the ssh connection:
ssh -i ~/.ssh/id_rsa user#remote nohup sh /path/to/script.sh > /dev/null 2>&1 &
If you want to execute a local script on remote host
ssh remotePassword#remoteHost < localScript.sh
If you want to invoke a script on remote host
ssh remotePassword#remoteHost "remoteScript.sh"

How to pass password to scp?

I know it is not recommended, but is it at all possible to pass the user's password to scp?
I'd like to copy a file via scp as part of a batch job and the receiving server does, of course, need a password and, no, I cannot easily change that to key-based authentication.
Use sshpass:
sshpass -p "password" scp -r user#example.com:/some/remote/path /some/local/path
or so the password does not show in the bash history
sshpass -f "/path/to/passwordfile" scp -r user#example.com:/some/remote/path /some/local/path
The above copies contents of path from the remote host to your local.
Install :
ubuntu/debian
apt install sshpass
centos/fedora
yum install sshpass
mac w/ macports
port install sshpass
mac w/ brew
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
just generate a ssh key like:
ssh-keygen -t rsa -C "your_email#youremail.com"
copy the content of ~/.ssh/id_rsa.pub
and lastly add it to the remote machines ~/.ssh/authorized_keys
make sure remote machine have the permissions 0700 for ~./ssh folder and 0600 for ~/.ssh/authorized_keys
If you are connecting to the server from Windows, the Putty version of scp ("pscp") lets you pass the password with the -pw parameter.
This is mentioned in the documentation here.
curl can be used as a alternative to scp to copy a file and it supports a password on the commandline.
curl --insecure --user username:password -T /path/to/sourcefile sftp://desthost/path/
You can script it with a tool like expect (there are handy bindings too, like Pexpect for Python).
You can use the 'expect' script on unix/terminal
For example create 'test.exp' :
#!/usr/bin/expect
spawn scp /usr/bin/file.txt root#<ServerLocation>:/home
set pass "Your_Password"
expect {
password: {send "$pass\r"; exp_continue}
}
run the script
expect test.exp
I hope that helps.
You may use ssh-copy-id to add ssh key:
$which ssh-copy-id #check whether it exists
If exists:
ssh-copy-id "user#remote-system"
Here is an example of how you do it with expect tool:
sub copyover {
$scp = Expect->spawn("/usr/bin/scp ${srcpath}/$file $who:${destpath}/$file");
$scp->expect(30,"ssword: ") || die "Never got password prompt from $dest:$!\n";
print $scp 'password' . "\n";
$scp->expect(30,"-re",'$\s') || die "Never got prompt from parent system:$!\n";
$scp->soft_close();
return;
}
Nobody mentioned it, but Putty scp (pscp) has a -pw option for password.
Documentation can be found here: https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp
Once you set up ssh-keygen as explained above, you can do
scp -i ~/.ssh/id_rsa /local/path/to/file remote#ip.com:/path/in/remote/server/
If you want to lessen typing each time, you can modify your .bash_profile file and put
alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote#ip.com:/path/in/remote/server/
Then from your terminal do source ~/.bash_profile. Afterwards if you type remote_scp in your terminal it should run the scp command without password.
Here's a poor man's Linux/Python/Expect-like example based on this blog post: Upgrading simple shells to fully interactive
TTYs. I needed this for old machines where I can't install Expect or add modules to Python.
Code:
(
echo 'scp jmudd#mysite.com:./install.sh .'
sleep 5
echo 'scp-passwd'
sleep 5
echo 'exit'
) |
python -c 'import pty; pty.spawn("/usr/bin/bash")'
Output:
scp jmudd#mysite.com:install.sh .
bash-4.2$ scp jmudd#mysite.com:install.sh .
Password:
install.sh 100% 15KB 236.2KB/s 00:00
bash-4.2$ exit
exit
Make sure password authentication is enabled on the target server. If it runs Ubuntu, then open /etc/ssh/sshd_config on the server, find lines PasswordAuthentication=no and comment all them out (put # at the start of the line), save the file and run sudo systemctl restart ssh to apply the configuration. If there is no such line then you're done.
Add -o PreferredAuthentications="password" to your scp command, e.g.:
scp -o PreferredAuthentications="password" /path/to/file user#server:/destination/directory
make sure you have "expect" tool before, if not, do it
# apt-get install expect
create the a script file with following content. (# vi /root/scriptfile)
spawn scp /path_from/file_name user_name_here#to_host_name:/path_to
expect "password:"
send put_password_here\n;
interact
execute the script file with "expect" tool
# expect /root/scriptfile
copy files from one server to other server ( on scripts)
Install putty on ubuntu or other Linux machines. putty comes with pscp. we can copy files with pscp.
apt-get update
apt-get install putty
echo n | pscp -pw "Password#1234" -r user_name#source_server_IP:/copy_file_path/files /path_to_copy/files
For more options see pscp help.
Using SCP non interactively from Windows:
Install the community Edition of netcmdlets
Import Module
Use Send-PowerShellServerFile -AuthMode password -User MyUser -Password not-secure -Server YourServer -LocalFile C:\downloads\test.txt -RemoteFile C:\temp\test.txt for sending File with non-interactive password
In case if you observe a strict host key check error then use -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null options.
The complete example is as follows
sshpass -p "password" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root#domain-name.com:/tmp/from/psoutput /tmp/to/psoutput
You can use below steps. This works for me!
Step1-
create a normal file suppose "fileWithScpPassword" which contains the ssh password for the destination server.
Step2- use sshpaas -f followed by password file name and then normal scp command.
sshpass -f "fileWithScpPassword" scp /filePathToUpload user#ip:/destinationPath/
One easy way I do this:
Use the same scp cmd as you use with ssh keys i.e
scp -C -i <path_to opens sshkey> <'local file_path'> user#<ip_address_VM>: <'remote file_path’>
for transferring file from local to remote
but instead of providing the correct <path_to_opensshkey>, use some garbage path. Due to wrong key path you will be asked for password instead and you can simply pass the password now to get the work done!
An alternative would be add the public half of the user's key to the authorized-keys file on the target system. On the system you are initiating the transfer from, you can run an ssh-agent daemon and add the private half of the key to the agent. The batch job can then be configured to use the agent to get the private key, rather than prompting for the key's password.
This should be do-able on either a UNIX/Linux system or on Windows platform using pageant and pscp.
All the solutions mentioned above can work only if you the app installed or you should have the admin rights to install except or sshpass.
I found this very useful link to simply start the scp in Background.
$ nohup scp file_to_copy user#server:/path/to/copy/the/file > nohup.out 2>&1
https://charmyin.github.io/scp/2014/10/07/run-scp-in-background/
I found this really helpful answer here.
rsync -r -v --progress -e ssh user#remote-system:/address/to/remote/file /home/user/
Not only you can pass there the password, but also it will show the progress bar when copying. Really awesome.

Resources