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

作者:TTyb 时间:2023-01-25 23:10:33 

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

代码如下


#!/usr/bin/python
# -*- coding: utf-8 -*-

'''https://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.html'''

import pexpect
import types

username = "root"
ip = "192.168.***.***"
password = "****"
pex = pexpect.spawn('ssh %s@%s' % (username, ip))

def _check(pattern, timeout=120):
 i = pex.expect(pattern, timeout=timeout)
 return i

def sendcr(cmd):
 if pex == None:
   return 0
 n = pex.send("%s\r" % cmd)
 return n

def getexec(cmd):

child = pexpect.spawn(cmd)
 child.expect(pexpect.EOF)
 return child.before

if __name__ == '__main__':

checklist1 = [["(?i)Connection refused", False],
        ["(?i)Host key verification failed.", False],
        ["(?i)VENUSTECH AUDIT SYSTEM MA1000", True],
        # ["(?i)#\[/]",True],    # hpux
        ["(?i).+>", True], # windows
        [".+[>$#]\s*$", True], # debian
        ["(?i)Last login", True],
        ["(?i)access denied", False],
        ["(?i)NT_STATUS_LOGON_FAILURE", False],
        ["(?i)are you sure you want to continue connecting", "yes"],
        ["(?i)authentication fail(?!ure)", False],
        ["(?i)connection closed by remote host", False],
        ["(?i)login failed", False],
        ["(?i)login incorrect", False],
        ["(?i)need to be root", False],
        ["(?i)no route to host", False],
        ["(?i)not found", False],
        ["(?i)Bad secrets", False],
        ["(?i)incorrect password", False],
        ["(?i)permission denied", False],
        # ["(?i)terminal type",terminal_type],
        ["This private key will be ignored.", False],
        ["(?i)no route to host", False],
        ["(?i)press 'Enter' key to proceed", "\r"],
        ["(?i)Y/N", 'Y'],
        [pexpect.EOF, False],
        [pexpect.TIMEOUT, False],
        ["(?i)Enter passphrase for key .*:", password],
        ["(?i)assword", password],
        ["(?i)passwd", password],
        ["(?i)sername", username],
        ["(?i)(?<!sful )login", username],
        ["(?i)----------------------------------------------------------------", True]]

checklist2 = [i[0] for i in checklist1]
 while True:
   i = _check(checklist2)
   print i, checklist1[i], checklist1[i][1]
   if (type(checklist1[i][1]) is types.BooleanType):
     if type(checklist1[i][1]):
       break
   else:
     sendcr(checklist1[i][1])

cmd = "ls -l /etc/rsyslog.conf"
 result = getexec(cmd)
 print "result", result

打印结果:


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

来源:https://www.cnblogs.com/TTyb/p/6595472.html

标签:python,连接,Linux
0
投稿

猜你喜欢

  • python练习程序批量修改文件名

    2022-01-29 06:53:12
  • Pycharm中配置远程Docker运行环境的教程图解

    2022-08-06 09:39:19
  • Python实现Word文档样式批量处理

    2022-01-13 01:16:05
  • Python字符串逆序输出的实例讲解

    2023-04-21 22:59:46
  • Python匹配中文的正则表达式

    2022-03-21 18:05:50
  • Python自动发邮件脚本

    2022-12-31 18:31:29
  • JS获得选取checkbox整行数据的方法

    2024-02-26 21:10:33
  • 通过代码实例了解页面置换算法原理

    2024-01-24 21:26:23
  • Echarts实例教程之树形图表的实现方法

    2024-04-18 09:44:01
  • Pytorch Tensor的统计属性实例讲解

    2023-11-21 22:27:52
  • Linux下设置每天自动备份数据库的方法

    2024-01-24 02:45:31
  • darknet框架中YOLOv3对数据集进行训练和预测详解

    2023-11-21 23:11:15
  • 让所有IE支持HTML5的解决方案

    2009-10-31 18:43:00
  • Asp模板制作方法详解

    2007-10-11 19:05:00
  • Pandas DataFrame中的tuple元素遍历的实现

    2023-12-21 09:47:00
  • 图文详解Mysql使用left join写查询语句执行很慢问题的解决

    2024-01-13 17:14:52
  • 卓越网的配送服务让我很不满意

    2009-03-19 13:49:00
  • 将pandas.dataframe的数据写入到文件中的方法

    2022-07-13 14:56:38
  • Python任务自动化工具tox使用教程

    2022-08-28 12:33:58
  • Python实现XML文件解析的示例代码

    2022-02-24 11:03:43
  • asp之家 网络编程 m.aspxhome.com