使用Python 3的paramiko库连接本地SSH服务器指南
最编程
2024-02-10 08:55:08
...
- Python : 3.8.11
- OS : Ubuntu Kylin 20.04
- Conda : 4.10.1
- Pycharm : 2021.1.3
Ubuntu Kylin 安装openssh-server 开启sshd服务
(base) coder@ubuntu:~/Desktop$ ps -e | grep ssh
1860 ? 00:00:00 ssh-agent
(base) coder@ubuntu:~/Desktop$ apt-get install openssh-server
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
(base) coder@ubuntu:~/Desktop$ sudo apt-get install openssh-server
[sudo] password for coder:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-5.10.0-1029-oem linux-image-5.10.0-1029-oem linux-modules-5.10.0-1029-oem
linux-oem-5.10-headers-5.10.0-1029
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
ncurses-term openssh-client openssh-sftp-server ssh-import-id
Suggested packages:
keychain libpam-ssh monkeysphere ssh-askpass molly-guard
The following NEW packages will be installed:
ncurses-term openssh-server openssh-sftp-server ssh-import-id
The following packages will be upgraded:
openssh-client
1 upgraded, 4 newly installed, 0 to remove and 160 not upgraded.
Need to get 1,359 kB of archives.
After this operation, 6,010 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openssh-client amd64 1:8.2p1-4ubuntu0.3 [671 kB]
Get:1 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openssh-client amd64 1:8.2p1-4ubuntu0.3 [671 kB]
Get:1 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openssh-client amd64 1:8.2p1-4ubuntu0.3 [671 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu focal/main amd64 ncurses-term all 6.2-0ubuntu2 [249 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openssh-sftp-server amd64 1:8.2p1-4ubuntu0.3 [51.5 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 openssh-server amd64 1:8.2p1-4ubuntu0.3 [377 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu focal/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB]
Fetched 1,261 kB in 1min 37s (13.0 kB/s)
Preconfiguring packages ...
(Reading database ... 326835 files and directories currently installed.)
Preparing to unpack .../openssh-client_1%3a8.2p1-4ubuntu0.3_amd64.deb ...
Unpacking openssh-client (1:8.2p1-4ubuntu0.3) over (1:8.2p1-4ubuntu0.2) ...
Selecting previously unselected package ncurses-term.
Preparing to unpack .../ncurses-term_6.2-0ubuntu2_all.deb ...
Unpacking ncurses-term (6.2-0ubuntu2) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a8.2p1-4ubuntu0.3_amd64.deb ...
Unpacking openssh-sftp-server (1:8.2p1-4ubuntu0.3) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a8.2p1-4ubuntu0.3_amd64.deb ...
Unpacking openssh-server (1:8.2p1-4ubuntu0.3) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_5.10-0ubuntu1_all.deb ...
Unpacking ssh-import-id (5.10-0ubuntu1) ...
Setting up openssh-client (1:8.2p1-4ubuntu0.3) ...
Setting up ssh-import-id (5.10-0ubuntu1) ...
Attempting to convert /etc/ssh/ssh_import_id
Setting up ncurses-term (6.2-0ubuntu2) ...
Setting up openssh-sftp-server (1:8.2p1-4ubuntu0.3) ...
Setting up openssh-server (1:8.2p1-4ubuntu0.3) ...
Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:/D0jMJSfZT0pOh4SjzkDSBMPv6gPrLSZg5EeiEFY0R4 root@ubuntu (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:NCaLjTCdbBQYJoqLUIMC+eMO6TJamAKC1udUInKkgNE root@ubuntu (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:knQNCadoS3qxutGBj6dX9KAHR8liUTV9Oqdpj6BB+Js root@ubuntu (ED25519)
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
rescue-ssh.target is a disabled or a static unit, not starting it.
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ufw (0.36-6) ...
(base) coder@ubuntu:~/Desktop$ ps -e | grep ssh
1860 ? 00:00:00 ssh-agent
77543 ? 00:00:00 sshd
paramiko 连接本地的ssh
import paramiko
client = paramiko.SSHClient()
# Set policy to use when connecting to servers without a known host key.
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# 连接
# 此处用的是传统的用户名和密码,而非密钥
client.connect("localhost", 22, "coder", "coder")
# 发送命令
stdin, stdout, stderr = client.exec_command("pwd")
print(stdin)
print(stderr)
for line in stdout.readlines():
print(line)
except Exception as err:
print(err)
finally:
client.close()
运行效果
/home/coder/anaconda3/bin/python3.8 /home/coder/PycharmProjects/pythonProject3/main.py
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x5866f7f0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x5866f7f0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
/home/coder
Process finished with exit code 0
学习推荐
- Python文档 - English
- Python文档 - 中文
- Python规范 PEP
- Python规范 google版
- Python 源码
- Python PEP
- 优麒麟
- 掘金平台
- gitee平台
Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。
推荐阅读
-
用Python自动操作工具paramiko:通过SSH连接并远端执行服务器命令的第三方库指南
-
如何使用Python轻松实现连接SSH并从服务器下载日志文件的操作指南
-
使用Python的Paramiko库实现SSH连接操作指南
-
如何使用Python Paramiko库实现实时SSH远程连接操作指南
-
使用Python(paramiko库)连接Linux服务器的方法
-
如何使用Python的Paramiko库将本地文件夹传输至远程服务器目录
-
用Python实现SSH连接:Paramiko与Fabric库的实践指南
-
使用Python 3的Paramiko库实现SSH远程操作:运行命令、传输文件(上传与下载)
-
使用Python 3的paramiko库连接本地SSH服务器指南
-
使用Python的Paramiko库实现远程SSH连接,执行nohup命令的方法