购买Colab Pro之后,界面多了终端显示:
在代码命令行输入命令,实现colab和google云盘的连接:
from google.colab import drive
drive.mount('/content/drive/')
在/content下多了个/drive/MyDrive的文件夹,里面是google云盘的内容。
(colab中删除文件夹的时候,只能删除空的文件夹, *** 作不方便,可以在google云盘中 *** 作。)
代码命令行输入:
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}')
点击d出来的链接:
需要登陆,然后在Your Authtoken中,Copy后,复制到框里面即可。
d出来的用户名、密码、主机、端口号:
在XShell中进行连接:
在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)
参考:
- SSH 连接 Google colab:https://blog.csdn.net/u010986080/article/details/93505201
- 薅资本主义羊毛,用Google免费GPU:https://zhuanlan.zhihu.com/p/33344222
- SSH 连接 Google colab(转载)(免费学习用途Linux服务器):https://www.bilibili.com/read/cv6970519
- 防止colab自动断开连接:
https://blog.csdn.net/bornfree5511/article/details/106980379
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)