远程连接Linux

https://www.cnblogs.com/xshan/p/13037604.html

一.
G:\Flasktest>pip3 install paramiko
Collecting paramiko
Downloading https://files.pythonhosted.org/packages/95/19/124e9287b43e6ff3ebb9cdea3e5e8e88475a873c05ccdf8b7e20d2c42
01e/paramiko-2.7.2-py2.py3-none-any.whl (206kB)
100% |████████████████████████████████| 215kB 643kB/s
Collecting pynacl>=1.0.1 (from paramiko)
Downloading https://files.pythonhosted.org/packages/6d/d9/e07edb489aacc819ff76cab97d7de751c6d00f48c6a600a9f4b774508
0c4/PyNaCl-1.4.0-cp37-cp37m-win_amd64.whl (206kB)
100% |████████████████████████████████| 215kB 5.4MB/s
Collecting cryptography>=2.5 (from paramiko)
Downloading https://files.pythonhosted.org/packages/48/f8/05ac4ffb66c8c7dfad2862a141b56b8c2918c4b4e24917e020dd23f2e
94a/cryptography-3.2.1-cp37-cp37m-win_amd64.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 1.6MB/s
Collecting bcrypt>=3.1.3 (from paramiko)
Downloading https://files.pythonhosted.org/packages/d8/ba/21c475ead997ee21502d30f76fd93ad8d5858d19a3fad7cd153de698c
4dd/bcrypt-3.2.0.tar.gz (42kB)
100% |████████████████████████████████| 51kB 2.2MB/s
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing wheel metadata ... done
Requirement already satisfied: six in e:\python3\lib\site-packages (from pynacl>=1.0.1->paramiko) (1.14.0)
Collecting cffi>=1.4.1 (from pynacl>=1.0.1->paramiko)
Using cached https://files.pythonhosted.org/packages/87/f0/a4ec0ac56fa09d0cf6440dfa817562e8bea181b4bbb6cde3a86271e7
8097/cffi-1.14.3-cp37-cp37m-win_amd64.whl
Collecting pycparser (from cffi>=1.4.1->pynacl>=1.0.1->paramiko)
Using cached https://files.pythonhosted.org/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e7
1821/pycparser-2.20-py2.py3-none-any.whl
Building wheels for collected packages: bcrypt
Building wheel for bcrypt (PEP 517) ... done
Stored in directory: C:\Users\Administrator\AppData\Local\pip\Cache\wheels\df\ff\0f\e588ec95ea51f480627d66264f5587f
4105694ab7ddd61e711
Successfully built bcrypt
Installing collected packages: pycparser, cffi, pynacl, cryptography, bcrypt, paramiko
Successfully installed bcrypt-3.2.0 cffi-1.14.3 cryptography-3.2.1 paramiko-2.7.2 pycparser-2.20 pynacl-1.4.0

G:\Flasktest>

二.
import paramiko

创建SSH对象

ssh = paramiko.SSHClient()

允许连接不在know_hosts文件中的主机

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

连接服务器

ssh.connect(hostname='47.115.1.74', port=22, username='root', password='1111@')

执行命令

stdin, stdout, stderr = ssh.exec_command('touch ttt')

获取命令结果

result = stdout.read()
print(111)
print(result)

关闭连接

ssh.close()

你可能感兴趣的:(远程连接Linux)