ssh script to connect to server [duplicate] - bash

This question already has answers here:
How do you use ssh in a shell script?
(5 answers)
ssh key passwordless using bash
(3 answers)
how to SSH Login Without Password [closed]
(4 answers)
Automatically enter SSH password with script
(24 answers)
Closed 3 years ago.
I have this basic script file.sh that connects to a server
name=username
routerip=172.21.200.37
echo "$name""#""$routerip"
ssh "$name""#""$routerip"
sample output:
$ ./file.sh
username#172.21.200.37
username#172.21.200.37's password:
What I am wondering is how to best handle the password that is requested in my script. Should I use expect? Or is there another way. Also if the password is in the script should it be encrypted for security?
And maybe a silly question but, is there a way to connect to the server with haveing a username?

If you care about security, you definitely don't want to embed the password in the script. And you can't really "encrypt" the password, either, because the script would have to know how to decrypt it, and anyone reading the script would see how you did it (and what the decryption key was, etc.).
If you have a script running on machine A that is authorized to log in to machine B, the way I've always done it is to set up a private ssh key on machine A, and put the public key in the user's authorized_keys file on machine B.
(In answer to your last question, no, there's no way to do this sort of login without a username.)

I agree with #Steve Summit that, if machine B has public key of machine A then just by giving username you can login. You don't need to give password.
Use the below simple script to solve your problem
USERNAME=myUsername
GRID=$1
SERVER=$2
echo "=================== Connecting ==================="
if [ ${GRID} == 'dev' ] && [ ${SERVER} == 'uk' ]
then
ssh -l ${USERNAME} "dev-uk.xyz.com"
elif [ ${GRID} == 'dev' ] && [ ${SERVER} == 'us' ]
then
ssh -l ${USERNAME} "dev-us.xyz.com"
else
ssh -l ${USERNAME} ${GRID}"-"${SERVER}".xyz.com"
fi
So, while connecting you just need to run
./file.sh dev uk
or
./file.sh qa jer

Related

Automatic ssh script without expect and without key (with user/password) [duplicate]

This question already has answers here:
Shell script to automate SSH login using password
(2 answers)
Closed 5 years ago.
I am trying to make a script in bash that connects via ssh to another machine.
I only have user access to this machines so
I can't use expect (It is not installed and I can't install it)
I can't install ssh keys
So I have to log in via username and password.
Is there a way to make my script send my password just using bash?
You can do that with sshpass, however, it's insecure.
sshpass -p YOUR_PW ssh ...
That will connect without asking for password.
You might need to add the flag -oStrictHostKeyChecking=no to ssh for auto-accepting keys.

run a local script as root on remote server [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I try to run a local script on multiple remote servers as root. I don't have su to root on those but just can run root commands using sudo. So far I tried:
for host in $(cat hosts_list); do ssh -tt $host "echo mypassword | sudo bash -s" < ./myscript.sh
And in myscript.sh there is something like:
echo "test test123" >> /etc/tests
exit 0
But it looks like not working and won't change the file. What is the proper way to run this script as root and without typing password separately for each host?
Ok, then why do you "echo mypassword" ?
Can't you add your SSH account to the sudoers file with NOPASSWD ?
From man sudoers:
authenticate If set, users must authenticate themselves via a password (or other means
of authentication) before they may run commands. This default may be
overridden via the PASSWD and NOPASSWD tags. This flag is on by default.

Automatically enter a password in a bash script [duplicate]

This question already has answers here:
How to enter ssh password using bash? [duplicate]
(2 answers)
Closed 7 years ago.
I have a simple bash application in Mac OS that establishes a SSH connection.
the code is like this:
$ ssh user1#machine.com -p61023
When I run the script, console always prompts asking for a password. (It is a very rudimentary process)
appelelog#sunlineclass.com's password:
How I can automate this process?
Create a key pair.
Add the private key to the Keychain: ssh-add -K ~/.ssh/host_id_rsa.
Never type the password again, other than the login password.
If you run the server, then other things I do:
Listen on port 422 instead of 22, to reduce break-in attempts.
Only allow login via public/private key pairs, and not passwords.
Only allow login by a select list of users (never root).
This question is off-topic, however, and you should have asked it here.
#Kalanidhi gave me a hint:
Here is my new script
sshpass -p 'yourpassword' ssh user#machine.com -p61023

SFTP with Password VIA Shell Script [duplicate]

This question already has answers here:
How to run the sftp command with a password from Bash script?
(12 answers)
Closed 8 years ago.
Is it possible to pass SFTP USER/PASS to a server in an automated script that will log in and retrieve a file?
I know that KEY PAIRS are the recommended method but assume thats not possible in this case.
In the simplest case you use a key based authorization so you don't need to enter any credentials.
For doing that create a key:
ssh-keygen -t rsa
And copy it to the target system:
ssh-copy-id -i ~/.ssh/id_rsa.pub user#remote-system
Now you can login to the system without a password.
If your problem is the missing ssh-copy-id command try this here:
cat ~/.ssh/*.pub | ssh user#remote-system 'umask 077; cat >>.ssh/authorized_keys'

How to enter ssh password using bash? [duplicate]

This question already has answers here:
Bash: controlling SSH
(8 answers)
Closed 9 years ago.
Everyday I am connecting to a server through ssh. I go through this routine:
IC001:Desktop user$ ssh user#my.server.com
user#my.server.com's password:
Last login: Tue Jun 4 10:09:01 2013 from 0.0.0.0
$
I would like to automate this process and create a bash script to do it for me. I don't care about security and okay to store my password openly in the script. I am also okay for it to get typed openly on the screen while the script gets executed. So I've created this:
#!/bin/bash
ssh user#my.server.com
echo mypassword
But it doesn't work. I've also tried send instead of echo, but it also didn't work. Please advise if it is possible to do.
Double check if you are not able to use keys.
Otherwise use expect:
#!/usr/bin/expect -f
spawn ssh user#my.server.com
expect "assword:"
send "mypassword\r"
interact
Create a new keypair: (go with the defaults)
ssh-keygen
Copy the public key to the server: (password for the last time)
ssh-copy-id user#my.server.com
From now on the server should recognize your key and not ask you for the password anymore:
ssh user#my.server.com

Resources