# -*- coding: utf-8 -*-
import paramiko
import os
def ssh_command(command):
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
client.connect('ip',port=22,username='root',password="password")
stdin,stdout,stderr = client.exec_command(f"{command}",timeout=60)
result = stdout.read()
result = result.decode('utf-8');
print(result)
client.close()
def sftp_upload_file(local_file,server_file):
transport = paramiko.Transport(('ip', 22))
transport.connect(username='root', password='root.123')
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(local_file,server_file)
local_path = os.getcwd()
ssh_command("""
cat > 1.sh <<EOF
# create 1.sh
echo \`date\`
EOF
""")
sftp_upload_file(f'{local_path}/2.txt',"/root/1.txt")
ssh_command('ls')