mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-18 14:40:25 -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()
|
Loading…
Add table
Add a link
Reference in a new issue