Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #!/usr/bin/env python3.7
  2. import iterm2
  3. import re
  4. import os
  5. import socket
  6. import pprint
  7. import asyncio
  8.  
  9.  
  10. def sshToProccesTextRedux(lines):
  11. regex_ip = []
  12. verified_ip = []
  13. 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}"
  14. foundip = re.findall(pattern, lines)
  15. for addr in foundip:
  16. addr = addr.strip()
  17. try:
  18. socket.inet_aton(addr)
  19. if addr not in verified_ip:
  20. verified_ip.append(addr)
  21. except socket.error:
  22. pass
  23. return verified_ip
  24.  
  25.  
  26. async def sshToSelection(connection):
  27. sshToSelectionApp = await iterm2.async_get_app(connection)
  28.  
  29. @iterm2.RPC
  30. async def sshToSelectionOnClick(session_id):
  31. colors = "<style>body { background-color: black; color: pink</style>"
  32. bad_selection = f'<pre>{colors}Unable to find any IP in selected text: <br> <REPL> </pre>'
  33. session = sshToSelectionApp.get_session_by_id(session_id)
  34. selection = await session.async_get_selection()
  35. selectedText = await session.async_get_selection_text(selection)
  36. good_ip = sshToProccesTextRedux(selectedText)
  37. # check for ips and then create windows for good ip.
  38. if not good_ip:
  39. await sshToComponent.async_open_popover(session_id, bad_selection.replace('<REPL>', selectedText), iterm2.util.Size(1, 1))
  40. else:
  41. newwin = await iterm2.Window.async_create(connection)
  42. anchor = sshToSelectionApp.current_terminal_window.current_tab.current_session
  43. count = 0
  44. for addr in good_ip:
  45. if anchor:
  46. dict_commands = {
  47. '0' : 'source ~/.custombashrc/random.rc.sh',
  48. '1' : 'source ~/.custombashrc/shared.rc.sh',
  49. '2' : f'set_title {addr}',
  50. '3' : 'clear',
  51. '4' : "read -p PRESS_ENTER_WHEN_READY_TO_CONNECT",
  52. '5' : f'issh {addr}'
  53. }
  54. if count == 0:
  55. session_id = anchor.session_id
  56. else:
  57. this_session = await anchor.async_split_pane(vertical=False)
  58. session_id = this_session.session_id
  59. term_session = sshToSelectionApp.get_session_by_id(session_id)
  60. shellscript = f'/tmp/cmd_{addr}.txt'
  61. try:
  62. os.remove(shellscript)
  63. except Exception as e:
  64. continue
  65. for index, command in dict_commands.items():
  66. command = f'{command}\n'
  67. print(f'{command}', file=open(shellscript, 'a'))
  68. # await term_session.async_send_text(command, suppress_broadcast=True)
  69. await term_session.async_send_text(f'bash {shellscript}\n', suppress_broadcast=True)
  70. count += 1
  71.  
  72. # create status bar 'icons'
  73.  
  74. sshTovl = "list_to_ssh"
  75. sshToKnobs = [iterm2.CheckboxKnob("List to SSH", False, sshTovl)]
  76. sshToComponent = iterm2.StatusBarComponent(
  77. short_description="List to SSH",
  78. detailed_description="finds IPV4 address in selection and opens terminal to each one.",
  79. knobs=sshToKnobs,
  80. exemplar="🧲",
  81. update_cadence=None,
  82. identifier="com.iterm2.list-to-ssh")
  83.  
  84. @iterm2.StatusBarRPC
  85. async def sshToStatusBarRPC(knobs):
  86. return ["🧲"]
  87.  
  88. await sshToComponent.async_register(connection, sshToStatusBarRPC, onclick=sshToSelectionOnClick)
  89.  
  90. iterm2.run_forever(sshToSelection)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement