DBN

iTerm Telnet Script

Word count: 203Reading time: 1 min
2022/02/14

Python script for iTerm that opens telnet sessions in new tabs.

Overview

A simple Python script that opens telnet sessions to multiple routers. This makes it faster to work with Cisco CML and associated breakout tool. It uses iTerm’s built in Python API.

The example below opens a new tab, then issues telnet localhost {i} command for all even numbered ports (9000 - 9026). This matches the default port assigned to Cisco IOSv devices.

Demonstration

run-script

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3.7

import iterm2
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.

async def main(connection):
# Your code goes here. Here's a bit of example code that adds a tab to the current window:
app = await iterm2.async_get_app(connection)
window = app.current_terminal_window
if window is not None:

for i in range(9000,9028, 2):
await window.async_create_tab()
# Get the active session in this tab
session = app.current_terminal_window.current_tab.current_session
# Send text to the session as though the user had typed it
await session.async_send_text(f'telnet localhost {i}\n')

else:
# You can view this message in the script console.
print("No current window")

iterm2.run_until_complete(main)

CATALOG
  1. 1. Overview
  2. 2. Demonstration
  3. 3. Code