久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Python paramiko使用方法代碼匯總

瀏覽:5日期:2022-07-04 16:46:46

1、用戶名、密碼登陸方式

import paramikoparamiko.util.log_to_file(’paramiko.log’) # 記錄日志文件ssh = paramiko.SSHClient()try: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(’139.xx.xx.xx’, username=’work’, password=’***’) cmd = ’ls’ # 需要執行的Linux命名 stdin, stdout, stderr = ssh.exec_command(cmd) #執行命令后的結構 print(stdout.readlines()) print(stdout.read().decode())except Exception as e: print('%s:%s' % (e.__class__, e))finally: # 關閉 ssh.close()

2、免密登陸方式

import paramikossh = paramiko.SSHClient()SSH_PRIVATE_KEY =’/Users/xueerhuan/.ssh/id_rsa’ #本地密鑰文件路徑try: key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY) # 無解密密碼時 #key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY, password=’******’) # 有解密密碼時, ssh.load_system_host_keys() #通過known_hosts 方式進行認證可以用這個,如果known_hosts 文件未定義還需要定義 known_hosts #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 通過公共方式進行認證 (不需要在known_hosts 文件中存在) ssh.connect(hostname=’139.XX.XX.XX’, port=22, username=’root’, pkey=key) stdin, stdout, stderr = ssh.exec_command('ps') # 獲取命令結果 result = stdout.read() # 打印輸出 print(result.decode())except Exception as e: print('%s:%s' % (e.__class__, e))finally: # 關閉 ssh.close()

注意:生成密碼的方法

A、進入本地 ssh文件夾 cd .ssh/

B、使用ssh-keygen生產本地公鑰和私鑰 ssh-keygen

xueerhuan@ubuntu:~/.ssh$ ls id_rsa id_rsa.pub

C、將生成的id_rsa.pub文件中的內容copy到目標機的.ssh/authorized_keys中就可以了,如果沒有authorized_keys,自己創建。但是要注意authorized_keys的權限一般是600

或者直接在本地使用一條命令也可以實現公鑰的復制,ssh-copy-id后面接入的用戶就是要支持免密登錄的用戶。

morra@ubuntu:~/.ssh$ ssh-copy-id 'morra@192.168.1.42'/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: '/home/morra/.ssh/id_rsa.pub'The authenticity of host ’192.168.1.42 (192.168.1.42)’ can’t be established.ECDSA key fingerprint is SHA256:/ufx+/OLtdsYy7vsdk4KDu9xJsBp6zHonRAf2jjT0GI.Are you sure you want to continue connecting (yes/no)? n^HPlease type ’yes’ or ’no’: yes/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysPassword:Number of key(s) added: 1Now try logging into the machine, with: 'ssh ’morra@192.168.1.42’' and check to make sure that only the key(s) you wanted were added.#去目標機器下,檢查authorized_keys文件localhost:.ssh morra$ cat authorized_keys

3、密碼上傳文件

import osimport paramikossh = paramiko.SSHClient()SSH_PRIVATE_KEY =’/Users/xueerhuan/.ssh/id_rsa’ #本地密鑰文件路徑key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY)paramiko.util.log_to_file(’paramiko.log’)ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(’139.XX.XX.XX’, username=’root’, password=’***’)t = ssh.get_transport()sftp = paramiko.SFTPClient.from_transport(t)d = sftp.put('mm.txt', '/home/work/.ssh/mm.txt')print(d)

4、免密上傳文件

import osimport paramikossh = paramiko.SSHClient()SSH_PRIVATE_KEY =’/Users/xueerhuan/.ssh/id_rsa’ #本地密鑰文件路徑key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY)paramiko.util.log_to_file(’paramiko.log’)ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname=’139.XX.XX.XX’, port=22, username=’root’, pkey=key)t = ssh.get_transport()sftp = paramiko.SFTPClient.from_transport(t)d = sftp.put('mm.txt', '/home/work/.ssh/mm.txt')print(d)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲国产精品一区二区久久 | 久久麻豆 | 中文在线视频 | 91性高湖久久久久久久久网站 | 欧美成在线观看 | 中文字幕av亚洲精品一部二部 | 国产激情在线观看 | 国产精品国产成人国产三级 | 这里只有精品视频 | 久久久国产一区二区 | 亚洲精品久久久久久下一站 | 午夜小视频在线观看 | 国产成人精品在线观看 | 国产亚洲欧美在线 | 欧美第一网站 | 99久久国产 | 久草在线观看福利视频 | 欧美精品在线观看 | www国产成人免费观看视频,深夜成人网 | 色综久久 | 男人的天堂在线视频 | 91tv亚洲精品香蕉国产一区 | 精品综合 | 黄色一级大片在线免费看产 | 精品无码久久久久国产 | 91亚洲国产成人久久精品网站 | 91视频在线网址 | 欧美精品一区二区视频 | 天天操妹子 | 久久久亚洲精品视频 | 国产日韩av在线 | 国产综合视频在线播放 | 天天干狠狠干 | 99r在线| 三级网站在线播放 | 国产亚洲精品精品国产亚洲综合 | 久色成人 | 在线成人www免费观看视频 | 久久亚洲一区 | 综合久久综合久久 | 干狠狠|