# ssh username@ip passwd
# 解決的問題:如果隻前沒有連線過的ip 會出現
"""[root@foundation0 .ssh]# ssh [email protected]
the authenticity of host '172.25.254.36 (172.25.254.36)' can't be established.
ecdsa key fingerprint is bc:f5:f7:4b:39:41:d8:27:61:b0:56:55:e5:05:a9:5b.
are you sure you want to continue connecting (yes/no)? yes
warning: permanently added '172.25.254.36' (ecdsa) to the list of known hosts.
[email protected]'s password:
last login: sun jan 12 10:16:28 2020
[root@foundation54 ~]# exit
logout
connection to 172.25.254.36 closed.
[root@foundation0 .ssh]# ssh [email protected]
[email protected]'s password:
"""
parammiko
基於ssh用於連線遠端服務操作包:
遠端執行命令
上傳命令
import paramiko
# 建立乙個ssh物件
client = paramiko.sshclient()
# 自動選擇yes
client.set_missing_host_key_policy(paramiko.autoaddpolicy())
# 連線的伺服器
client.connect(
hostname='172.25.254.11',
username='root',
password='westos'
)# 執行操作
# 標準輸入 標準正確輸出 標準錯誤輸出
stdin,stdout,stderr = client.exec_command('hostname')
# 獲取命令的執行結果
print(stdout.read().decode('utf-8'))
# 關閉連線
client.close()
from paramiko.ssh_exception import novalidconnectionserror, authenticationexception
import paramiko
def connect(cmd, hostname, user, password):
# 建立乙個ssh物件
client = paramiko.sshclient()
# 自動選擇yes
client.set_missing_host_key_policy(paramiko.autoaddpolicy())
try:
# 連線的伺服器
client.connect(
hostname=hostname,
username=user,
password=password
)except novalidconnectionserror as e:
return '主機%s連線失敗' % (hostname)
except authenticationexception as e:
return '主機%s密碼錯誤' % (hostname)
except exception as e:
return '未知錯誤:', e
# 執行操作
# 標準輸入 標準正確輸出 標準錯誤輸出
stdin, stdout, stderr = client.exec_command(cmd)
# 獲取命令的執行結果
print(stdout.read().decode('utf-8'))
# 關閉連線
client.close()
with open('hosts') as f:
for line in f:
# 172.25.254.254:root:dd
hostname, username, password = line.strip().split(':')
print(hostname.center(50, '*'))
print('使用者名稱:%s' % (username))
res = connect('hostname', hostname, username, password)
import paramiko
transport = paramiko.transport(('172.25.60.250', 22))
transport.connect(username='root', password='westos')
sftp = paramiko.sftpclient.from_transport(transport)
# 上傳檔案, 包含檔名(將當前電腦中的檔案上傳到位址為172.25.60.250的電腦中)
sftp.put('/home/kiosk/desktop/test/passwd1', '/home/kiosk/desktop/haha')
transport.close()
import paramiko
transport = paramiko.transport(('172.25.254.13', 22))
transport.connect(username='root', password='westos')
sftp = paramiko.sftpclient.from_transport(transport)
sftp.get('/home/kiosk/dd1', 'dd1')
transport.close()
paramiko 基於ssh用於連線遠端伺服器
ssh username ip passwd 解決的問題 如果隻前沒有連線過的ip 會出現 root foundation0 ssh ssh root 172.25.254.36 the authenticity of host 172.25.254.36 172.25.254.36 can t b...
python下paramiko模組ssh登入
python下有個paramiko模組,這個模組可以實現ssh登入linux伺服器,下面貼出 注意,我在centos5.6下,python2.6.5,paramiko 1.7的版本下測試成功。usr bin env python import paramiko hostname 172.28.102...
Python的paramiko模組ssh操作
sshclient 用於連線遠端伺服器並執行基本命令 基於使用者名稱密碼連線 12 3456 78910 1112 1314 1516 importparamiko 建立ssh物件 ssh paramiko.sshclient 允許連線不在know hosts檔案中的主機 ssh.set missi...