使用paramiko庫(kù)ssh連接到遠(yuǎn)端云主機(jī)上時(shí),非常偶現(xiàn)卡死現(xiàn)象,連接無(wú)法退出(可以是執(zhí)行命令時(shí)云主機(jī)重啟等造成)。需要給定一段時(shí)間,不管命令執(zhí)行是否卡住,都退出連接,顯示命令執(zhí)行超時(shí)錯(cuò)誤。
實(shí)現(xiàn)方式:
線程+事件,在線程中執(zhí)行ssh命令,給事件配置超時(shí)時(shí)間。
代碼示例:
1 from threading import Thread, Event
2 import paramiko
class SshClient(object):
def __init__(self, ip, port, username, password):
self.ip = ip
self.host = host
self.username = username
self.password = password
def exec_command(cmd, timeout):
log.info(u"在ip:%s上執(zhí)行命令%s" % (self.ip, cmd))
sc = paramiko.SSHClient()
sc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 用來(lái)接收stdout stderror status信息
res = [None, None, None]
def get_return(start_event, res_list):
_, cmd_stdout, cmd_stderr = sc.exec_command(command=cmd, timeout=timeout)
channel = cmd_stdout.channel
cmd_status = channel.recv_exit_status()
res_list[0] = cmd_stdout
res_list[1] = cmd_stderr
res_list[2] = cmd_status
start_event.set() # 表示線程已經(jīng)執(zhí)行完畢
try:
sc.connect(hostname=self.ip, port=self.port, username=self.username, password=self.password, timeout=30) # 這里的timeout是連接使用的,與我們要的不同
start_evt = Event()
t = Thread(target=get_return, args=(start_evt, res))
t.start()
start_evt.wait(timeout=timeout)
# 執(zhí)行到這里說(shuō)明線程已經(jīng)退出
if None in res:
raise Exception(u"命令超時(shí)退出")
stdout, stderr, status = res
if status != 0:
raise Exception(u"命令執(zhí)行返回非0!返回值為%s,錯(cuò)誤信息為%s" % (status, stdout.read() + stderr.read()))
return stdout.read() + stderr.read()
finally:
sc.close()
}
知識(shí)點(diǎn)補(bǔ)充:
python paramiko的使用介紹
一: 使用paramiko
#設(shè)置ssh連接的遠(yuǎn)程主機(jī)地址和端口
t=paramiko.Transport((ip,port))
#設(shè)置登錄名和密碼
t.connect(username=username,password=password)
#連接成功后打開(kāi)一個(gè)channel
chan=t.open_session()
#設(shè)置會(huì)話超時(shí)時(shí)間
chan.settimeout(session_timeout)
#打開(kāi)遠(yuǎn)程的terminal
chan.get_pty()
#激活terminal
chan.invoke_shell()
然后就可以通過(guò)chan.send('command')和chan.recv(recv_buffer)來(lái)遠(yuǎn)程執(zhí)行命令以及本地獲取反饋。
二: paramiko的兩個(gè)模塊介紹
paramiko有兩個(gè)模塊SSHClient()和SFTPClient()
SSHClient()的使用代碼:
import paramiko
ssh = paramiko.SSHClient() # 創(chuàng)建SSH對(duì)象
# 允許連接不在know_hosts文件中的主機(jī)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 連接服務(wù)器
ssh.connect(hostname='192.168.2.103', port=22, username='root', password='123456')
stdin, stdout, stderr = ssh.exec_command('ls') # 執(zhí)行命令
result = stdout.read() # 獲取命令結(jié)果
print (str(result,encoding='utf-8'))
ssh.close() # 關(guān)閉連接
SSHClient()里有個(gè)transport變量,是用于獲取連接,我們也可單獨(dú)的獲取到transport變量,然后執(zhí)行連接操作
import paramiko
transport = paramiko.Transport(('192.168.2.103', 22))
transport.connect(username='root', password='123456')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
print (str(stdout.read(),encoding='utf-8'))
transport.close()
用transport實(shí)現(xiàn)上傳下載以及命令的執(zhí)行:
#coding:utf-8
import paramiko
import uuid
class SSHConnection(object):
def __init__(self, host='192.168.2.103', port=22, username='root',pwd='123456'):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None
def connect(self):
transport = paramiko.Transport((self.host,self.port))
transport.connect(username=self.username,password=self.pwd)
self.__transport = transport
def close(self):
self.__transport.close()
def upload(self,local_path,target_path):
# 連接,上傳
# file_name = self.create_file()
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 將location.py 上傳至服務(wù)器 /tmp/test.py
sftp.put(local_path, target_path)
def download(self,remote_path,local_path):
sftp = paramiko.SFTPClient.from_transport(self.__transport)
sftp.get(remote_path,local_path)
def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 執(zhí)行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 獲取命令結(jié)果
result = stdout.read()
print (str(result,encoding='utf-8'))
return result
ssh = SSHConnection()
ssh.connect()
ssh.cmd("ls")
ssh.upload('s1.py','/tmp/ks77.py')
ssh.download('/tmp/test.py','kkkk',)
ssh.cmd("df")
ssh.close()
到此這篇關(guān)于使paramiko庫(kù)執(zhí)行命令時(shí),在給定的時(shí)間強(qiáng)制退出的文章就介紹到這了,更多相關(guān)paramiko庫(kù)執(zhí)行命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python Paramiko模塊的使用實(shí)際案例
- python利用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行命令的方法
- windows下python安裝paramiko模塊和pycrypto模塊(簡(jiǎn)單三步)
- Python Paramiko模塊的安裝與使用詳解
- python paramiko實(shí)現(xiàn)ssh遠(yuǎn)程訪問(wèn)的方法
- 使用paramiko遠(yuǎn)程執(zhí)行命令、下發(fā)文件的實(shí)例