Mini Shell

Direktori : /proc/self/root/opt/sharedrads/
Upload File :
Current File : //proc/self/root/opt/sharedrads/pastebin

#!/opt/imh-python/bin/python3

from getpass import GetPassWarning
import urllib.request
import urllib.parse
import urllib.error
import sys
import platform
import time
import re
import os
import termios
import warnings

PASTE_URL = "https://pastebin.imhadmin.net/"


def _raw_input(prompt="", stream=None, input_io=None):
    # A raw_input() replacement that doesn't save the string in the
    # GNU readline history.
    if not stream:
        stream = sys.stderr
    if not input_io:
        input_io = sys.stdin
    prompt = str(prompt)
    if prompt:
        stream.write(prompt)
        stream.flush()
    # NOTE: The Python C API calls flockfile() (and unlock) during readline.
    line = input_io.readline()
    if not line:
        raise EOFError
    if line[-1] == '\n':
        line = line[:-1]
    return line


def fallback_getpass(prompt='Password: ', stream=None):
    warnings.warn(
        "Can not control echo on the terminal.", GetPassWarning, stacklevel=2
    )
    if not stream:
        stream = sys.stderr
        print("Warning: Password input may be echoed.", file=stream)
    return _raw_input(prompt, stream)


def getpass(prompt="Password: ", stream=None):
    fd = None
    tty = None
    try:
        # Always try reading and writing directly on the tty first.
        fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
        tty = os.fdopen(fd, 'w+', 1)
        input_io = tty
        if not stream:
            stream = tty
    except OSError:
        # If that fails, see if stdin can be controlled.
        try:
            fd = sys.stdin.fileno()
        except (AttributeError, ValueError):
            passwd = fallback_getpass(prompt, stream)
        input_io = sys.stdin
        if not stream:
            stream = sys.stderr

    if fd is not None:
        passwd = None
        try:
            old = termios.tcgetattr(fd)  # a copy to save
            new = old[:]
            new[3] &= ~termios.ECHO  # 3 == 'lflags'
            tcsetattr_flags = termios.TCSAFLUSH
            if hasattr(termios, 'TCSASOFT'):
                tcsetattr_flags |= termios.TCSASOFT
            try:
                termios.tcsetattr(fd, tcsetattr_flags, new)
                passwd = _raw_input(prompt, stream, input_io=input_io)
            finally:
                termios.tcsetattr(fd, tcsetattr_flags, old)
                stream.flush()  # issue7208
        except termios.error:
            if passwd is not None:
                # _raw_input succeeded.  The final tcsetattr failed.  Reraise
                # instead of leaving the terminal in an unknown state.
                raise
            # We can't control the tty or stdin.  Give up and use normal IO.
            # fallback_getpass() raises an appropriate warning.
            del input_io, tty  # clean up unused file objects before blocking
            passwd = fallback_getpass(prompt, stream)

    stream.write('\n')
    return passwd


def getuser():
    """
    Obtain user from utmp trail, otherwise prompt
    """
    username = os.getlogin()
    if username == 'root':
        print(
            "Unable to obtain user from utmp.",
            "Please enter your FreeIPA username.",
        )
        username = getpass(prompt="Username: ")
    return username


def main():
    request = urllib.request.Request(PASTE_URL)
    user = getuser()
    password = getpass()
    password_manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, PASTE_URL, user, password)
    auth_manager = urllib.request.HTTPBasicAuthHandler(password_manager)
    opener = urllib.request.build_opener(auth_manager)
    urllib.request.install_opener(opener)
    values = {
        'name': platform.node(),
        'title': "Paste at %s" % time.strftime("%Y-%m-%d %H:%M:%S %Z"),
        'lang': 'text',
        'code': sys.stdin.read(),
        'expire': 0,
        'submit': 'submit',
    }
    data = urllib.parse.urlencode(values)
    try:
        with urllib.request.urlopen(request, data) as handler:
            response = handler.read()
    except urllib.error.HTTPError:
        sys.exit("Could not post to pastebin. Wrong password?")
    urlmatcher = re.compile(r"""URL\s+<\/span><a\s+href="([\w:\.\/]+)">""")
    result_url = urlmatcher.search(response)
    if result_url:
        print("Your paste is at %s" % result_url.group(1))
    else:
        print("Could not parse the response for your paste.  Sorry.")


if __name__ == '__main__':
    main()

Zerion Mini Shell 1.0