gpt4 book ai didi

scp - 使用 Colab 将文件发送到主机

转载 作者:行者123 更新时间:2023-12-04 11:46:01 24 4
gpt4 key购买 nike

我想使用 scp 共享 Colab 文件
我使用 SSH-keygen 创建了一个 RSA key 对。当我运行时:

!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 模块:面条永远,没有任何结果

最佳答案

您首先需要通过可以通过 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

关于scp - 使用 Colab 将文件发送到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com