Last active
April 20, 2019 06:17
-
-
Save gyoza/112e1625b3c927d7ceaf3d502bfb11ef to your computer and use it in GitHub Desktop.
iterm2 Build 3.3.0beta2 python-api - 'harvest ip out of screen selection, create new window, and sessions then ssh'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.7 | |
import iterm2 | |
import re | |
import os | |
import socket | |
import pprint | |
import asyncio | |
def sshToProccesTextRedux(lines): | |
regex_ip = [] | |
verified_ip = [] | |
pattern = r"(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})){3}" | |
foundip = re.findall(pattern, lines) | |
for addr in foundip: | |
addr = addr.strip() | |
try: | |
socket.inet_aton(addr) | |
if addr not in verified_ip: | |
verified_ip.append(addr) | |
except socket.error: | |
pass | |
return verified_ip | |
async def sshToSelection(connection): | |
sshToSelectionApp = await iterm2.async_get_app(connection) | |
@iterm2.RPC | |
async def sshToSelectionOnClick(session_id): | |
colors = "<style>body { background-color: black; color: pink</style>" | |
bad_selection = f'<pre>{colors}Unable to find any IP in selected text: <br> <REPL> </pre>' | |
session = sshToSelectionApp.get_session_by_id(session_id) | |
selection = await session.async_get_selection() | |
selectedText = await session.async_get_selection_text(selection) | |
good_ip = sshToProccesTextRedux(selectedText) | |
# check for ips and then create windows for good ip. | |
if not good_ip: | |
await sshToComponent.async_open_popover(session_id, bad_selection.replace('<REPL>', selectedText), iterm2.util.Size(1, 1)) | |
else: | |
newwin = await iterm2.Window.async_create(connection) | |
anchor = sshToSelectionApp.current_terminal_window.current_tab.current_session | |
count = 0 | |
for addr in good_ip: | |
if anchor: | |
dict_commands = { | |
'0' : 'source ~/.custombashrc/random.rc.sh', | |
'1' : 'source ~/.custombashrc/shared.rc.sh', | |
'2' : f'set_title {addr}', | |
'3' : 'clear', | |
'4' : "read -p PRESS_ENTER_WHEN_READY_TO_CONNECT", | |
'5' : f'issh {addr}' | |
} | |
if count == 0: | |
session_id = anchor.session_id | |
else: | |
this_session = await anchor.async_split_pane(vertical=False) | |
session_id = this_session.session_id | |
term_session = sshToSelectionApp.get_session_by_id(session_id) | |
shellscript = f'/tmp/cmd_{addr}.txt' | |
try: | |
os.remove(shellscript) | |
except Exception as e: | |
pass | |
for index, command in dict_commands.items(): | |
command = f'{command}\n' | |
print(f'{command}', file=open(shellscript, 'a')) | |
# await term_session.async_send_text(command, suppress_broadcast=True) | |
await term_session.async_send_text(f'bash {shellscript}\n', suppress_broadcast=True) | |
count += 1 | |
# create status bar 'icons' | |
sshTovl = "list_to_ssh" | |
sshToKnobs = [iterm2.CheckboxKnob("List to SSH", False, sshTovl)] | |
sshToComponent = iterm2.StatusBarComponent( | |
short_description="List to SSH", | |
detailed_description="finds IPV4 address in selection and opens terminal to each one.", | |
knobs=sshToKnobs, | |
exemplar="🧲", | |
update_cadence=None, | |
identifier="com.iterm2.list-to-ssh") | |
@iterm2.StatusBarRPC | |
async def sshToStatusBarRPC(knobs): | |
return ["🧲"] | |
await sshToComponent.async_register(connection, sshToStatusBarRPC, onclick=sshToSelectionOnClick) | |
iterm2.run_forever(sshToSelection) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment