Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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:
- continue
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement