You are not logged in.
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
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
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
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
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
**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.
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
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
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
Arch i686 on Phenom X4 | GTX760
Offline
Just remove pass phrase from your key.
Offline
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
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