|
import paramiko
import socket
import logging
from base64 import b64encode
import time
logging.basicConfig(loglevel=logging.DEBUG)
LOG = logging.getLogger("squid")
def http_proxy(proxy,target,auth=None, timeout=None):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect(proxy)
LOG.debug("connected")
cmd_connect = "CONNECT %s:%d HTTP/1.1\r\n" % target
if auth is not None:
cmd_connect += " : basic %s\r\n" % b64encode('%s:%s' % auth)
cmd_connect += "\r\n"
LOG.debug("--> %s" % str(cmd_connect))
sock.sendall(cmd_connect)
response = []
sock.settimeout(2)
try:
# in worst case this loop will take 2 seconds if not response was received (sock.timeout)
while True:
chunk = sock.recv(1024)
if not chunk:
break
response.append(chunk)
if "\r\n\r\n" in chunk:
break
except socket.error,se:
if "timed out" not in se:
response = [se]
response = ''.join(response)
LOG.debug("<-- %s" % str(response))
if "200 connection established" not in response.lower():
raise Exception("Unable to establish HTTP-Tunnel: %s" % str(response))
return sock
if __name__ == "__main__":
LOG.setLevel(logging.DEBUG)
LOG.debug("--start--")
sock = http_proxy(proxy=("172.28.117.157", 80),
target=("10.95.113.131", 22),
auth=('germany', 'germany'),
timeout=50)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="10.95.113.131", sock=sock, username="root", password="123")
# time.sleep(100)
print "#> login users \n%s" % ssh.exec_command("w")[1].read() |
|
|