LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH - SSH Login? (https://www.linuxquestions.org/questions/programming-9/bash-ssh-login-778759/)

worm5252 12-29-2009 03:02 PM

BASH - SSH Login?
 
Hey guys, I want to write my own script to rsync my files to my file server over SSH. I do not use authorized keys, I just prefer the warm cuddly feeling of a username and password authentication. So my question is how can I create an SSH session with my file server via a BASH Script?

Here are some details:
My Desktop
IP: Static, 10.1.1.20
HOSTNAME: debian

My File Server
IP: Static, 10.1.1.21
HOSTNAME: S-FSXX01

SethsdadtheLinuxer 12-29-2009 03:10 PM

in /etc/sshd_config, change PubkeyAuthentication to no and PasswordAuthentication to yes.

worm5252 12-29-2009 03:14 PM

My SSH works fine. I can do SSH, my question is how can I initiate the SSH session automatically via a BASH script?

cpplinux 12-29-2009 03:52 PM

Do you have to use Bash script? If you use Perl, there is a SSH module you can take advantage of.

hawk__0 12-29-2009 04:14 PM

Do you mean you want to run a script, then have it ssh? That's very easy.

create a file for the script:
touch script.sh

make it executable:
chmod +x script.sh

edit it:
vi script.sh

add this to it:
ssh username@servername
rsync <command stuffs here>
exit

you could further expand it by just using the rsync command in the initial SSH command.... like so:
ssh username@servername "rsynch <commands>"

irishbitte 12-29-2009 07:15 PM

So, hawk_0, where do you put in your password?

ddreggors 12-29-2009 08:22 PM

what is wrong with straight rsync?
or scp for that matter if you want security?


Code:

#!/bin/bash

# Using the user@host method will prompt the user for a password
# and do a full secure login (ala ssh) to transfer!

# Also note the path/from/* and the path/to/ locations
# as well as the trailing slash (/) and star (*) as they
# matter when it comes to 'what' you are copying and where
# you are copying to!

# Examples using rsync and also scp

rsync -avz user@host:/path/to/rsync/from/* /path/to/rsync/to/
scp -r user@host:/path/to/rsync/from/* /path/to/rsync/to/

Both lines in the code above accomplish the same task however one is optimized for speed and the other for security.

Take your pick and you have a one-liner script that does what you need!

infact you can even adapt that to take the args on command line for portability:


Code:

#!/bin/bash

# Script Name: remote_trans.sh
# Script Version: 1.0

USER=$1
HOST=$2
RPATH=$3
LPATH=$4

scp -r ${USER}@${HOST}:${RPATH} ${LPATH}

or

Code:

#!/bin/bash

# Script Name: remote_trans.sh
# Script Version: 1.0

USER=$1
HOST=$2
RPATH=$3
LPATH=$4

rsync -avz ${USER}@${HOST}:${RPATH} ${LPATH}


You can then call the script like this:

Code:

$ remote_trans.sh someuser somehost '/repote/path/*' '/local/path'

worm5252 12-29-2009 08:28 PM

I don't have to use Bash, but I would prefer to since I am trying to improve my bash scripting skills. I could do it with expect if needed, but I would need to hard code the password.

I guess I am trying to figure out a way to initiate an SSH session using password authentication, without user interaction, have the script do whatever it is going to do, and then terminate the SSH session.

In this case I want to rsync some files to have it automate a differential flat file back up. I have the destination mounted using NFS, so in theory I could just do a copy, which is how I do it now, but I do it manually. I could script it to be automated using cp, but that just avoids me learning how to initiate the SSH session and using rsync.

worm5252 12-29-2009 08:30 PM

Quote:

Using the user@host method will prompt the user for a password
# and do a full secure login (ala ssh) to transfer!
The problem is it will request the password and I am trying to avoid that

ddreggors 12-29-2009 08:38 PM

Quote:

Originally Posted by worm5252 (Post 3808408)
The problem is it will request the password and I am trying to avoid that

Then if you do not want to use ssh keys you should look into expect, you can use it inline or in a seperate script:

Code:

#!/bin/bash

USER='someuser'
PASS='somepass'
HOST=$1
RPATH=$2
LPATH=$3

function do_remote_cmd() {
/usr/bin/expect -- << EndMark
    spawn -noecho scp -r ${USER}@${HOST}:${RPATH} ${LPATH}
    log_user 0
    expect -re ".*assword.*"
    sleep 1
    send "$PASS\r"
    log_user 1
    expect {
        "\r" {
            exp_continue
        }
    }
    exit
EndMark
}
 
do_remote_cmd
# Creating the function helps contain the expect in-line...

This should work the same way however the command line would now read:

remote_trans.sh somehost '/repote/path/*' '/local/path'

worm5252 12-29-2009 08:46 PM

I had a feeling I was going to have to use expect to do this. I have never used expect so that adds to my learning curve.

ddreggors 12-29-2009 08:56 PM

If you don't mind me asking what is your issue with ssh keys?


It has been my experience that if done properly they are a nice (secure) tool.

No one can shoulder surf your password and you can lock down the ssh server to only allow keys, this way nobody can brute force your ssh server passwords either.

On the other hand if you are not in the habit of locking your own terminal, anyone can ssh without password to the remote server as well as having access to your terminal.

GooseYArd 12-29-2009 09:06 PM

you want ssh-agent

ddreggors 12-29-2009 09:14 PM

No GooseYArd worm5252 doesn't hence my question... worm5252 does not want to use ssh keys at all (via ssh-agent or not).

My question is still (if you do not mind me asking worm5252), why not?

GooseYArd 12-29-2009 09:27 PM

no i mean, he wants it , he just doesn't know that he wants it yet. these situations are precisely why ssh-agent exists. :)


All times are GMT -5. The time now is 09:17 PM.