pxssh.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. '''This class extends pexpect.spawn to specialize setting up SSH connections.
  2. This adds methods for login, logout, and expecting the shell prompt.
  3. PEXPECT LICENSE
  4. This license is approved by the OSI and FSF as GPL-compatible.
  5. http://opensource.org/licenses/isc-license.txt
  6. Copyright (c) 2012, Noah Spurrier <noah@noah.org>
  7. PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
  8. PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
  9. COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. '''
  18. from pexpect import ExceptionPexpect, TIMEOUT, EOF, spawn
  19. import time
  20. import os
  21. __all__ = ['ExceptionPxssh', 'pxssh']
  22. # Exception classes used by this module.
  23. class ExceptionPxssh(ExceptionPexpect):
  24. '''Raised for pxssh exceptions.
  25. '''
  26. class pxssh (spawn):
  27. '''This class extends pexpect.spawn to specialize setting up SSH
  28. connections. This adds methods for login, logout, and expecting the shell
  29. prompt. It does various tricky things to handle many situations in the SSH
  30. login process. For example, if the session is your first login, then pxssh
  31. automatically accepts the remote certificate; or if you have public key
  32. authentication setup then pxssh won't wait for the password prompt.
  33. pxssh uses the shell prompt to synchronize output from the remote host. In
  34. order to make this more robust it sets the shell prompt to something more
  35. unique than just $ or #. This should work on most Borne/Bash or Csh style
  36. shells.
  37. Example that runs a few commands on a remote server and prints the result::
  38. from pexpect import pxssh
  39. import getpass
  40. try:
  41. s = pxssh.pxssh()
  42. hostname = raw_input('hostname: ')
  43. username = raw_input('username: ')
  44. password = getpass.getpass('password: ')
  45. s.login(hostname, username, password)
  46. s.sendline('uptime') # run a command
  47. s.prompt() # match the prompt
  48. print(s.before) # print everything before the prompt.
  49. s.sendline('ls -l')
  50. s.prompt()
  51. print(s.before)
  52. s.sendline('df')
  53. s.prompt()
  54. print(s.before)
  55. s.logout()
  56. except pxssh.ExceptionPxssh as e:
  57. print("pxssh failed on login.")
  58. print(e)
  59. Example showing how to specify SSH options::
  60. from pexpect import pxssh
  61. s = pxssh.pxssh(options={
  62. "StrictHostKeyChecking": "no",
  63. "UserKnownHostsFile": "/dev/null"})
  64. ...
  65. Note that if you have ssh-agent running while doing development with pxssh
  66. then this can lead to a lot of confusion. Many X display managers (xdm,
  67. gdm, kdm, etc.) will automatically start a GUI agent. You may see a GUI
  68. dialog box popup asking for a password during development. You should turn
  69. off any key agents during testing. The 'force_password' attribute will turn
  70. off public key authentication. This will only work if the remote SSH server
  71. is configured to allow password logins. Example of using 'force_password'
  72. attribute::
  73. s = pxssh.pxssh()
  74. s.force_password = True
  75. hostname = raw_input('hostname: ')
  76. username = raw_input('username: ')
  77. password = getpass.getpass('password: ')
  78. s.login (hostname, username, password)
  79. '''
  80. def __init__ (self, timeout=30, maxread=2000, searchwindowsize=None,
  81. logfile=None, cwd=None, env=None, ignore_sighup=True, echo=True,
  82. options={}, encoding=None, codec_errors='strict'):
  83. spawn.__init__(self, None, timeout=timeout, maxread=maxread,
  84. searchwindowsize=searchwindowsize, logfile=logfile,
  85. cwd=cwd, env=env, ignore_sighup=ignore_sighup, echo=echo,
  86. encoding=encoding, codec_errors=codec_errors)
  87. self.name = '<pxssh>'
  88. #SUBTLE HACK ALERT! Note that the command that SETS the prompt uses a
  89. #slightly different string than the regular expression to match it. This
  90. #is because when you set the prompt the command will echo back, but we
  91. #don't want to match the echoed command. So if we make the set command
  92. #slightly different than the regex we eliminate the problem. To make the
  93. #set command different we add a backslash in front of $. The $ doesn't
  94. #need to be escaped, but it doesn't hurt and serves to make the set
  95. #prompt command different than the regex.
  96. # used to match the command-line prompt
  97. self.UNIQUE_PROMPT = "\[PEXPECT\][\$\#] "
  98. self.PROMPT = self.UNIQUE_PROMPT
  99. # used to set shell command-line prompt to UNIQUE_PROMPT.
  100. self.PROMPT_SET_SH = "PS1='[PEXPECT]\$ '"
  101. self.PROMPT_SET_CSH = "set prompt='[PEXPECT]\$ '"
  102. self.SSH_OPTS = ("-o'RSAAuthentication=no'"
  103. + " -o 'PubkeyAuthentication=no'")
  104. # Disabling host key checking, makes you vulnerable to MITM attacks.
  105. # + " -o 'StrictHostKeyChecking=no'"
  106. # + " -o 'UserKnownHostsFile /dev/null' ")
  107. # Disabling X11 forwarding gets rid of the annoying SSH_ASKPASS from
  108. # displaying a GUI password dialog. I have not figured out how to
  109. # disable only SSH_ASKPASS without also disabling X11 forwarding.
  110. # Unsetting SSH_ASKPASS on the remote side doesn't disable it! Annoying!
  111. #self.SSH_OPTS = "-x -o'RSAAuthentication=no' -o 'PubkeyAuthentication=no'"
  112. self.force_password = False
  113. # User defined SSH options, eg,
  114. # ssh.otions = dict(StrictHostKeyChecking="no",UserKnownHostsFile="/dev/null")
  115. self.options = options
  116. def levenshtein_distance(self, a, b):
  117. '''This calculates the Levenshtein distance between a and b.
  118. '''
  119. n, m = len(a), len(b)
  120. if n > m:
  121. a,b = b,a
  122. n,m = m,n
  123. current = range(n+1)
  124. for i in range(1,m+1):
  125. previous, current = current, [i]+[0]*n
  126. for j in range(1,n+1):
  127. add, delete = previous[j]+1, current[j-1]+1
  128. change = previous[j-1]
  129. if a[j-1] != b[i-1]:
  130. change = change + 1
  131. current[j] = min(add, delete, change)
  132. return current[n]
  133. def try_read_prompt(self, timeout_multiplier):
  134. '''This facilitates using communication timeouts to perform
  135. synchronization as quickly as possible, while supporting high latency
  136. connections with a tunable worst case performance. Fast connections
  137. should be read almost immediately. Worst case performance for this
  138. method is timeout_multiplier * 3 seconds.
  139. '''
  140. # maximum time allowed to read the first response
  141. first_char_timeout = timeout_multiplier * 0.5
  142. # maximum time allowed between subsequent characters
  143. inter_char_timeout = timeout_multiplier * 0.1
  144. # maximum time for reading the entire prompt
  145. total_timeout = timeout_multiplier * 3.0
  146. prompt = self.string_type()
  147. begin = time.time()
  148. expired = 0.0
  149. timeout = first_char_timeout
  150. while expired < total_timeout:
  151. try:
  152. prompt += self.read_nonblocking(size=1, timeout=timeout)
  153. expired = time.time() - begin # updated total time expired
  154. timeout = inter_char_timeout
  155. except TIMEOUT:
  156. break
  157. return prompt
  158. def sync_original_prompt (self, sync_multiplier=1.0):
  159. '''This attempts to find the prompt. Basically, press enter and record
  160. the response; press enter again and record the response; if the two
  161. responses are similar then assume we are at the original prompt.
  162. This can be a slow function. Worst case with the default sync_multiplier
  163. can take 12 seconds. Low latency connections are more likely to fail
  164. with a low sync_multiplier. Best case sync time gets worse with a
  165. high sync multiplier (500 ms with default). '''
  166. # All of these timing pace values are magic.
  167. # I came up with these based on what seemed reliable for
  168. # connecting to a heavily loaded machine I have.
  169. self.sendline()
  170. time.sleep(0.1)
  171. try:
  172. # Clear the buffer before getting the prompt.
  173. self.try_read_prompt(sync_multiplier)
  174. except TIMEOUT:
  175. pass
  176. self.sendline()
  177. x = self.try_read_prompt(sync_multiplier)
  178. self.sendline()
  179. a = self.try_read_prompt(sync_multiplier)
  180. self.sendline()
  181. b = self.try_read_prompt(sync_multiplier)
  182. ld = self.levenshtein_distance(a,b)
  183. len_a = len(a)
  184. if len_a == 0:
  185. return False
  186. if float(ld)/len_a < 0.4:
  187. return True
  188. return False
  189. ### TODO: This is getting messy and I'm pretty sure this isn't perfect.
  190. ### TODO: I need to draw a flow chart for this.
  191. def login (self, server, username, password='', terminal_type='ansi',
  192. original_prompt=r"[#$]", login_timeout=10, port=None,
  193. auto_prompt_reset=True, ssh_key=None, quiet=True,
  194. sync_multiplier=1, check_local_ip=True):
  195. '''This logs the user into the given server.
  196. It uses
  197. 'original_prompt' to try to find the prompt right after login. When it
  198. finds the prompt it immediately tries to reset the prompt to something
  199. more easily matched. The default 'original_prompt' is very optimistic
  200. and is easily fooled. It's more reliable to try to match the original
  201. prompt as exactly as possible to prevent false matches by server
  202. strings such as the "Message Of The Day". On many systems you can
  203. disable the MOTD on the remote server by creating a zero-length file
  204. called :file:`~/.hushlogin` on the remote server. If a prompt cannot be found
  205. then this will not necessarily cause the login to fail. In the case of
  206. a timeout when looking for the prompt we assume that the original
  207. prompt was so weird that we could not match it, so we use a few tricks
  208. to guess when we have reached the prompt. Then we hope for the best and
  209. blindly try to reset the prompt to something more unique. If that fails
  210. then login() raises an :class:`ExceptionPxssh` exception.
  211. In some situations it is not possible or desirable to reset the
  212. original prompt. In this case, pass ``auto_prompt_reset=False`` to
  213. inhibit setting the prompt to the UNIQUE_PROMPT. Remember that pxssh
  214. uses a unique prompt in the :meth:`prompt` method. If the original prompt is
  215. not reset then this will disable the :meth:`prompt` method unless you
  216. manually set the :attr:`PROMPT` attribute.
  217. '''
  218. ssh_options = ''.join([" -o '%s=%s'" % (o, v) for (o, v) in self.options.items()])
  219. if quiet:
  220. ssh_options = ssh_options + ' -q'
  221. if not check_local_ip:
  222. ssh_options = ssh_options + " -o'NoHostAuthenticationForLocalhost=yes'"
  223. if self.force_password:
  224. ssh_options = ssh_options + ' ' + self.SSH_OPTS
  225. if port is not None:
  226. ssh_options = ssh_options + ' -p %s'%(str(port))
  227. if ssh_key is not None:
  228. try:
  229. os.path.isfile(ssh_key)
  230. except:
  231. raise ExceptionPxssh('private ssh key does not exist')
  232. ssh_options = ssh_options + ' -i %s' % (ssh_key)
  233. cmd = "ssh %s -l %s %s" % (ssh_options, username, server)
  234. # This does not distinguish between a remote server 'password' prompt
  235. # and a local ssh 'passphrase' prompt (for unlocking a private key).
  236. spawn._spawn(self, cmd)
  237. i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connection closed by remote host", EOF], timeout=login_timeout)
  238. # First phase
  239. if i==0:
  240. # New certificate -- always accept it.
  241. # This is what you get if SSH does not have the remote host's
  242. # public key stored in the 'known_hosts' cache.
  243. self.sendline("yes")
  244. i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
  245. if i==2: # password or passphrase
  246. self.sendline(password)
  247. i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
  248. if i==4:
  249. self.sendline(terminal_type)
  250. i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT])
  251. if i==7:
  252. self.close()
  253. raise ExceptionPxssh('Could not establish connection to host')
  254. # Second phase
  255. if i==0:
  256. # This is weird. This should not happen twice in a row.
  257. self.close()
  258. raise ExceptionPxssh('Weird error. Got "are you sure" prompt twice.')
  259. elif i==1: # can occur if you have a public key pair set to authenticate.
  260. ### TODO: May NOT be OK if expect() got tricked and matched a false prompt.
  261. pass
  262. elif i==2: # password prompt again
  263. # For incorrect passwords, some ssh servers will
  264. # ask for the password again, others return 'denied' right away.
  265. # If we get the password prompt again then this means
  266. # we didn't get the password right the first time.
  267. self.close()
  268. raise ExceptionPxssh('password refused')
  269. elif i==3: # permission denied -- password was bad.
  270. self.close()
  271. raise ExceptionPxssh('permission denied')
  272. elif i==4: # terminal type again? WTF?
  273. self.close()
  274. raise ExceptionPxssh('Weird error. Got "terminal type" prompt twice.')
  275. elif i==5: # Timeout
  276. #This is tricky... I presume that we are at the command-line prompt.
  277. #It may be that the shell prompt was so weird that we couldn't match
  278. #it. Or it may be that we couldn't log in for some other reason. I
  279. #can't be sure, but it's safe to guess that we did login because if
  280. #I presume wrong and we are not logged in then this should be caught
  281. #later when I try to set the shell prompt.
  282. pass
  283. elif i==6: # Connection closed by remote host
  284. self.close()
  285. raise ExceptionPxssh('connection closed')
  286. else: # Unexpected
  287. self.close()
  288. raise ExceptionPxssh('unexpected login response')
  289. if not self.sync_original_prompt(sync_multiplier):
  290. self.close()
  291. raise ExceptionPxssh('could not synchronize with original prompt')
  292. # We appear to be in.
  293. # set shell prompt to something unique.
  294. if auto_prompt_reset:
  295. if not self.set_unique_prompt():
  296. self.close()
  297. raise ExceptionPxssh('could not set shell prompt '
  298. '(received: %r, expected: %r).' % (
  299. self.before, self.PROMPT,))
  300. return True
  301. def logout (self):
  302. '''Sends exit to the remote shell.
  303. If there are stopped jobs then this automatically sends exit twice.
  304. '''
  305. self.sendline("exit")
  306. index = self.expect([EOF, "(?i)there are stopped jobs"])
  307. if index==1:
  308. self.sendline("exit")
  309. self.expect(EOF)
  310. self.close()
  311. def prompt(self, timeout=-1):
  312. '''Match the next shell prompt.
  313. This is little more than a short-cut to the :meth:`~pexpect.spawn.expect`
  314. method. Note that if you called :meth:`login` with
  315. ``auto_prompt_reset=False``, then before calling :meth:`prompt` you must
  316. set the :attr:`PROMPT` attribute to a regex that it will use for
  317. matching the prompt.
  318. Calling :meth:`prompt` will erase the contents of the :attr:`before`
  319. attribute even if no prompt is ever matched. If timeout is not given or
  320. it is set to -1 then self.timeout is used.
  321. :return: True if the shell prompt was matched, False if the timeout was
  322. reached.
  323. '''
  324. if timeout == -1:
  325. timeout = self.timeout
  326. i = self.expect([self.PROMPT, TIMEOUT], timeout=timeout)
  327. if i==1:
  328. return False
  329. return True
  330. def set_unique_prompt(self):
  331. '''This sets the remote prompt to something more unique than ``#`` or ``$``.
  332. This makes it easier for the :meth:`prompt` method to match the shell prompt
  333. unambiguously. This method is called automatically by the :meth:`login`
  334. method, but you may want to call it manually if you somehow reset the
  335. shell prompt. For example, if you 'su' to a different user then you
  336. will need to manually reset the prompt. This sends shell commands to
  337. the remote host to set the prompt, so this assumes the remote host is
  338. ready to receive commands.
  339. Alternatively, you may use your own prompt pattern. In this case you
  340. should call :meth:`login` with ``auto_prompt_reset=False``; then set the
  341. :attr:`PROMPT` attribute to a regular expression. After that, the
  342. :meth:`prompt` method will try to match your prompt pattern.
  343. '''
  344. self.sendline("unset PROMPT_COMMAND")
  345. self.sendline(self.PROMPT_SET_SH) # sh-style
  346. i = self.expect ([TIMEOUT, self.PROMPT], timeout=10)
  347. if i == 0: # csh-style
  348. self.sendline(self.PROMPT_SET_CSH)
  349. i = self.expect([TIMEOUT, self.PROMPT], timeout=10)
  350. if i == 0:
  351. return False
  352. return True
  353. # vi:ts=4:sw=4:expandtab:ft=python: