Paramiko module is a relatively powerful SSH connection module. I have used Paramiko to connect to the Linux machine before. The following is a DEMO to determine whether the remote machine has a certain file:
import paramiko
def ssh_file_isexist(app_kind, file_key, is_exist):
# Create SSH objects
ssh = paramiko.SSHClient()
# Allow the host that is not connected in the know_hosts file
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect to the server, hostname is an IP address, username, password to the server account password
ssh.connect(hostname='', port=22, username='', password='')
# Create a SFTP session and return a SFTP object based on the current connection to upload operation of the file
sftp_client = ssh.open_sftp()
# 7 7 7 file
ssh.exec_command('ls -t')
file_ = sftp_client.listdir("/data/ccloud-host-volume/app/edgelink/staticv2/%s" % app_kind)
# is_exist = 1 Judging file existence
if is_exist:
if file_key in file_:
return True
# is_exist = 0 judgment file does not exist
else:
if file_key not in file_:
return True
ssh.close()
I haven’t tried it, the information collected online
import paramiko
t = paramiko.Transport(("Host","Port"))
t.connect(username = "Username", password = "password")
#t.connect (username = "Username", password = "password", hostkey = "key") #The host needs the key to need the key
First of all
two or more hosts need to achieve densely logging:
Assuming host1 ip: 192.168.0.1, host2 IP: 192.168.0.2
- Run on Host1, SSH -Keygen -T RSA, Enter all the way, this will generate .ssh directory in your user root directory, which contains your root user’s public key file ID_RSA.PUB and private key files
- ID_RSA runs on host1, ssh-copy-id[email protected], when you need to confirm, enter yes, and then enter the root user password of Host2. This order will copy your public key to Authorized_keys, the trusted key file of Host2. When you log in to Host2 with the private key of host1, host2 will use the host1 public key stored in Authorized_keys for authentication.
- Run SSH in host1[email protected], if there is no error, it proves that you can make host1 password -free login Host2.
(Use EXIT to withdraw from Host2 to log in, will return to host1)
Then install the Paramiko library on Host1,
pip install paramiko -y
and then create a python file, the content is as follows
import paramiko
if __name__ == "__main__":
remote_host = '192.168.0.2'
ssh_port = 22
user_name = 'root'
# Load the RSA key
private_key_file = '/root/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(private_key_file)
# Upload file to remote host
t = paramiko.Transport((remote_host, ssh_port))
t.connect(username=user_name, pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
# Upload the file in the current directory to the/root/directory of the remote host
# can also specify the absolute path directly
sftp.put("test.txt", "test.txt")
t.close()
WINRM is the remote execution command of the Windows server,
Below a demo;
wintest = winrm.Session('http://{}:{}/wsman'.format(ip,port),auth=(device_user,device_pass))
res = wintest.run_cmd('star.exe')
print(res.std_err,res.std_out)
However, when this library is used, the device needs to be connected to the device to open: WinRM service
as follows:
1, check whether the winrm service starts
winrmenumerate winrm/config/listener # There is no return value, it means that it has not started
2, winrm service for basic configuration
WinRM QuickConfig will return some parameters after success....,Go to set the network connection as a proprietary choice Y,n Click y3, View Winrm Lintener
winrm e winrm/config/Listener Return to some parameters, protocols and other parameters4, configure Authwinrmset winrm/config/service/auth @{
Basic="true"}
5, the encryption method for the WINRM service configuration is allowed to allow non -encryption
winrmset winrm/config/service @{
AllowUnencrypted="true"}
6, see if it starts
winrmenumerate winrm/config/listener
7, authorized designated IP
winrm s winrm/config/Client @{
TrustedHosts="172.*"} # The author can be connected to the IP of this machine through this command