【问题标题】:Auto configure Jupyter password from command line从命令行自动配置 Jupyter 密码
【发布时间】:2018-04-16 00:21:26
【问题描述】:

我了解到 Jupyter-notebook 可以配置密码而不是令牌。

两步-

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

我想自动化这个过程(在Dockerfile中,当提示输入密码时我将无法手动输入),像这样,

echo 'password' | jupyter notebook password

当提示输入密码验证密码时,这应该会自动输入我的'password'到shell

你能给我一个shell命令,它可以自动设置密码而无需用户干预。

【问题讨论】:

  • 通过echo 发送密码是非常不安全的。这样暴露密码可以吗
  • 是的。在这种情况下,没关系。
  • 那么你的尝试没有成功吗? echo 'password' | jupyter notebook password?您到底在寻找什么?
  • 不。当我运行这个命令jupyter notebook password 时,它会提示输入密码,一旦输入它就会要求验证它。我想要一个 shell 脚本来自动输入这个命令标准输入的密码。
  • 试试expect实用程序

标签: linux bash shell ubuntu command-line


【解决方案1】:

您可以提供 token 参数作为 Docker 中 jupyter 密码的解决方法:

sudo docker run -i -t -p 8888:8888 -v /home/YOUR_USER:/home/ -e USERID=1003 continuumio/anaconda3 /opt/conda/bin/jupyter lab --ip='*' --port=8888 --no-browser --NotebookApp.token='YOUR_PASSWORD' --allow-root --notebook-dir=/home/

然后,输入您的地址:8888(不带令牌),并在出现提示时输入您的令牌而不是密码。

不要忘记检查您的 USERID(使用终端中的 id 命令),以确保您能够与容器外的文件夹同步读写。

【讨论】:

  • NotebookApp.token= 只会强制将 YOUR_PASSWORD 转换为十六进制
  • 你是对的,这就是为什么我提到这是一种解决方法
【解决方案2】:

1。使用passwd函数生成哈希

在控制台中手动为 your_password 生成哈希:

In [1]: from notebook.auth import passwd; passwd()
Enter password: *************
Verify password: *************
Out[1]: 'sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

或在脚本中:

$ python3 -c "from notebook.auth import passwd; print(passwd('your_password'))"
sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea

2。使用 NotebookApp.password param 运行 jupyter

何时开始:

jupyter notebook --NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

或通过 jupyter 配置,例如在 Dockerfile 中如 this answer:

RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'">>/root/.jupyter/jupyter_notebook_config.py

【讨论】:

  • 小记:在jupyter实验室,命令是python3 -c "from IPython.lib.security import passwd; print(passwd(passphrase='your_password', algorithm='sha1'))"
【解决方案3】:

我设法用this commit 解决了这个问题。我创建了a custom python file,它重用了笔记本 passwd() 函数的一部分,然后将其存储在 ~/.jupyter/jupyter_notebook_config.py 文件中。就我而言,我使用的是 docker-compose,因此传递要保存的密码有点复杂,但您可以使用 Docker 轻松地将其作为环境文件传递:

# -*- encoding: utf-8 -*-

import os
import fileinput
import hashlib
import random
from ipython_genutils.py3compat import cast_bytes, str_to_bytes

# Get the password from the environment
password_environment_variable = os.environ.get('JUPYTER_PASSWORD')

# Hash the password, this is taken from https://github.com/jupyter/notebook/blob/master/notebook/auth/security.py
salt_len = 12
algorithm = 'sha1'
h = hashlib.new(algorithm)
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
h.update(cast_bytes(password_environment_variable, 'utf-8') + str_to_bytes(salt, 'ascii'))
password = ':'.join((algorithm, salt, h.hexdigest()))

# Store the password in the configuration
setup_line = "#c.NotebookApp.password = ''"
new_setup_line = setup_line.replace("''", "u'" + password + "'")
new_setup_line = new_setup_line.replace("#", "")
setup_file = os.getenv("HOME") + "/.jupyter/jupyter_notebook_config.py"

for line in fileinput.input(setup_file, inplace=True):
    print line.replace(setup_line, new_setup_line),

for line in fileinput.input(setup_file, inplace=True):
    print line.replace("#c.NotebookApp.password_required = False", "c.NotebookApp.password_required = True"),

【讨论】:

【解决方案4】:

要使用 echo 从 bash 设置密码,您可以执行以下操作

jupyter notebook --NotebookApp.password="$(echo PUT_YOUR_PASSWORD_HERE | python -c 'from notebook.auth import passwd;print(passwd(input()))')"

请注意,通过回显发送密码非常不安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    相关资源
    最近更新 更多