Trigger file transfer from a shell session

core-shell

(Yang.Y) #1

Have you ever wished you could transfer files from/ to current working directory of remote shell without having to run a separate scp or sftp command?

Core Shell can help make your wish come true. This feature requires you install a simple script to report current working directory, we will discuss it later, first let's look at how Core Shell make your daily file transfer more efficient.

Upload

You drag a local file or folder, then drop it on the shell window:

image

And an uploading task will be created immediately:

The file end up uploaded to your current working directory of the host, i.e. ~/Public in this example.

Download

Downloading is also straightforward, right-click on the file name, select Download [FILEPATH] from context menu or press key combination โ‡งโŒ˜D:

image

Then a downloading task will be created:

The download progress is synced to Finder, you can also cancel a running download from the Finder or Dock.

image

Enable Reporting Current Working Directory

When you try to upload a file for the first time (or select Shell > Enable Reporting Current Working Directory from main menu), Core Shell would requests for update $PROMPT_COMMAND on the host:

image

Core Shell has no idea about the current working directory (aka $PWD) of the shell session, but in order to create SFTP transfer tasks on your behalf, Core Shell needs to know that value. An update on $PROMPT_COMMAND environment variable can tell Core Shell the value of $PWD at each prompt.

Take the above downloading task example, bash reports the current working directory is pointed to /home/, when you try to download letsencrypt directory, Core Shell creates a SFTP task in background and push it into the queue:

sftp [OPTIONS] user@host -p PORT
> get /home/letsencrypt ~/Downloads/
> bye

You generally press Continue button then you're all set. Please continue reading if you are curious to know how it works.

How it works

After you press Continue button, Core Shell prints a command on current shell:

curl -fsSL https://raw.githubusercontent.com/codinn/core-shell-scripts/master/bashrc_Core_Shell -o ~/.bashrc_Core_Shell && . ~/.bashrc_Core_Shell && grep -q ".bashrc_Core_Shell" ~/.bash_profile || echo $'\n[ -n "$SSH_TTY" ] && [ -r "$HOME/.bashrc_Core_Shell" ] && . "$HOME/.bashrc_Core_Shell"' >> ~/.bash_profile

This command downloads a bash script bashrc_Core_Shell on your remote host, and will be loaded when you login via a SSH connection.

Is this bash script secure? The short answer is absolutely.

If you have some background in Bash scripting, you will find the bashrc_Core_Shell script does nothing special except reporting the current working directory at each prompt.

This function has been implanted by default on Apple's macOS for over a decade. In Terminal.app, the represented directory in title bar is changing with your current working directory automatically:

image
macOS achieve this by loading /etc/bashrc_Apple_Terminal when you start a shell. /etc/bashrc_Apple_Terminal goes into effect if you use Terminal.app to start a shell on macOS.

bashrc_Core_Shell uses exactly the same solution in /etc/bashrc_Apple_Terminal, except bashrc_Core_Shell goes into effect if you login via a SSH connection.

Troubleshooting

My ~/.bashrc is not executed anymore after bashrc_Core_Shell installed

If you login via a SSH connection, on some system the ~/.bashrc only be executed if ~/.bash_profile does not exists. While you were installing bashrc_Core_Shell, ~/.bash_profile was created for loading ~/.bashrc_Core_Shell with this line:

[ -n "$SSH_TTY" ] && [ -r "$HOME/.bashrc_Core_Shell" ] && . "$HOME/.bash  rc_Core_Shell"

The solution is tell the ~/.bash_profile also loads ~/.bashrc, edit ~/.bash_profile and add this line right at the very beginning:

[ -r "$HOME/.bashrc" ] && . "$HOME/.bashrc"

FAQ โ€“ Core Shell
.bashrc_Core_Shell problems
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
Core Shell 1.6 โ€“ Download / Upload
"How It Works..." button doesn't work