遠端執行linux命令等;
'''paramiko
'''import
traceback
import
paramiko as param,time,re
from scp import
scpclient
"""ssh=param.sshclient()
ssh.set_missing_host_key_policy(param.autoaddpolicy())
ssh.connect(hostname='103.20.1.181',port=22,username='root',password='888888')
stdin,stdout,stderr=ssh.exec_command('pwd')
#print('stdin:',stdin)
#print('stdout:',stdout)
#print('stderr:',stderr)
result=stdout.read().decode()
print(result)
err=stderr.read().decode()
print(err)
ssh.close()
"""class
sshconnection():
def__init__(self,host,user,passwd,port=22): #
遠端server的ip、使用者名稱、密碼
self.trans =param.transport((host,port))
try:
self.trans.connect(username=user, password=passwd)
except
: traceback.print_exc()
raise exception("
%s建立連線失敗!
"%host)
defclose(self):
self.trans.close()
def upload(self,local_path,remote_path): #
通過sftp上傳檔案,本地遠端都要具體到檔名
sftpclient=param.sftpclient.from_transport(self.trans)
sftpclient.put(local_path,remote_path)
sftpclient.close()
def download(self,remote_path,local_path):#
sftpclient=param.sftpclient.from_transport(self.trans)
sftpclient.get(remote_path,local_path)
sftpclient.close()
defscp_uploadfile(self,localfile,remotefile):
#ssh = param.sshclient()
#ssh.set_missing_host_key_policy(param.autoaddpolicy())
#ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password)
#ssh._transport = self.trans
#scpclient = scpclient(ssh.get_transport(), socket_timeout=15.0) #配合上面的全部注釋項
scpclient = scpclient(self.trans,socket_timeout=15.0) #
簡化版try
: scpclient.put(localfile, remotefile)
except
: traceback.print_exc()
finally
: scpclient.close()
defscp_downloadfile(self,remotefile,localfile):
scpclient = scpclient(self.trans,socket_timeout=15.0) #
簡化版try
: scpclient.get(remotefile,localfile)
except
: traceback.print_exc()
finally
: scpclient.close()
def do_cmd(self,command):#
遠端執行linux命令,返回執行結果
sshclient=param.sshclient()
#ssh.set_missing_host_key_policy(param.autoaddpolicy())
#ssh.connect(hostname=self.host,port=self.port,username=self.username,password=self.password)
sshclient._transport=self.trans
a,out,err=sshclient.exec_command(command)
result=out.read().decode()
sshclient.close()
return
result
def get_remote_ip(self):#
獲取遠端伺服器的ip
ifconfig=self.do_cmd('
ifconfig')
return re.search(r'
\d+\.\d+\.\d+\.\d+
',ifconfig).group()
if__name__=='
__main__':
ssh=sshconnection('
*.*.*.*
','username
','password')
#ssh=sshconnection('*.*.*.*','username','password')
#ssh.connect()
(ssh)
#command='pwd'
#print('pwd命令結果:',ssh.cmd(command))
#ifconfig=ssh.cmd('ls')
#print(ifconfig)
#ssh.scp_downloadfile('/home/oneu/ccpos/config/config.ini','f:\\test\\config.ini')
# ssh.scp_downloadfile('
/home/oneu/ccpos/config/version.xml
', '
f:\\test\\version.xml')
ssh.close()
'''ssh.upload('pp232.txt','/root/jin/aa/pp233.txt')
ssh.download('/root/jin/aa/lall.txt','f:\\pylianxi\\test\\113.txt')
ifconfig=ssh.cmd('ifconfig')
print(ifconfig)
print('匹配的ip位址為:',ssh.get_remote_ip())
'''
python paramiko 各種錯誤
這個錯誤出現在伺服器接受連線但是ssh守護程序沒有及時響應的情況 預設是15s 要解決這個問題,需要將paramiko的響應等待時間調長。transport.py中def init 初始化函式中 how long seconds to wait for the ssh banner self.ban...
Python Paramiko模組的使用
windows下有很多非常好的ssh客戶端,比如putty。在python的世界裡,你可以使用原始套接字和一些加密函式建立自己的ssh客戶端或服務端,但如果有現成的模組,為什麼還要自己實現呢。使用paramiko庫中的pycrypto能夠讓你輕鬆使用ssh2協議。paramiko的安裝方法網上有很多...
Python paramiko實現跳轉控制
通過ssh 的proxycommand,建立關係,用paramiko模組,呼叫proxycommand方法 一 通過一台跳板機免密 a主機 b跳板機 c主機 做好a免密登入b,b免密登入c a主機 ssh下新增檔案b private,內容為b的私鑰 a主機 ssh下增加config檔案,內容為 ho...