av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

python Paramiko使用示例

瀏覽:112日期:2022-07-10 15:28:34

Paramiko 是由 Python 語言編寫的一個擴展模塊,提供了基于 SSHv2 協(xié)議 (包括客戶端和服務(wù)端)的多種功能實現(xiàn)。通常被用來遠(yuǎn)程控制類 UNIX 系統(tǒng)。

Paramiko 可以直接使用 pip 命令安裝:

$ pip install paramiko

此處不作過多介紹,參考后文中的代碼示例。

遠(yuǎn)程執(zhí)行 Linux 命令

代碼如下:

import paramiko# 初始化 SSH 客戶端,通過用戶名密碼連接至遠(yuǎn)程服務(wù)器client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy)client.connect(hostname=’remoteserver_ip’, username=’username’, password=’password’)# 通過 RSA 秘鑰驗證的方式連接至遠(yuǎn)程 SSH 服務(wù)# private_key = paramiko.RSAKey.from_private_key_file(’~/.ssh/id_rsa’)# client.connect(hostname='remoteserver_ip', username='username', pkey=private_key)# 遠(yuǎn)程執(zhí)行 df -h 命令并打印輸出stdin, stdout, stderr = client.exec_command(’df -h’)print(stdout.read().decode(’utf-8’))client.close()

運行效果如下:

python Paramiko使用示例

SFTP 文件傳輸

示例代碼如下:

import paramikotransport = paramiko.Transport((’hostname_or_ip’, port))# 通過用戶名密碼完成驗證建立連接transport.connect(username=’username’, password=’password’)# 通過 RSA 私鑰文件完成驗證建立連接# private_key = paramiko.RSAKey.from_private_key_file(’/path/to/private_key_file’)# transport.connect(username=’username’, pkey=private_key)sftp = paramiko.SFTPClient.from_transport(transport)localpath = 'localfile'remotepath = 'remotefile_fullpath'sftp.put(localpath, remotepath)print('Successfully uploaded')transport.close()

綜合示例

代碼如下(文件名 ssh_connection.py ):

import paramikoimport getpassimport osclass SSHConnection(): def __init__(self, user, host, port=22, password=’’): self.username = user self.host = host self.port = port self.password = password self.keyfile = self.get_keyfile() def get_keyfile(self, path=os.getcwd()): default_keyfile = os.path.join( os.environ[’HOME’], ’.ssh’, ’id_rsa’) if ’id_rsa’ in os.listdir(path): keyfile = os.path.join(path, ’id_rsa’) elif os.path.isfile(default_keyfile): keyfile = default_keyfile else: keyfile = ’’ return keyfile def connect(self): transport = paramiko.Transport((self.host, self.port)) if self.password: transport.connect(username=self.username, password=self.password) elif self.keyfile: transport.connect(username=self.username,pkey=paramiko.RSAKey.from_private_key_file(self.keyfile)) else: password = getpass.getpass('Password for %s@%s: ' % (self.username, self.host)) transport.connect(username=self.username, password=password) self._transport = transport print('Connected to %s as %s' % (self.host, self.username)) def close(self): self._transport.close() def run_cmd(self, command): ssh = paramiko.SSHClient() ssh._transport = self._transport stdin, stdout, stderr = ssh.exec_command(command) res = stdout.read().decode(’utf-8’) error = stderr.read().decode(’utf-8’) if error.strip(): return error else: return res def trans_file(self, localpath, remotepath, method=’’): sftp = paramiko.SFTPClient.from_transport(self._transport) if method == ’put’: sftp.put(localpath, remotepath) print('File %s has uploaded to %s' % (localpath, remotepath)) elif method == ’get’: sftp.get(remotepath, localpath) print('File %s has saved as %s' % (remotepath, localpath)) else: print(’usage: trans_file(localpath, remotepath, method='get/put'’) def __del__(self): self.close()

測試結(jié)果如下:

(python3) D:Programpythondevops>pythonPython 3.7.2 (default, Jan 2 2019, 17:07:39) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32Type 'help', 'copyright', 'credits' or 'license' for more information.>>> from ssh_connection import SSHConnection>>> client = SSHConnection(’starky’,’127.0.0.1’)>>> client.connect()Connected to 127.0.0.1 as starky>>> client.run_cmd(’uname -a’)’Linux server1 5.0.0-20-generic #21-Ubuntu SMP Mon Jun 24 09:32:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linuxn’>>> client.trans_file(’id_rsa.pub’, ’/home/starky/id_rsa.pub’, method=’put’)File id_rsa.pub has uploaded to /home/starky/id_rsa.pub>>> client.run_cmd(’ls -l /home/starky/id_rsa.pub’)’-rw-rw-r-- 1 starky starky 410 7月 20 15:01 /home/starky/id_rsa.pubn’>>> exit()

以上就是python Paramiko使用示例的詳細(xì)內(nèi)容,更多關(guān)于python Paramiko的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美天堂在线 | 午夜影院在线观看视频 | 成人区精品一区二区婷婷 | 中国一级黄色录像 | 国产精品福利在线 | 99视频+国产日韩欧美 | 四虎在线观看视频 | 一级黄色在线观看 | 99精品欧美一区二区蜜桃免费 | 日韩大片在线观看 | 少妇在线 | 日韩精品影院 | 黄色免费片 | 四虎com| 91av在线免费观看 | av不卡一区 | 国产精品美女久久久 | 欧美在线视频观看 | 天堂中文av | 精品一区二区三区四区 | 午夜美女福利 | 天堂成人在线 | 最新超碰| 18在线观看网站 | 久久久久久久久久久国产 | 在线中文字幕 | 91精品国产日韩91久久久久久 | 深夜福利av | 欧美区一区二 | a级在线观看 | 欧美黄色片在线观看 | 国产精品一区一区三区 | 亚洲一区亚洲二区 | 99热在线免费观看 | 国产欧美日韩在线 | 中文字幕在线免费观看视频 | 欧美激情五月 | 国产精品久久久一区二区 | 在线观看av免费 | 国产h在线观看 | 亚洲黄色成人 |