You are not logged in.

#1 2010-09-10 09:13:02

manzinger
Member
Registered: 2009-11-11
Posts: 26

bashscript for ssh login and file transfer

I need a bashscript to login to my server using ssh and transfer a file to it.
I don't know how to handle the password request and how to transfer files. Usually I do it manually with MC but I want a faster way as I backup some files on a regular routine.
I use ssh with a keyfile+password, I log in with "ssh name@server" and then type the password.
Thanks for help.

Offline

#2 2010-09-10 09:26:05

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: bashscript for ssh login and file transfer

Google for automate ssh connections. Transfering files should be as simple as cat file | ssh 'cat > file.bck'. Personally, I would use sftp for file transfers.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2010-09-10 11:12:26

linux-ka
Member
From: ADL
Registered: 2010-05-07
Posts: 232

Re: bashscript for ssh login and file transfer

man ssh....you might use a keyfile via ssh -i /path/to/keyfile to gain access without entering a password.  the key can be created with ssh-keygen, followed by a ssh-copy-id to place the counterpart on the remote machine.

you will find a detailed instruction when asking google for answer.

Offline

#4 2010-09-10 11:29:14

Wintervenom
Member
Registered: 2008-08-20
Posts: 1,011

Re: bashscript for ssh login and file transfer

synchronize () {
  if [ -z "$1" ]; then
    echo 'Synchronize *to* or *from* what *host*, and which *files*?'
    return 1
  fi
  if [ "$1" != 'from' -a "$1" != 'to' ]; then
    echo "I do not know how to synchronize '$1' a host; only 'to' or 'from.'"
    return 1
  fi
  direction="$1"
  shift
  if [ -z "$1" ]; then
    echo "Synchronize ${direction} what host?'"
    return 1
  fi
  host="$1:"
  shift
  sync='rsync -e ssh -axlz --delete --progress'
  while [ "$1" ]; do
    case "$1" in
      'aliases')
        item="${HOME}/.config/bash/alias"
      ;;
      'chrome'|'chromium')
        item="${HOME}/.config/chromium"
      ;;
      'home')
        for item in $HOME/{Music,Pictures,Videos,Documents,bin,.Evil,.config/bash/alias,.mozilla,.config/chromium}; do
          $sync "${item%/}" ${host}"$(dirname "${item/${HOME/\//\\\/}\//}")"
        done
        break
      ;;
      *)
        item="$1"
      ;;
    esac
    if [ "${direction}" = 'to' ]; then
      $sync "${item%/}" ${host}"$(dirname "${item/${HOME/\//\\\/}\//}")"
    else
      $sync ${host}"${item/${HOME/\//\\\/}\//}" "$(dirname "${item%/}")"
    fi
    shift
  done
}
teach () { synchronize to $*; }
learn () { synchronize from $*; }

Last edited by Wintervenom (2010-09-10 11:29:36)

Offline

#5 2010-09-10 12:31:22

manzinger
Member
Registered: 2009-11-11
Posts: 26

Re: bashscript for ssh login and file transfer

thanks for your answers.

I already use a keyfile, but I use a keyfile with passphrase for additional security. Scripting without entering the passphrase would be easy, but that is not the case.

@wintervenom
thanks, but how do I use this script or what do I have to edit to make it work for me. I'm a bash noob.

Offline

#6 2010-09-10 12:57:42

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: bashscript for ssh login and file transfer

**NOT TESTED**

while [ -z $PASSWORD ]; do
    echo -n 'password: '
   read PASSWORD
done
sshpass -p $PASSWORD ssh....etc.

sshpass is here http://aur.archlinux.org/packages.php?ID=23370
I've never used it so cannot vouch for it. You may want to search to see if there are issues with it.

read and while can be found in man bash.
If it doesn't work, hope it gets you on the right track. smile


EDIT: wintervenom's script uses rsync to backup/copy/etc. files to the server. You could go that way (or sftp as said above) instead of trying to force ssh. They can be just as secure.

Last edited by skanky (2010-09-10 12:59:19)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#7 2010-09-10 13:22:20

Ogion
Member
From: Germany
Registered: 2007-12-11
Posts: 367

Re: bashscript for ssh login and file transfer

For this kind of thing, automating interactive cli applications, you can use expect. It's in extra.
It's long ago that i used it but i remember that you can do things like "expec 'password: ' put 'passw'" etc

Ogion


(my-dotfiles)
"People willing to trade their freedom for temporary security deserve neither and will lose both." - Benjamin Franklin
"Enlightenment is man's leaving his self-caused immaturity." - Immanuel Kant

Offline

#8 2010-09-10 16:11:09

stefanwilkens
Member
From: Enschede, the Netherlands
Registered: 2008-12-10
Posts: 624

Re: bashscript for ssh login and file transfer

can I suggest using rsync over ssh to transfer files?

I've setup something similar to transfer files between myself and a friend.

#!/bin/bash
# should be in $HOME/.gnome2/nautilus-scripts
# should be executable
dirpath="`echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | sed 's/file:\/\///'`"
xterm -hold -e rsync -vapog -e "ssh -l <username> -p <port>" --partial --bwlimit=80 --stats --progress  $dirpath <hostname>:<remote_path>

This transfers the file or folder you have selected in nautilus through rsync over ssh. I chose to run it in a terminal window because I'd like to see the progress smile


Arch i686 on Phenom X4 | GTX760

Offline

#9 2010-09-10 17:03:28

yejun
Member
Registered: 2009-10-21
Posts: 66

Re: bashscript for ssh login and file transfer

Just remove pass phrase from your key.

Offline

#10 2010-09-10 17:09:46

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: bashscript for ssh login and file transfer

yejun wrote:

Just remove pass phrase from your key.

Not a good idea -- use ssh-agent to keep your keys in memory. You just have to type the key once when you startup your computer, and it's way more secure than using no passphrase with your key.

Scott

Offline

#11 2010-09-11 01:54:16

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: bashscript for ssh login and file transfer

manzinger wrote:

I need a bashscript to login to my server using ssh and transfer a file to it.
I don't know how to handle the password request and how to transfer files. Usually I do it manually with MC but I want a faster way as I backup some files on a regular routine.
I use ssh with a keyfile+password, I log in with "ssh name@server" and then type the password.
Thanks for help.

Look into scp + plus passwordless ssh (its backend).

create password less key with something like

ssh-keygen -t rsa

and hit enter for default as well as the password. Transfer the *.pub it produces onto your server's username as ~/.ssh/authorized_keys
From there on out ssh, scp, and rsync connections will go through without prompting for a password.

Offline

Board footer

Powered by FluxBB