Created
July 5, 2022 22:22
-
-
Save ashitaka1963/51e3b156551d5f47491a740c9346c2c7 to your computer and use it in GitHub Desktop.
[SSH接続]paramikoを使用したSSH接続 #paramiko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paramiko | |
with paramiko.SSHClient() as client: | |
HOSTNAME = '127.0.0.1' | |
USERNAME = 'ashitaka1963' | |
KEY_FILENAME = 'C:\\ssh\\wsl_ubuntu' | |
PASSWORD = 'password' | |
LINUX_COMMAND = 'pwd' # カレントディレクトリを表示 | |
# SSH接続 | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
# 1. パスワード認証認証方式 | |
# client.connect(hostname=HOSTNAME, port=22, username=USERNAME, password=PASSWORD) | |
# 2. 公開鍵認証方式 | |
client.connect(hostname=HOSTNAME, port=22, username=USERNAME, key_filename=KEY_FILENAME) | |
# コマンド実行 | |
stdin, stdout, stderr = client.exec_command(LINUX_COMMAND) | |
for line in stdout: | |
print(line, end='') #/home/ashitaka1963 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment