difflib模块
import difflib
file1 = '/etc/passwd'
file2 = '/mnt/passwd'
with open(file1) as f1,open(file2) as f2: #对file1 fiel2进行打开
text1 = f1.readlines()
text2 = f2.readlines()
d = difflib.HtmlDiff() #实力化出一个difflib.HtmlDiff对象d
with open('passwd.html','w') as f:
f.write(d.make_file(text1,text2)) #将一个text1和text2的内容进行一个比较并生成一个html文件
文件在第6行出不一样:
paraiko模块
#1.创建一个ssh对象
import paramiko
client = paramiko.SSHClient()
#2.解决问题:如果之前没有连接过的IP,会出现,执行下面语句,会自动执行yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#3.连接服务器
client.connect(hostname='172.25.254.208',
port=22,
username='root',
password='redhat')
#4.执行操作
stdin,stdout,stderr = client.exec_command('ifconfig')
#5.获取执行的结果
result = stdout.read().decode('utf-8')
print(result)
print(stderr.read())
#6.关闭连接
client.close()
通过远程连接获取到的172.25.254.208的ip信息:
批量对远程主机进行连接
import paramiko
from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException
def connect(cmd,hostname,port,username,passwd): #定义一个连接函数
client = paramiko.SSHClient() #实力化出一个SSH对象
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #忽略其他因素的影响
try:
client.connect(hostname=hostname, #连接服务器
port=port,
username=username,
password=passwd
)
except NoValidConnectionsError as e: #对登陆过程中出现的异常进行捕获
print('error:连接失败')
except AuthenticationException as g:
print('error:密码错误')
else: #当登陆过程中,没有捕获到错误时,执行else后面的语句
stdin,stdout,stderr = client.exec_command(cmd) ##对执行语句的结果进行一个接受
print('连接成功')
result = stdout.read().decode('utf-8') #获取执行的结果
print(result)
client.close() #关闭连接
with open('IP.txt') as e:
for i in e:
i = i.rstrip()
i = i.split(':')
if i[0] == "":
break
print('正在连接%s主机'.center(50,'*') %i[0])
i[0],i[1],i[2],i[3] = i
connect('hostname',i[0],int(i[1]),i[2],i[3])
将连接封装成一个模块
import os
import paramiko
from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException
class SSHClient(object):
def __init__(self,cmd,hostname,port,username,passwd):
'''
:param cmd: 用户需要的执行的命令
:param hostname: 目标服务器的主机名
:param port:目标服务器的端口
:param username: 目标服务器的用户名
:param passwd: 目标服务器的密码
'''
self.cmd = cmd
self.hostname = hostname
self.port = port
self.username = username
self.passwd = passwd
def connect(self):
client = paramiko.SSHClient() #实力化一个SHH对象
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #忽略登陆过程中的操作
try:
client.connect(hostname=self.hostname,
port=self.port,
username=self.username,
password=self.passwd)
except NoValidConnectionsError:
print('error:登陆失败')
except AuthenticationException:
print('error:密码错误')
else:
stdin,stdout,stderr = client.exec_command(self.cmd)
result = stdout.read().decode('utf-8')
print(result)
client.close()
def main():
while True:
li = os.listdir('conf')
print('服务'.center(50,'*'))
for i in li:
print(i.split('.')[0] + '服务')
service_choice = input('请输入您所要进行的服务:')
if service_choice == 'exit' or service_choice == 'quit':
exit()
with open('conf/' + service_choice + '.conf') as e:
for i in e:
print(i.rstrip().split(':')[0])
SSH_info = input('请选择您所要连接的主机:')
e.seek(0,0)
for j in e:
if SSH_info == j.rstrip().split(':')[0]:
cmd = input('请输入您所要执行的命令:')
hostname,port,username,passwd = j.rstrip().split(':')
SSh_obj = SSHClient(cmd,hostname,port,username,passwd)
SSh_obj.connect()
main()
通过密码实现文件的上传和下载
import paramiko
transport = paramiko.Transport('172.25.254.208',22)
transport.connect(username='root',password='redhat')
sftp = paramiko.SFTPClient.from_transport(transport)
#sftp.put('IP.txt','/root/Desktop/file1') #注意:必须包含文件名
sftp.get('/root/Desktop/payton','file5.py')
transport.close()