# coding=utf-8
import traceback
import paramiko
import logging
import socket
from paramiko import ssh_exception
from paramiko.ssh_exception import AuthenticationException
def verify(sip, sport, susername, spassword):
conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
conn.connect(sip, port=sport, username=susername, password=spassword, timeout=15)
connect_result = "Connect Server {0} {1} {2} {3} 主机连接成功!\n".format(
sip, sport, susername, spassword)
# 连接成功
logging.info(connect_result)
data = '{"code": "1000", "msg": "主机连接成功","result": true}'
except AuthenticationException:
connect_result = "Connect Server {0} {1} {2} {3} 用户名或密码错误!\n".format(
sip, sport, susername, spassword)
# 用户名或密码错误
print(traceback.format_exc())
logging.info(connect_result)
data = '{"code": "5000", "msg": "用户名或密码错误","result": false}'
except socket.timeout:
connect_result = "Connect Server {0} {1} {2} {3} 主机连接异常!\n".format(
sip, sport, susername, spassword)
# 主机连接异常
print(traceback.format_exc())
logging.info(connect_result)
data = '{"code": "6000", "msg": "主机连接异常","result": false}'
except ssh_exception.SSHException:
connect_result = "Connect Server {0} {1} {2} {3} 端口错误!\n".format(
sip, sport, susername, spassword)
# 端口错误
print(traceback.format_exc())
logging.info(connect_result)
data = '{"code": "7000", "msg": "端口错误","result": false}'
return data
if __name__ == '__main__':
res = verify("10.1.10.31", 22, "root", "keepmesecret")
print(res)