How to determine your ssh-client's IP when you only konw its usrname and passwd

#https://www.cnblogs.com/ma6174/archive/2012/05/25/2508378.html
 2 
 3 #!/usr/bin/python
 4 #coding=utf-8
 5 import paramiko
 6 import threading
 7 
 8 def ssh2(ip,username,passwd,cmd):
 9      try:
10          ssh = paramiko.SSHClient()
11          ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
12          ssh.connect(ip,22,username,passwd,timeout=5)
13          for m in cmd:
14              stdin, stdout, stderr = ssh.exec_command(m)
# stdin.write("Y")   #if needed, input ‘Y’ 
15 out = stdout.readlines() 16 # below will show the output 17 for o in out: 18 print o, 19 print '%s\tOK\n'%(ip) 20 ssh.close() 21 except : 22 print '%s\tError\n'%(ip) 23 24 25 if __name__=='__main__': 26 cmd = ['cal','echo hello!'] 27 username = "username" 28 passwd = "passwd" 29 threads = [] 30 print "Begin......" 31 for i in range(0,255): 32 ip = '10.19.141.'+str(i) 33 a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd)) 34 a.start()

More:
    for i in range(221,228):
        for j in range(0,255):
             ip = '10.19.' + str(i) + '.' + str(j)
             a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
             a.start()


Note:
1.
 |  set_missing_host_key_policy(self, policy)
 |      Set the policy to use when connecting to a server that doesn't have a
 |      host key in either the system or local `.HostKeys` objects.  The
 |      default policy is to reject all unknown servers (using `.RejectPolicy`).
 |      You may substitute `.AutoAddPolicy` or write your own policy class.
 |
 |      :param .MissingHostKeyPolicy policy:
 |          the policy to use when receiving a host key from a
 |          previously-unknown server

2.
 |  connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=Fa
lse, gss_deleg_creds=True, gss_host=None, banner_timeout=None)
 |      Connect to an SSH server and authenticate to it.  The server's host key
 |      is checked against the system host keys (see `load_system_host_keys`)
 |      and any local host keys (`load_host_keys`).  If the server's hostname
 |      is not found in either set of host keys, the missing host key policy
 |      is used (see `set_missing_host_key_policy`).  The default policy is
 |      to reject the key and raise an `.SSHException`.
 |
 |      Authentication is attempted in the following order of priority:
 |
 |          - The ``pkey`` or ``key_filename`` passed in (if any)
 |          - Any key we can find through an SSH agent
 |          - Any "id_rsa", "id_dsa" or "id_ecdsa" key discoverable in
 |            ``~/.ssh/``
 |          - Plain username/password auth, if a password was given
 |
 |      If a private key requires a password to unlock it, and a password is
 |      passed in, that password will be used to attempt to unlock the key.
 |
 |      :param str hostname: the server to connect to
 |      :param int port: the server port to connect to
 |      :param str username:
 |          the username to authenticate as (defaults to the current local
 |          username)
 |      :param str password:
 |          a password to use for authentication or for unlocking a private key
 |      :param .PKey pkey: an optional private key to use for authentication
 |      :param str key_filename:
 |          the filename, or list of filenames, of optional private key(s) to
 |          try for authentication
 |      :param float timeout:
 |          an optional timeout (in seconds) for the TCP connect
 |      :param bool allow_agent:
 |          set to False to disable connecting to the SSH agent
 |      :param bool look_for_keys:
 |          set to False to disable searching for discoverable private key
 |          files in ``~/.ssh/``
 |      :param bool compress: set to True to turn on compression
 |      :param socket sock:
 |          an open socket or socket-like object (such as a `.Channel`) to use
 |          for communication to the target host
 |      :param bool gss_auth:
 |          ``True`` if you want to use GSS-API authentication
 |      :param bool gss_kex:
 |          Perform GSS-API Key Exchange and user authentication
 |      :param bool gss_deleg_creds: Delegate GSS-API client credentials or not
 |      :param str gss_host:
 |          The targets name in the kerberos database. default: hostname
 |      :param float banner_timeout: an optional timeout (in seconds) to wait
 |          for the SSH banner to be presented.
 |
 |      :raises BadHostKeyException: if the server's host key could not be
 |          verified
 |      :raises AuthenticationException: if authentication failed
 |      :raises SSHException: if there was any other error connecting or
 |          establishing an SSH session
 |      :raises socket.error: if a socket error occurred while connecting
 |
 |      .. versionchanged:: 1.15
 |          Added the ``banner_timeout``, ``gss_auth``, ``gss_kex``,
 |          ``gss_deleg_creds`` and ``gss_host`` arguments.
 
3.
 |  exec_command(self, command, bufsize=-1, timeout=None, get_pty=False)
 |      Execute a command on the SSH server.  A new `.Channel` is opened and
 |      the requested command is executed.  The command's input and output
 |      streams are returned as Python ``file``-like objects representing
 |      stdin, stdout, and stderr.
 |
 |      :param str command: the command to execute
 |      :param int bufsize:
 |          interpreted the same way as by the built-in ``file()`` function in
 |          Python
 |      :param int timeout:
 |          set command's channel timeout. See `Channel.settimeout`.settimeout
 |      :return:
 |          the stdin, stdout, and stderr of the executing command, as a
 |          3-tuple
 |
 |      :raises SSHException: if the server fails to execute the command
 |
4.

Help on class AutoAddPolicy in module paramiko.client:


class AutoAddPolicy(MissingHostKeyPolicy)
 |  Policy for automatically adding the hostname and new host key to the
 |  local `.HostKeys` object, and saving it.  This is used by `.SSHClient`.
 |
 |  Method resolution order:
 |      AutoAddPolicy
 |      MissingHostKeyPolicy
 |      __builtin__.object
 |
 |  Methods defined here:
 |
 |  missing_host_key(self, client, hostname, key)
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from MissingHostKeyPolicy:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)


 

posted on 2017-12-25 14:57  cdekelon  阅读(198)  评论(0编辑  收藏  举报

导航