可通过远程Linux主机配置文件信息连接到远程主机自动操作各种服务
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/9/11 16:44
# @Author : qhh
# @Site :
# @File : pyshell.py
# @Software: PyCharm
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# 查阅https://blog.csdn.net/qq_32502511/article/details/79849826
import paramiko
import configparser
import re
import time
# 获取配置文件数据
def get_cfg_data(cfg_path):
cf = configparser.ConfigParser()
cf.read(cfg_path, encoding='utf-8')
# 获取所有section,返回值为list
secs = cf.sections()
print('可输入信息列表%s' % secs)
# # 获取db中的所有属性名
# dboption = cf.options('db')
# print(dboption)
#
# 获取db中的键值对
input_comm = input("输入ip地址的最后一个(例如配置文件中的为[host_169]则需要输入169或者核对可输入信息列表查看下划线_后的数值是否存在):")
if 'host_'+input_comm not in secs:
print('输入的信息有误系统自动退出!请检查host.ini配置文件中的中括号[]内是否包含了所输入的%s' % input_comm)
exit(0)
dbitem = cf.items('host_'+input_comm)
print(dict(dbitem))
# 获取section为db,属性名为db_pass的值
# input_comm = input("输入ip地址的最后一个(例如配置文件中的为[host_169]则需要输入169):")
# li = []
# print(cf.get('host_'+input_comm, 'host_ip'))
# print(cf.get('host_'+input_comm, 'host_user'))
# print(cf.get('host_'+input_comm, 'host_pw'))
# li.append(cf.get('host_'+input_comm, 'host_ip'))
# li.append(cf.get('host_'+input_comm, 'host_user'))
# li.append(cf.get('host_'+input_comm, 'host_pw'))
return dict(dbitem)
# 定义一个类,表示一台远端linux主机
class Linux(object):
# 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
def __init__(self, ip, username, password, timeout=30):
self.ip = ip
self.username = username
self.password = password
self.timeout = timeout
# transport和chanel
self.t = ''
self.chan = ''
# 链接失败的重试次数
self.try_times = 3
# 调用该方法连接远程主机
def connect(self):
while True:
# 连接过程中可能会抛出异常,比如网络不通、链接超时
try:
self.t = paramiko.Transport(sock=(self.ip, 22))
self.t.connect(username=self.username, password=self.password)
self.chan = self.t.open_session()
self.chan.settimeout(self.timeout)
self.chan.get_pty()
self.chan.invoke_shell()
# 如果没有抛出异常说明连接成功,直接返回
print(u'连接%s成功' % self.ip)
# 接收到的网络数据解码为str
print(self.chan.recv(65535).decode('utf-8'))
return
# 这里不对可能的异常如socket.error, socket.timeout细化,直接一网打尽
except Exception as e1:
if self.try_times != 0:
print(u'连接%s失败,进行重试' % self.ip)
self.try_times -= 1
else:
print(u'重试3次失败,结束程序')
exit(1)
# 断开连接
def close(self):
self.chan.close()
self.t.close()
# 发送要执行的命令
def send(self, cmd):
cmd += '\r'
# 通过命令执行提示符来判断命令是否执行完成
p = re.compile('[email protected]:.*?#')
result = ''
# 发送要执行的命令
self.chan.send(cmd)
# 回显很长的命令可能执行较久,通过循环分批次取回回显,执行成功返回true,失败返回false
while True:
time.sleep(0.5)
ret = self.chan.recv(65535)
ret = ret.decode('utf-8')
result += ret
return result
# 连接正常的情况
if __name__ == '__main__':
# 获取配置文件中的数据
cfg_data = get_cfg_data('host.ini')
host = Linux(cfg_data['host_ip'], cfg_data['host_user'], cfg_data['host_pw']) # 传入Ip,用户名,密码
host.connect()
while True:
# result = host.send('ifconfig') # 发送一个查看ip的命令
# host.send('ifconfig')
host.send("alias ls='ls --color=never'")
host.send("alias ll='ls -l --color=never'")
# 遍历命令列表
for key, value in cfg_data.items():
print(key, value)
if 'comm' in key:
result = host.send(value)
# print("返回的结果--")
# print(result)
input_com = input("输入命令:")
result = host.send(input_com)
print("返回的结果--")
print(result)
host.close()
