How can i input password from bash script? - linux

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"

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.

Shell script remotely

I have one script running on server and doing some job on other server
I have many scp commands and ssh commands, this is why each time I have to enter the remote server password at each remote command.
is there any way to establish ssh connection between the servers so I type the remote password only once?
thanks
I would suggest to setup an ssh config together with ssh keys. In a nutshell the config will hold an alias for one or more remote servers.
ssh remote_server1
ssh remote server2
While your config file will look something like this:
Host remote_server1
Hostname 192.168.1.12
user elmo
IdentityFile ~/.ssh/keys/remote.key
...
If an ssh config file is not for you (although I can highly recommend it), you can use sshpass as well.
sshpass -p 't#uyM59bQ' ssh username#server.example.com
Do note that the above does expose your password. If someone else has access to your account, the history command will show the code snippet above.

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'

How to make key based ssh user?

I am new to Ubuntu-Linux,i have to create a ssh user in remote system and generate its key. and access this system by key_file through the command.
ssh -i key_file user#host
Can any body tell me how can i do ?
On the system you are trying to connect to, the public key (usually id_rsa.pub or something similar) needs to be added to the authorized_keys file.
If the user is brand new and the authorized_keys file doesn't exist yet, this command will create it for you.
cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
Next just make sure sshd is running on the host and you should be able to connect with the command you posted.
on remote-server-
ssh-keygen
ssh-copy-id user#host
cd .ssh
make a copy of the file id_rsa and give any body who want to access this server/system.
on the other system
ssh -i id_rsa user#host
If you want to connect to another host as user "user", what you need is the public key of the user that is going to open that connection, i.e. the user you are logged in on your desktop computer or some server you are coming from, not for the user, you are logging in to on the remote host.
You can check, if the keys for your current user are already created in $HOME/.ssh; there you should find something like "id_rsa" and "id_rsa.pub" (for rsa keys). If they don't exist, you create them by calling
ssh-keygen -t rsa
The public key that is generated that way, id_rsa.pub in this example, has to be put in a file ${HOME of user on remote host}/.ssh/authorized_keys on the target host.
If this file does not exist on the remote host or if even .ssh does not exist, you have to create those files with the following permissions:
.ssh 700
.ssh/authorized_keys 600
See http://www.openssh.com/faq.html#3.14 for details.
A detailed description of the process can be found here:
https://help.github.com/articles/generating-ssh-keys/

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