Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add code snippets for obtaining playground's URL and for scanning open ports (#100)

Vasek Mlejnsky9fb7fa1b1113
diff --git a/apps/docs/src/app/getting-started/basics/page.mdx b/apps/docs/src/app/getting-started/basics/page.mdx
index 810e12b4b..8bfa6bb83 100644
--- a/apps/docs/src/app/getting-started/basics/page.mdx
+++ b/apps/docs/src/app/getting-started/basics/page.mdx
@@ -27,6 +27,34 @@ There are two main primitives in our SDK that you use to interact with the cloud
```
</CodeGroup>
+## Get playground URL
+Each playground is accessible from the internet via a unique URL of that playground.
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/basics/get_url.py' }}
+```
+```js {{ language: 'js', snippet: 'js/basics/get_url.js' }}
+```
+</CodeGroup>
+
+### Get URL for a specific port inside playground
+Below is an example showing how to get a URL for a port 3000. If there's any server inside the playground listening on port 3000 this would be its URL.
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/basics/get_url_port.py' }}
+```
+```js {{ language: 'js', snippet: 'js/basics/get_url_port.js' }}
+```
+</CodeGroup>
+
+## Scan open ports inside playground
+The following example continuosly scan open ports inside the playground and prints new ones to the console.
+
+<CodeGroup>
+```python {{ language: 'python', snippet: 'python/basics/scan_ports.py' }}
+```
+```js {{ language: 'js', snippet: 'js/basics/scan_ports.js' }}
+```
+</CodeGroup>
+
## Filesystem
Here are the basic operations you can do with the filesystem inside a cloud playground:
diff --git a/apps/docs/src/code/js/basics/get_url.js b/apps/docs/src/code/js/basics/get_url.js
new file mode 100644
index 000000000..fd41b7587
--- /dev/null
+++ b/apps/docs/src/code/js/basics/get_url.js
@@ -0,0 +1,11 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const url = session.getHostname()
+console.log('https://' + url)
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/get_url_port.js b/apps/docs/src/code/js/basics/get_url_port.js
new file mode 100644
index 000000000..500b56771
--- /dev/null
+++ b/apps/docs/src/code/js/basics/get_url_port.js
@@ -0,0 +1,11 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const url = session.getHostname(3000)
+console.log('https://' + url)
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/scan_ports.js b/apps/docs/src/code/js/basics/scan_ports.js
new file mode 100644
index 000000000..26596e87e
--- /dev/null
+++ b/apps/docs/src/code/js/basics/scan_ports.js
@@ -0,0 +1,38 @@
+import { Session } from '@e2b/sdk'
+
+const sleep = ms => new Promise(res => setTimeout(res, ms))
+
+// List of ports that have already been printed
+const printedPorts = []
+
+// The logs will look similar to this:
+// ip='0.0.0.0' port=49982 state='LISTEN' https://49982-smob7j0j-fce131d5.ondevbook.com
+// ip='0.0.0.0' port=22 state='LISTEN' https://22-smob7j0j-fce131d5.ondevbook.com
+// ip='169.254.0.21' port=49982 state='ESTABLISHED' https://49982-smob7j0j-fce131d5.ondevbook.com
+// ip='127.0.0.53' port=53 state='LISTEN' https://53-smob7j0j-fce131d5.ondevbook.com
+// ip='0.0.0.0' port=8000 state='LISTEN' https://8000-smob7j0j-fce131d5.ondevbook.com
+function printNewPortAndURL(openPorts, session) {
+ openPorts.forEach(port => {
+ if (!printedPorts.includes(port.port)) {
+ printedPorts.push(port.port)
+ console.log(port, `https://${session.getHostname(port.port)}`)
+ }
+ })
+}
+
+const session = await Session.create({
+ id: 'Python3',
+ apiKey: process.env.E2B_API_KEY,
+ onScanPorts: openPorts => printNewPortAndURL(openPorts, session)
+})
+
+// Start a new server on port 8000 inside the playground.
+const proc = await session.process.start({ cmd: 'python3 -m http.server 8000' })
+
+
+// Wait 10 seconds and then kill the server and close the session.
+await sleep(10000)
+await proc.kill()
+await session.close()
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/run_all.js b/apps/docs/src/code/js/run_all.js
index 7f6bcd1fc..0c9c18495 100644
--- a/apps/docs/src/code/js/run_all.js
+++ b/apps/docs/src/code/js/run_all.js
@@ -1,4 +1,7 @@
import * as quickstart from './quickstart.js'
+import * as get_url from './basics/get_url.js'
+import * as get_url_port from './basics/get_url_port.js'
+import * as scan_ports from './basics/scan_ports.js'
import * as init from './basics/init.js'
import * as fs_ls from './basics/fs_ls.js'
import * as fs_mkdir from './basics/fs_mkdir.js'
diff --git a/apps/docs/src/code/python/_run_all.py b/apps/docs/src/code/python/_run_all.py
index ae633db68..94f2cb50a 100644
--- a/apps/docs/src/code/python/_run_all.py
+++ b/apps/docs/src/code/python/_run_all.py
@@ -1,5 +1,8 @@
from quickstart import main as quickstart_main
from basics.init import main as init_main
+from basics.get_url import main as get_url_main
+from basics.get_url_port import main as get_url_port_main
+from basics.scan_ports import main as scan_ports_main
from basics.fs_ls import main as fs_ls_main
from basics.fs_mkdir import main as fs_mkdir_main
from basics.fs_read import main as fs_read_main
diff --git a/apps/docs/src/code/python/basics/get_url.py b/apps/docs/src/code/python/basics/get_url.py
new file mode 100644
index 000000000..1e1377839
--- /dev/null
+++ b/apps/docs/src/code/python/basics/get_url.py
@@ -0,0 +1,13 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+ url = session.get_hostname()
+ print("https://" + url)
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/code/python/basics/get_url_port.py b/apps/docs/src/code/python/basics/get_url_port.py
new file mode 100644
index 000000000..276545a50
--- /dev/null
+++ b/apps/docs/src/code/python/basics/get_url_port.py
@@ -0,0 +1,15 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+async def main():
+ session = await Session.create(id="Nodejs", api_key=E2B_API_KEY)
+
+ url = session.get_hostname(3000)
+ print("https://" + url)
+
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file
diff --git a/apps/docs/src/code/python/basics/scan_ports.py b/apps/docs/src/code/python/basics/scan_ports.py
new file mode 100644
index 000000000..b6ea6f2d9
--- /dev/null
+++ b/apps/docs/src/code/python/basics/scan_ports.py
@@ -0,0 +1,40 @@
+import asyncio
+from os import getenv
+from e2b import Session
+
+E2B_API_KEY = getenv("E2B_API_KEY")
+
+# List of ports that have already been printed
+printed_ports = []
+
+# The logs will look similar to this:
+# ip='0.0.0.0' port=49982 state='LISTEN' https://49982-smob7j0j-fce131d5.ondevbook.com
+# ip='0.0.0.0' port=22 state='LISTEN' https://22-smob7j0j-fce131d5.ondevbook.com
+# ip='169.254.0.21' port=49982 state='ESTABLISHED' https://49982-smob7j0j-fce131d5.ondevbook.com
+# ip='127.0.0.53' port=53 state='LISTEN' https://53-smob7j0j-fce131d5.ondevbook.com
+# ip='0.0.0.0' port=8000 state='LISTEN' https://8000-smob7j0j-fce131d5.ondevbook.com
+def print_new_port_and_url(open_ports, session):
+ for port in open_ports:
+ if port not in printed_ports:
+ printed_ports.append(port)
+
+ host = session.get_hostname(port.port)
+ port_url = f"https://{host}"
+ print(port, port_url)
+
+async def main():
+ session = await Session.create(
+ id="Python3",
+ api_key=E2B_API_KEY,
+ on_scan_ports=lambda open_ports: print_new_port_and_url(open_ports, session)
+ )
+
+ # Start a new server on port 8000 inside the playground.
+ proc = await session.process.start("python3 -m http.server 8000")
+
+ # Wait 10 seconds and then kill the server and close the session.
+ await asyncio.sleep(10)
+ await proc.kill()
+ await session.close()
+
+asyncio.run(main())
\ No newline at end of file