Command line Pastebin using curl

In this post, I will demonstrate how to use the Ubuntu Pastebin utility with cURL to make pastes from the command line.

curl -fsSL -X POST \
--url https://paste.ubuntu.com \
--output /dev/null \
--write-out "%{url_effective}\n" \
--data-urlencode "content@${PASTEBIN_CONTENT:-/dev/stdin}" \
--data "poster=${PASTEBIN_POSTER:-`whoami`@`hostname`}" \
--data "expiration=${PASTEBIN_EXPIRATION:-day}" \
--data "syntax=${PASTEBIN_SYNTAX:-text}"


You can download the script from here

By default, the pastes expire in one day. The Ubuntu pastebin utility is quite limited and offers no method for retracting a paste, so for safety, it's best to set an expiry. The options are day, week, month, year

To make the pastes permanent by default, set the expiration field as empty, i.e --data "expiration="

To test it works, I will use it to upload itself on pastebin.ubuntu.com

[rohan@desktop]$ ./pastebin.sh < pastebin.sh
https://paste.ubuntu.com/p/Wjz5HnbjJt/
[rohan@desktop]$

Annoyingly, Ubuntu doesn't provide a public link to the raw text. Here is a script to do it in shell

#! /bin/bash

# Extract content between delimiters
extract_between_tag(){
sed -n "/$1/,/$2/{ /$2/d; p }"
}

# Replace html control characters 
unescape_html(){
sed  's/&nbsp;/ /g; s/&amp;/\&/g; s/&lt;/\</g; s/&gt;/\>/g; s/&quot;/\"/g; s/#&#39;/\'"'"'/g; s/&ldquo;/\"/g; s/&rdquo;/\"/g;'
}

# Get the paste
curl -fSsL $1 | extract_between_tag  '<div class=\"paste\"><pre>' '<\/pre>' | unescape_html