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/* .; }

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

These Might Interest You

  • I usually have 5 or more ssh connections to various servers, and putting this command in my .bash_profile file makes my putty window or x terminal window title change to this easily recognizable and descriptive text. Includes the username, group, server hostname, where I am connecting from (for SSH tunneling), which device pts, current server load, and how many processes are running. You can also use this for your PROMPT_COMMAND variable, which updates the window title to the current values each time you exec a command. I prefix running this in my .bash_profile with [[ ! -z "$SSH_TTY" ]] && which makes sure it only does this when connecting via SSH with a TTY. Here's some rougher examples from http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html # If set, the value is executed as a command prior to issuing each primary prompt. #H=$((hostname || uname -n) 2>/dev/null | sed 1q);W=$(whoami) #export PROMPT_COMMAND='echo -ne "\033]0;${W}@${H}:${PWD/#$HOME/~} ${SSH_TTY/\/dev\//} [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"`]\007"' #PROMPT_COMMAND='echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n 2>/dev/null|sed 1q` `command who -m|sed -e "s%^.* \(pts/[0-9]*\).*(\(.*\))%[\1] (\2)%g"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `command ps aux|wc -l`]\007"' #[[ -z "$SSH_TTY" ]] || export PROMPT_COMMAND #[[ -z "$SSH_TTY" ]] && [[ -f /dev/stdout ]] && SSH_TTY=/dev/stdout And here's a simple function example for setting the title: function set_window_title(){ echo -e "\033]0; ${1:-$USER@$HOST - $SHLVL} \007"; } Show Sample Output


    4
    echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n|sed 1q` `who -m|sed -e "s%^.* \(pts/[0-9]*\).*(\(.*\))%[\1] (\2)%g"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `ps aux|wc -l`]\007"
    AskApache · 2009-09-19 06:57:53 1
  • If you have lots of remote hosts sitting "behind" an ssh proxy host, then there is a special-case use of "rsynch" that allows one to easily copy directories and files across the ssh proxy host, without having to do two explicit copies: the '-e' option allows for a replacement "rsh" command. We use this option to specify an "ssh" tunnel command, with the '-A' option that causes authentication agent requests to be forwarded back to the local host. If you have ssh set up correctly, the above command can be done without any passwords being entered.


    5
    rsync -avz -e 'ssh -A sshproxy ssh' srcdir remhost:dest/path/
    totoro · 2009-03-25 21:29:07 4
  • To truncate a file easily, just redirect something empty with no commands. Show Sample Output


    0
    > filename
    suryadavinci · 2016-11-14 10:21:27 0
  • Default output-file is "liveh.txt". This uses only BRE, in case you're using an older version of sed(1) that doesn't have support for ERE added. With a modern sed(1), to reduce false positive matches, you might do something like: liveh(){ tcpdump -lnnAs512 -i ${1-} tcp |sed 's/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/GET |Host: |POST /!d;/[\"'"'"]/d;/\.\./d;w '"${2-liveh.txt}"'' >/dev/null ;} Anyway, it's easy to clean up the output file with sed(1) later.


    5
    liveh(){ tcpdump -lnAs512 ${1-} tcp |sed ' s/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/[GPH][EOo][TSs]/!d;w '"${2-liveh.txt}"' ' >/dev/null ;} # usage: liveh [-i interface] [output-file] && firefox &
    argv · 2010-10-11 01:01:11 0

What Others Think

how about: mecp () { scp $@ ${SSH_CLIENT%% *}:Desktop/; }
rhythmx · 471 weeks and 4 days ago
Awesome! Just the improvement I was looking for :) thanks.
braino · 471 weeks and 3 days ago
cygwin produces this error. cp: cannot create regular file `::ffff:131.204.42.86:Desktop/2.25-win-1.000.pdb': No such file or directory
thebillywayne · 471 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 · 471 weeks and 3 days ago
(echo ${SSH_CLIENT%% *} | awk -F: '{print$NF}')
catawampus · 471 weeks and 1 day 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 · 470 weeks and 4 days ago
thanks for the tip. All fixed.
braino · 470 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 · 455 weeks and 4 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 · 455 weeks and 3 days ago
Is it possible to automatically attach the username from the originating host?
Dachaz · 331 weeks 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: