difflib paraiko模組
阿新 • • 發佈:2018-11-09
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()