Easily scp a file back to the host you're connecting from

mecp () { scp "$@" ${SSH_CLIENT%% *}:Desktop/; }
Place in .bashrc and invoke like this: "mecp /path/to/file", and it will copy the specified file(s) back to the desktop of the host you're ssh'ing in from. To easily upload a file from the host you're ssh'ing in from use this: ucp (){ scp ${SSH_CLIENT%% *}:Desktop/upload/* .; }

By: braino
2009-06-11 00:43:08

What Others Think

how about: mecp () { scp $@ ${SSH_CLIENT%% *}:Desktop/; }
rhythmx · 657 weeks and 3 days ago
Awesome! Just the improvement I was looking for :) thanks.
braino · 657 weeks and 3 days ago
cygwin produces this error. cp: cannot create regular file `::ffff:': No such file or directory
thebillywayne · 657 weeks and 3 days ago
Looks like cygwin is giving you back an ipv6 address. I think you need to enclose ipv6 addresses in brackets [ ] . Or, if "who" gives you back an ipv4 addresses, you can try replacing this "${SSH_CLIENT%% *}" with the original (ugly hack) code: `who | grep \`whoami\` | tail -n 1 | awk '{print $5}' | sed 's/\(.*\)./\1/' | sed 's/.\(.*\)/\1/'`
braino · 657 weeks and 2 days ago
(echo ${SSH_CLIENT%% *} | awk -F: '{print$NF}')
catawampus · 657 weeks ago
Very useful :) I have both a pull and a push script on half a dozen machines now. But you need to put the $@ in double quotes or you won't be able to copy files with spaces in their name scp "$@" ${SSH_CLIENT%% *}:Desktop/
meph · 656 weeks and 3 days ago
thanks for the tip. All fixed.
braino · 656 weeks and 3 days ago
I didn't find this exact syntax to work on my system. Specifically, if I echo $SSH_CLIENT I get three numbers. The first is the IP, the second perhaps the PID, and the third maybe is the port. So I do something like: SSH_CLIENT_IP="`echo $SSH_CLIENT | awk '{print $1}'`" mecp () { args=("$@"); scp -P "${args[1]}" "${args[0]}" ${SSH_CLIENT_IP}:downloads/; } ucp (){ args=("$@"); scp -P "${args[1]}" ${SSH_CLIENT_IP}:downloads/${args[0]} . ; } Since I also don't default to port 22, then I can say mecp file port, or just hardcode my port of choice.
daid · 641 weeks and 3 days ago
@daid: In order to see the IP you're logged in from, you need: echo ${SSH_CLIENT%% *} That uses bash shell expansion pattern matching (%%) to remove the longest matching pattern from the end of what is returned from $SSH_CLIENT. The " *" will match a space with anything after it.
braino · 641 weeks and 2 days ago
Is it possible to automatically attach the username from the originating host?
Dachaz · 516 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: