-
Notifications
You must be signed in to change notification settings - Fork 0
/
16. SSH.py
22 lines (15 loc) · 1.13 KB
/
16. SSH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# To connect to a remote server using SSH in Python, you can use the paramiko library. Here is an example code snippet that shows how to establish an SSH connection and execute a command on a remote server:
import paramiko
# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='remote.server.com', username='username', password='password')
# Execute command on remote server
stdin, stdout, stderr = ssh.exec_command('ls -l')
for line in stdout:
print(line.strip())
# Close SSH connection
ssh.close()
# In this code, we first import the paramiko library, which provides an SSH client implementation in Python. We then establish an SSH connection to a remote server by specifying the server hostname, username, and password.
# Once the connection is established, we can execute a command on the remote server using the exec_command method of the SSHClient object. In this example, we execute the ls -l command, which lists the files in the current directory on the remote server.
# Finally, we close the SSH connection using the close method of the SSHClient object.