This commit is contained in:
qiangyanwen 2023-01-07 18:39:27 +08:00
parent 75a75dac55
commit b93e3013d0
1 changed files with 3 additions and 2 deletions

View File

@ -4,6 +4,7 @@
# @Author :qiangyanwen # @Author :qiangyanwen
# @File :ssh_client.py # @File :ssh_client.py
import paramiko import paramiko
from config.log import logger
class SSHConnection(object): class SSHConnection(object):
@ -17,7 +18,7 @@ class SSHConnection(object):
self._client = None self._client = None
def __enter__(self): def __enter__(self):
print("客户端开始创建连接.....") logger.info("ssh开始创建链接")
transport = paramiko.Transport((self._host, self._port)) transport = paramiko.Transport((self._host, self._port))
transport.connect(username=self._username, password=self._password) transport.connect(username=self._username, password=self._password)
self._transport = transport self._transport = transport
@ -52,7 +53,7 @@ class SSHConnection(object):
self._transport.close() self._transport.close()
if self._client: if self._client:
self._client.close() self._client.close()
print("客户端关闭已成功.....") logger.info("ssh 关闭连接")
with SSHConnection("47.96.135.132", 22, "root", "Qyw1994@520") as ssh: with SSHConnection("47.96.135.132", 22, "root", "Qyw1994@520") as ssh: