python感悟,我的第一個Python隨筆

 2023-11-05 阅读 20 评论 0

摘要:  自學Python也很長時間了,注冊博客園寫了第一篇隨筆。之前想過很多次,但是始終不知道該怎么開始,內容如何,現在想想,隨筆嘛,是自己的想法,也自己的實踐,又是自己的鍛煉。話不多說,開始今天的正式內容。 Py

  自學Python也很長時間了,注冊博客園寫了第一篇隨筆。之前想過很多次,但是始終不知道該怎么開始,內容如何,現在想想,隨筆嘛,是自己的想法,也自己的實踐,又是自己的鍛煉。話不多說,開始今天的正式內容。

Python的paramiko模塊。

  paramiko是用python語言寫的一個模塊,遵循SSH2協議,支持以加密和認證的方式,進行遠程服務器的連接。由于使用的是python這樣的能夠跨平臺運行的語言,所以所有python支持的平臺,如Linux, Solaris, BSD, MacOS X,?Windows等,paramiko都可以支持,因此,如果需要使用SSH從一個平臺連接到另外一個平臺,進行一系列的操作時,paramiko是最佳工具之一。  

python感悟?SSHClient

用于連接遠程服務器并執行基本命令

基于用戶名密碼連接:

import paramiko# 創建SSH對象
ssh = paramiko.SSHClient()
# 允許連接不在know_hosts文件中的主機
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 連接服務器
ssh.connect(hostname='192.168.30.129', port=22, username='sunqi', password='43797189')# 執行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 獲取命令結果
result = stdout.read()# 關閉連接
ssh.close()

?

python的心得體會、?

示例代碼:

 1 import paramiko
 2 
 3 ssh = paramiko.SSHClient()
 4 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
 5 ssh.connect(hostname='192.168.30.129',port=22,username='root',password='43797189')#測試機器為Linux虛擬機(CentOS 7)
 6 while True:
 7     cmd = input('>>:')
 8     stdin,stdout,stderr = ssh.exec_command(cmd)
 9     result = stdout.read()
10     if stdout:
11         print(str(result,'utf8'))
12     else:
13         print(str(stderr.read(),'utf8'))
14 
15 ssh.close()

?

SFTPClient

python讀書筆記,用于連接遠程服務器并執行上傳:

import paramiko'''
從windows上傳文件到Linux虛擬機
IP地址: 192.168.30.129
端口:22
'''
host = '192.168.30.129'
port = 22t = paramiko.Transport((host,port))
t.connect(username="root",password='43797189',)
sftp = paramiko.SFTPClient.from_transport(t)
target_path = '/var/log/windows.log'
local_path = 'E:\\sunqi.log'
sftp.put(local_path,target_path)
t.close()

最后將多線程和這個paramiko模塊綜合起來寫了一個批量主機管理程序

請看到的朋友原諒我的混亂代碼規范,我會加倍努力的!

批量主機管理系統遠程連接主機實現命令的執行文件的上傳下載:

python基礎讀后感,主機列表:

示例代碼:host_dir.py

host_dic = {'group1':{'C1':{'host':'192.168.30.129','port':22,'username':'root','password':'43797189'},'C2':{'host':'192.168.30.130','port':22,'username':'root','password':'43797189'},'C3':{'host':'192.168.30.131','port':22,'username':'root','password':'43797189'}},'group2':{'C4':{'host':'192.168.30.132','port':22,'username':'root','password':'43797189'},'C5':{'host':'192.168.30.134','port':22,'username':'root','password':'43797189'},'C6':{'host':'192.168.30.135','port':22,'username':'root','password':'43797189'},}
}

運行代碼:run_code.py

import threading
import paramiko
import os
from paramiko模塊.批量主機管理小項目.host_dir import host_dic'''
主機批量管理程序
實現多個主機的同時管理
利用多線程以及paramiko模塊
實現多個主機同時執行命令,上傳或下載文件
'''
class host_manage():def __init__(self,host,port,username,password):self.host = hostself.port = portself.username = usernameself.password = passworddef command(self,cmd):ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password)#cmd = input(">>:")stdin, stdout, stderr = ssh.exec_command(cmd)result = stdout.read()if stdout:print(str(result, 'utf8'))else:print(str(stderr.read(), 'utf8'))def upload(self):t = paramiko.Transport(self.host,self.port)t.connect(username=self.username,password=self.password)sftp = paramiko.SFTPClient.from_transport(t)target_path = '/var/log/windows.log'local_path = 'E:\\sunqi.log'sftp.put(local_path,target_path)t.close()passdef download(self):pass
def choose_group():print('可管理主機分組')for k in host_dic:print(k)for i in host_dic[k]:print(host_dic[k][i])group_num = input('>>:選擇主機編號')return  group_num
def run():num = choose_group()print('已選組號:%s' %num)selected = input(">>:輸入即將進行的操作:command、upload、download")if selected =="command":cmd = input('>>:請輸入要批量操作的命令:')thread_conut = []for i in host_dic[num]:func = host_manage(host=host_dic[num][i]['host'],port=host_dic[num][i]['port'],username=host_dic[num][i]['username'],password=host_dic[num][i]['password'])if hasattr(func,selected):p = threading.Thread(target=getattr(func,selected),args=(cmd,))thread_conut.append(p)p.start()for i in thread_conut:i.join()if __name__ == '__main__':run()

?

python編程心得體會?  

轉載于:https://www.cnblogs.com/SunQi-Tony/p/8494908.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/166722.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息