233 lines
6.2 KiB
Plaintext
233 lines
6.2 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "98be7050",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Failed to set locale, defaulting to C.UTF-8\n",
|
||
"CentOS Linux 8 - AppStream 9.8 B/s | 38 B 00:03 \n",
|
||
"Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import paramiko\n",
|
||
"\n",
|
||
"\n",
|
||
"class SshClass:\n",
|
||
" \"\"\"\n",
|
||
" ssh连接对象\n",
|
||
" 本对象提供了密钥连接、密码连接、命令执行、关闭连接\n",
|
||
" \"\"\"\n",
|
||
" ip = ''\n",
|
||
" port = 22\n",
|
||
" username = ''\n",
|
||
" timeout = 0\n",
|
||
" ssh = None\n",
|
||
"\n",
|
||
" def __init__(self, ip, username, port=22, timeout=30):\n",
|
||
" \"\"\"\n",
|
||
" 初始化ssh对象\n",
|
||
" :param ip: str 主机IP\n",
|
||
" :param username: str 登录用户名\n",
|
||
" :param port: int ssh端口\n",
|
||
" :param timeout: int 连接超时\n",
|
||
" \"\"\"\n",
|
||
" self.ip = ip\n",
|
||
" self.username = username\n",
|
||
" self.port = port\n",
|
||
" self.timeout = timeout\n",
|
||
" ssh = paramiko.SSHClient()\n",
|
||
" ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
|
||
" self.ssh = ssh\n",
|
||
"\n",
|
||
" def conn_by_key(self, key):\n",
|
||
" \"\"\"\n",
|
||
" 密钥连接\n",
|
||
" :param key: str rsa密钥路径\n",
|
||
" :return: ssh连接对象\n",
|
||
" \"\"\"\n",
|
||
" rsa_key = paramiko.RSAKey.from_private_key_file(key)\n",
|
||
" self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, pkey=rsa_key, timeout=self.timeout)\n",
|
||
" if self.ssh:\n",
|
||
" print(\"密钥连接成功.\")\n",
|
||
" else:\n",
|
||
" self.close()\n",
|
||
" raise Exception(\"密钥连接失败.\")\n",
|
||
"\n",
|
||
" def conn_by_pwd(self, pwd):\n",
|
||
" \"\"\"\n",
|
||
" 密码连接\n",
|
||
" :param pwd: str 登录密码\n",
|
||
" :return: ssh连接对象\n",
|
||
" \"\"\"\n",
|
||
" self.ssh.connect(hostname=self.ip, port=self.port, username=self.username, password=pwd)\n",
|
||
" if self.ssh:\n",
|
||
" print(\"密码连接成功.\")\n",
|
||
" else:\n",
|
||
" self.close()\n",
|
||
" raise Exception(\"密码连接失败.\")\n",
|
||
"\n",
|
||
" def exec_command(self, command):\n",
|
||
" \"\"\"\n",
|
||
" 命令控制\n",
|
||
" :param command: str 命令\n",
|
||
" :return: dict 命令执行的返回结果\n",
|
||
" \"\"\"\n",
|
||
" if command:\n",
|
||
" stdin, stdout, stderr = self.ssh.exec_command(command)\n",
|
||
" return {\n",
|
||
" \"stdin\": command,\n",
|
||
" \"stdout\": stdout.read(),\n",
|
||
" \"stderr\": stderr.read()\n",
|
||
" }\n",
|
||
" else:\n",
|
||
" self.close()\n",
|
||
" raise Exception(\"命令不能为空字符串.\")\n",
|
||
"\n",
|
||
" def close(self):\n",
|
||
" \"\"\"\n",
|
||
" 关闭当前连接\n",
|
||
" :return:\n",
|
||
" \"\"\"\n",
|
||
" if self.ssh:\n",
|
||
" self.ssh.close()\n",
|
||
" else:\n",
|
||
" raise Exception(\"ssh关闭失败,当前对象并没有ssh连接.\")\n",
|
||
"\n",
|
||
"\n",
|
||
"if __name__ == '__main__':\n",
|
||
" SSH = SshClass(\"111.111.6.115\", \"root\", port=22)\n",
|
||
" SSH.conn_by_pwd(\"123456\")\n",
|
||
" print(SSH.exec_command(\"ls\"))\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "dd7e564d",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"#!pip install paramiko\n",
|
||
"import paramiko"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "9c7053e9",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"ssh = paramiko.SSHClient()\n",
|
||
"ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "7b0a0f37",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"key = \"./ssh/id_rsa\"\n",
|
||
"rsa_key = paramiko.RSAKey.from_private_key_file(key)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"id": "cb215350",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"PKey(alg=RSA, bits=3072, fp=SHA256:NR5wmeI1gzG4h8L+2wPchaM/8Zmliog2Pe5hNH8LW7E)"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"rsa_key \n",
|
||
"#SHA256:NR5wmeI1gzG4h8L+2wPchaM/8Zmliog2Pe5hNH8LW7E\n",
|
||
"#SHA256:NR5wmeI1gzG4h8L+2wPchaM/8Zmliog2Pe5hNH8LW7E"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"id": "17267b54",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"<bound method SSHClient._log of <paramiko.client.SSHClient object at 0x7fde96651ad0>>"
|
||
]
|
||
},
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"ssh._log"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 25,
|
||
"id": "327bb122",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"ip = \"175.24.226.114\"\n",
|
||
"port = 222\n",
|
||
"username = \"git\"\n",
|
||
"timeout = 30\n",
|
||
"ssh.connect(hostname=ip, port=port, username=username, pkey=rsa_key, timeout=timeout)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "da988de0",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.11.4"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|