How to pass the variable to ssh command on shell - linux

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.

Related

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"

Automate SFTP login using both key and password

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.

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'

ssh dynamically from script from any server

Ok, I have been searching for few hours and cannot seem to find the solution.
I have a file on a remote server to which one of the local users on that server has write access. I have the credentials. The requirement is:
The shell/perl script should automatically login to the server and write to that file.
The script should work from any server on the network without installing any extra packages as that will require me to sudo which will again ask for password and is therefore not possible from script.
I tried using expect but the server keeps saying spawn not found.
Please advise.
#!/bin/bash
ssh -l username hostname "password; ~/updatefile.sh params"
Doesn't work.
To use the key method, try the following:
#!/usr/bin/env ssh-agent /usr/bin/env bash
KEYFILE=`mktemp`
cat << EOF > ${KEYFILE}
-----BEGIN RSA PRIVATE KEY-----
[.......]
EOF
ssh-add ${KEYFILE}
ssh user host command
# Remove the key file.
rm -f ${KEYFILE}
To generate a key for use, refer to the following: http://www.ece.uci.edu/~chou/ssh-key.html

Enter password once in shell script of moving files linux

I am copying files to another server and I have this command:
scp -r "${inclr}" utzfin#utzfin1:"${backuppath}/${time_stamp}"
scp -r "${podout}" utzfin#utzfin1:"${backuppath}/${time_stamp}"
I keep getting a password prompt. is there a way of passing the password only once and the rest of the Commands executes without asking for password?
In this case sharing the ssh key of the target on source server or vice versa will do the needful and it will not ask for the password.
With below command you can generate the ssh key for the user and then share the id_rsa.pub key on another server.
ssh-keygen -t rsa
Command to share the key :-
ssh-copy-id -i ~/.ssh/id_rsa.pub username#hostname

Resources