Skip to content

Commit

Permalink
paramiko ssh client
Browse files Browse the repository at this point in the history
  • Loading branch information
Mari Wahl committed Dec 17, 2014
1 parent f4d51be commit b0c65c4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Network_and_802.11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
### paramiko

- Several scripts for SSH connections:
* command
* tunneling
* ssh client
* ssh tunneling


---
Expand Down
97 changes: 97 additions & 0 deletions Network_and_802.11/paramiko/ssh_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python

__author__ = "bt3"


import paramiko
import sys
import getopt

def usage():
print "Usage: ssh_client.py <IP> -p <PORT> -u <USER> -c <COMMAND> -a <PASSWORD> -k <KEY> -c <COMMAND>"
print " -a password authentication"
print " -i identity file location"
print " -c command to be issued"
print " -p specify the port"
print " -u specify the username"
print
print "Examples:"
print "ssh_client.py 129.49.76.26 -u buffy -p 22 -a killvampires -c pwd"
sys.exit()


def ssh_client(ip, port, user, passwd, key, command):
client = paramiko.SSHClient()
if key:
client.load_host_keys(key)
else:
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:
ssh_session.exec_command(command)
print
print ssh_session.recv(4096)


def main():

if not len(sys.argv[1:]):
usage()

# parse the arguments
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()

# Get user and ip
IP = sys.argv[1]
print "[*] Initializing connection to " + IP

# Handle the options and arguments
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 "[*] User set to " + USER
if PORT:
print "[*] The port to be used is %d. " % PORT
if PASSWORD:
print "[*] A password with length %d was submitted. " %len(PASSWORD)
if KEY:
print "[*] The key at %s will be used." % KEY
if COMMAND:
print "[*] Executing the command '%s' in the host..." % COMMAND
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()

0 comments on commit b0c65c4

Please sign in to comment.