I recently found a nice way to work on multiple servers using tmux and OpenSSH (not only, but who uses something else?).
The script looks like this:
#!/bin/sh
if [ $# -gt 0 ]; then
OK="0"
for host in $(egrep '^host' ~/.ssh/config | grep -v '\*' | pcregrep -o "\s+$1\S*?(\s|$)")
do
OK="1"
STYLE=$(pcregrep -A1 "\s$host(\s|$)" ~/.ssh/config | grep -o 'tmux-style: .*' | cut -d' ' -f2-)
tmux send-keys "ssh $host $2" \; select-pane -P "$STYLE" \; split-window \; select-layout tiled
done
if [ $OK = "1" ]; then
tmux set-window-option synchronize-panes \; send-keys ENTER
tmux kill-pane \; select-layout tiled
fi
fi
The script reads your ~/.ssh/config file and based on host entries it creates panes in a single tmux window and synchronises inputs to them. This allows for a simultaneous work done on multiple machines.
It also supports for setting styles for different hosts in your ~/.ssh/config file.
Usage:
Assuming you have a ~/.ssh/config file that looks like this:
host abc-*
User root
host abc-prod
# tmux-style: bg=red
Hostname 192.168.0.1
host abc-dr
# tmux-style: bg=blue
Hostname 192.168.0.2
host def-vm-*
User admin
host def-vm-1
Hostname 192.168.0.11
host def-vm-2
Hostname 192.168.0.12
host def-vm-3
Hostname 192.168.0.13
host def-vm-4
Hostname 192.168.0.14
host def-vm-5
Hostname 192.168.0.15
And also assuming that you saved the script in ~/bin/st then running this command:
$ st abc
will spawn to synchronised panes in your current window one with red and one with blue background
if you run:
$ st def-vm
or
$ st def-
it will spawn five panes to each one of the def-vm machines.
simple JSON pretty printer that you can easily copy paste
Simple JSON pretty printer that you can easily copy paste. Useful when you need to read a big JSON output and you are on a remote machine on which you can not install a lot.
python -c 'import sys; import json; import pprint; pprint.pprint(json.loads(sys.stdin.read()))'
Usage:
somecommand_that_generates_json_output | python -c 'import sys; import json; import pprint; pprint.pprint(json.loads(sys.stdin.read()))'
adding SOCKS5 support to python3 http client using just standard libs
I had a need for a simple way of running some HTTP queries through SOCKS5 (think OpenSSH -D) proxy. All the examples I could find on the internet required usage of external libraries, I prefer to just use the standard ones, makes things simpler in the end.
Here is the code I come up with, I borrowed some code from a different tool that I wrote sometime ago. Gist can be found here.
#!/usr/bin/env python3
import http.client
import socket
from struct import pack, unpack
class HTTPSocks5(http.client.HTTPConnection):
def setsocksproxy(self, host, port):
self.socks5host = host
self.socks5port = port
def connect(self):
if hasattr(self, "socks5host") and self.socks5host:
self.sock = self._create_connection(
(self.socks5host,self.socks5port), self.timeout, self.source_address)
error = ["succeeded", "general SOCKS server failure",\
"connection not allowed by ruleset", "Network unreachable",\
"Host unreachable", "Connection refused", "TTL expired",\
"Command not supported", "Address type not supported", "unassigned"]
data = pack('!3B',5,1,0) # lets connect to socks5 server
self.sock.send(data)
data = self.sock.recv(2)
auth = unpack('2B',data)[1] # do we need to authenticate
if auth != 255:
nport = pack('!H',self.port)
try:
if ":" in self.host: # we most likely have IPv6 here
data = pack('!4B',5,1,0,4)+\
socket.inet_pton(socket.AF_INET6,self.host)+nport
else: # IPv4
data = pack('!4B',5,1,0,1)+\
socket.inet_pton(socket.AF_INET,self.host)+nport
except socket.error: # or just a hostname to resolve by the SOCKS srv
data = pack('!5B',5,1,0,3,len(self.host))+\
bytearray(self.host,'UTF-8')+nport
self.sock.send(data)
data = self.sock.recv(256) # getting the status code
try:
code = unpack('BBB',data[:3])[1]
except:
raise("socks server sent a wrong replay")
if code != 0:
if code > 9:
code = 9
raise("socks server sent an error: %s" % (error[code],))
else:
raise("socks server requires authentication")
else:
self.sock = self._create_connection(
(self.host,self.port), self.timeout, self.source_address)
Subscribe to:
Posts (Atom)