Automate SFTP login using both key and password - linux

Recently SFTP hosted servers are enabled with two factor authentication. For details please refer the link here.
Now, we have requirement to automate this setup using command line. I found that as two different options for doing this:
sshpass -p password sftp -oBatchMode=no -oStrictHostKeyChecking=no username#server1
sftp -oIdentityFile=/path/to/private/key/file -oBatchMode=no -oStrictHostKeyChecking=no username#server1
But now I need to combine these two into single command and use it in my script for automating this sftp flow.
So, at first I need to send the private key file in the command line and then send the password in the same command line. I tried few options but it didn't work.

How hard can it be to put both lines together:
sshpass -p password sftp -oBatchMode=no -oStrictHostKeyChecking=no \
-oIdentityFile=/path/to/private/key/file username#server1
ssh itself will take care of both authentication methods.

Related

How to pass the variable to ssh command on shell

I am writing the script to ssh the server.
I have the server which require the verification code and password login, and I want the script can login the server and run some script on server every day (crontab).
I only have the access on the server and I cannot the right to setup the crontab on the server.
So i want to setup the crontab and script on my own server to do.
(If i cannot run the script on server, at least I want to login my server via one line command.)
Currnet Flow
user ~% ssh xxxx#example.com
The authenticity of host 'example.com' can't be established.
RSA key fingerprint is SHA256?[yyyyyyyyyyyyyyyyyyy].
Are you sure you want to continue connecting (yes/no/[fingerprint])? (yes)
Verification code: (input)
Password: (input)
xxxx$ ./a.sh
I write the command below
print "(verification code)\n(pwd)\n" | ssh -t -t xxxx#example.com
I think that this command will pass "the code , enter, the password" to the ssh and perform the one line command login.
However the output is
Verification code: | (cursor)
Any one can help me how to fix this (let the script auto press enter) ??
Use 'sshpass', for example:
sshpass -p <password> ssh <username>#<remote-address> ./a.sh
First, I don't have an answer if "verification code and password login" is the only authentication option setup in the server.
Since you have not mention key based authentication in your question, I would suggest investigating whether it is an option.
Use this to generate private/public keys for the machine sending the command:
ssh-keygen -t rsa
That would normally create the keys in ~/.ssh (id_rsa and id_rsa.pub).
Then, use this to authorise login on the server:
ssh-copy-id xxxx#example.com
That would add your public key (~/.ssh/id_rsa.pub) to ~/.ssh/authorized_keys in the server.

How can i input password from bash script?

I am creating a bash script that trying to connect to a remote server, but it requires to enter a password, I wrote the following script:
ssh HostIP
expect "password:"
send "password"
but it connects and gives "user#HostIP's password:", so the send command is not writing any password to the screen....what should I do to make it work?
Writing passwords in file(s) or scripts is NEVER a good practice. Why don't you give a try to password less authentication from one server to another.
Simple steps:
I- generate the RSA public and private keys from command ssh -keygen -t rsa to your server1.
II- Now create .ssh directory in your another server(server2)'s home dorectory with correct permissions.
III- Create file named authorized_keys on server2.
IV- Open file named authorized_keys on server2 and copy file named id_rsa.pub from server1 to server2.
V- Set permissions to 640 to ~/.ssh/authorized_keys now.
VI- try to login to server2 now by doing:
ssh user#server2
Here is a nice link which could tell you about same too.
https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
Once passwordless authentication is set from server1 to server2 with ssh then you could simply execute all ssh commands in your script which you want to run on another server.
You can do it with sshpass like :
sshpass -p **your_password** ssh user#HostIP
If sshpass is not already installed, you can install it and make the first connection in bash console for "the yes confirmation"

Transfer files between local to remote server using ssh without password authentication

I want to transfer some files from my local to remote, like github does it. I want to happend it very smooth like in shell script. I tried creating one shell script which automates the process of ssh authentication without password but for first time it exposes my remote server password. I dont want to do it that way. Like in git we can't see their server password. Is there any possible way that we can do ?
I used this article script to automate ssh login. http://www.techpaste.com/2013/04/shell-script-automate-ssh-key-transfer-hosts-linux/
As i mentioned, you can use the scp command, like this:
scp /local_dir/some*.xml remote_user#remote_machine:/var/www/html
This requires that you need connect to the remote machine without password, only with ssh key-authentication.
Here is a link: http://linuxproblem.org/art_9.html to help you.
The important steps: (automatic login from host A / user a to Host B / user b.)
a#A:~> ssh-keygen -t rsa
a#A:~> ssh b#B mkdir -p .ssh
a#A:~> cat .ssh/id_rsa.pub | ssh b#B 'cat >> .ssh/authorized_keys'

rsync: how to authenticate user to the remote side

I was writing a script that uses rsync. The problem that I'm facing is how to authenticate the user to the remote side. It seems that the only approaches to provide the password is to use the RSYNC_PASSWORD env var, or to use the --password-file option. What I'm think is that use something like "--password=
You need to user sshpass try following command
sshpass -p password rsync "your remaining command"
if sshpass is not installed then install it.

multiple passwords in sshpass

Is there a way to try multiple passwords when using sshpass command? I have a txt file named hosts.txt listing multiple system IPaddresses and each system uses different passwords (for example - 'mypasswd', 'newpasswd, nicepasswd'). The script reads the hosts.txt file and execute a set of commands on each system. Since I don't know which system uses which of these given passwords, i wanted to try all these set along with sshpass command and execute the script with the password that works.. Is that possible?
#!/bin/bash
while read host; do
sshpass -p 'mypasswd' ssh -o StrictHostKeyChecking=no -n root#$host 'ls;pwd;useradd test'
done < hosts.txt
Instead of trying to get password based authentication, isn't it an option to setup key based auth? You can then either add your one public key to ll systems or optionally generate different ones and use the -i keyfile option or create an entry in the ssh configuration file as below.
Host a
IdentityFile /home/user/.ssh/host-a-key

Resources