[Linux][Colab] Colab连接google云盘 | ssh连接Colab | 防止Colab断连

购买Colab Pro之后,界面多了终端显示:
在这里插入图片描述

1. Colab连接goole云盘

在代码命令行输入命令,实现colab和google云盘的连接:

from google.colab import drive
drive.mount('/content/drive/')

在/content下多了个/drive/MyDrive的文件夹,里面是google云盘的内容。
(colab中删除文件夹的时候,只能删除空的文件夹,操作不方便,可以在google云盘中操作。)

2.ssh连接Colab

代码命令行输入:

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}')

点击弹出来的链接:
在这里插入图片描述

需要登陆,然后在Your Authtoken中,Copy后,复制到框里面即可。
在这里插入图片描述
弹出来的用户名、密码、主机、端口号:
在这里插入图片描述
在XShell中进行连接:
在这里插入图片描述
在这里插入图片描述

3. 防止colab自动断开连接

在colab页面,按Ctrl+Shit+i

在下方Console里粘贴下述代码:

function ConnectButton(){
    console.log("Connect pushed");
    document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click()
}
setInterval(ConnectButton,60000); # 将间隔设置为60000

回车得到的数字是intervalId,例如为83,当想停止上述代码时,执行:

clearInterval(83)

参考:

  1. SSH 连接 Google colab:https://blog.csdn.net/u010986080/article/details/93505201
  2. 薅资本主义羊毛,用Google免费GPU:https://zhuanlan.zhihu.com/p/33344222
  3. SSH 连接 Google colab(转载)(免费学习用途Linux服务器):https://www.bilibili.com/read/cv6970519
  4. 防止colab自动断开连接:
    https://blog.csdn.net/bornfree5511/article/details/106980379
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值