【问题标题】:Sending file to host with Colab使用 Colab 将文件发送到主机
【发布时间】:2019-11-10 20:46:42
【问题描述】:

我想使用 scp 共享 Colab 文件 我使用 SSH-keygen 创建了一个 RSA 密钥对。当我跑步时:

!scp "/full/path/to/file"  [user]@[host]:~/path/to/dest

我得到(没有密码提示):

>>>Host key verification failed.
>>>lost connection

herehere 所示的经典答案在这种情况下不起作用,因为 colab 环境无法访问相关文件:

!ssh-keygen -R [host]
>>>do_known_hosts: hostkeys_foreach failed: No such file or directory

!rm /home/USERNAME/.ssh/known_hosts
>>>rm: cannot remove '/home/USERNAME/.ssh/known_hosts': No such file or directory

!scp "/full/path/to/file" -o 'StrictHostKeyChecking no' [user]@[host]:~/path/to/dest

一样

paramiko pip 模块:永远的面条,没有任何结果

【问题讨论】:

  • 我不一定要使用 scp

标签: scp google-colaboratory


【解决方案1】:

您首先需要通过可以通过 ngrok 连接的终端与您的 colab 帐户建立 ssh 连接,代码如下

import random, string, urllib.request, json, getpass

#Generate root password
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))

#Download ngrok
! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! unzip -qq -n ngrok-stable-linux-amd64.zip

#Setup sshd
! apt-get install -qq -o=Dpkg::Use-Pty=0 openssh-server pwgen > /dev/null

#Set root password
! echo root:$password | chpasswd
! mkdir -p /var/run/sshd
! echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
! echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
! echo "LD_LIBRARY_PATH=/usr/lib64-nvidia" >> /root/.bashrc
! echo "export LD_LIBRARY_PATH" >> /root/.bashrc

#Run sshd
get_ipython().system_raw('/usr/sbin/sshd -D &')

#Ask token
print("Copy authtoken from https://dashboard.ngrok.com/auth")
authtoken = getpass.getpass()

#Create tunnel
get_ipython().system_raw('./ngrok authtoken $authtoken && ./ngrok tcp 22 &')

#Get public address and print connect command
with urllib.request.urlopen('http://localhost:4040/api/tunnels') as response:
  data = json.loads(response.read().decode())
  (host, port) = data['tunnels'][0]['public_url'][6:].split(':')
  print(f'SSH command: ssh -p{port} root@{host}')

#Print root password
print(f'Root password: {password}')

通过终端连接到colab后,您可以在该终端上使用SCP

【讨论】:

  • 我找到了另一种通过终端here连接colab的方法
猜你喜欢
  • 2017-01-03
  • 2021-07-12
  • 1970-01-01
  • 2019-03-23
  • 2014-05-12
  • 2018-08-15
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多