系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

通过python连接Linux命令行代码实例

时间:2020-02-18来源:系统城作者:电脑系统城

这篇文章主要介绍了通过python连接Linux命令行代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下


 
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''https://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.html'''
  5.  
  6. import pexpect
  7. import types
  8.  
  9.  
  10. username = "root"
  11. ip = "192.168.***.***"
  12. password = "****"
  13. pex = pexpect.spawn('ssh %s@%s' % (username, ip))
  14.  
  15.  
  16. def _check(pattern, timeout=120):
  17. i = pex.expect(pattern, timeout=timeout)
  18. return i
  19.  
  20.  
  21. def sendcr(cmd):
  22. if pex == None:
  23. return 0
  24. n = pex.send("%s\r" % cmd)
  25. return n
  26.  
  27.  
  28. def getexec(cmd):
  29.  
  30. child = pexpect.spawn(cmd)
  31. child.expect(pexpect.EOF)
  32. return child.before
  33.  
  34.  
  35. if __name__ == '__main__':
  36.  
  37. checklist1 = [["(?i)Connection refused", False],
  38. ["(?i)Host key verification failed.", False],
  39. ["(?i)VENUSTECH AUDIT SYSTEM MA1000", True],
  40. # ["(?i)#\[/]",True], # hpux
  41. ["(?i).+>", True], # windows
  42. [".+[>$#]\s*$", True], # debian
  43. ["(?i)Last login", True],
  44. ["(?i)access denied", False],
  45. ["(?i)NT_STATUS_LOGON_FAILURE", False],
  46. ["(?i)are you sure you want to continue connecting", "yes"],
  47. ["(?i)authentication fail(?!ure)", False],
  48. ["(?i)connection closed by remote host", False],
  49. ["(?i)login failed", False],
  50. ["(?i)login incorrect", False],
  51. ["(?i)need to be root", False],
  52. ["(?i)no route to host", False],
  53. ["(?i)not found", False],
  54. ["(?i)Bad secrets", False],
  55. ["(?i)incorrect password", False],
  56. ["(?i)permission denied", False],
  57. # ["(?i)terminal type",terminal_type],
  58. ["This private key will be ignored.", False],
  59. ["(?i)no route to host", False],
  60. ["(?i)press 'Enter' key to proceed", "\r"],
  61. ["(?i)Y/N", 'Y'],
  62. [pexpect.EOF, False],
  63. [pexpect.TIMEOUT, False],
  64. ["(?i)Enter passphrase for key .*:", password],
  65. ["(?i)assword", password],
  66. ["(?i)passwd", password],
  67. ["(?i)sername", username],
  68. ["(?i)(?<!sful )login", username],
  69. ["(?i)----------------------------------------------------------------", True]]
  70.  
  71. checklist2 = [i[0] for i in checklist1]
  72. while True:
  73. i = _check(checklist2)
  74. print i, checklist1[i], checklist1[i][1]
  75. if (type(checklist1[i][1]) is types.BooleanType):
  76. if type(checklist1[i][1]):
  77. break
  78. else:
  79. sendcr(checklist1[i][1])
  80.  
  81. cmd = "ls -l /etc/rsyslog.conf"
  82. result = getexec(cmd)
  83. print "result", result

打印结果:


 
  1. 26 ['(?i)assword', '***'] ***
  2. 5 ['(?i)Last login', True] True
  3. result -rw-r--r--. 1 root root 3167 Mar 13 11:24 /etc/rsyslog.conf

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

分享到:

相关信息

  • linux定时关机设置教程

    当linux在运作时不能直接关闭电源容易将档案系统损毁,因此需要用shutdown以安全的方式关闭,那么这个操作该怎么实现呢?下面就为大家带来了详细教程。...

    2022-11-07

  • linux强制删除文件教程

    由于linux系统和我们常用的windows系统是不一样的,所以如果是初学者,可能会不知道linux怎么强制删除文件,其实我们只要打开终端,使用命令就可以删除了。...

    2022-11-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载