欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python paramiko使用方法代码汇总

程序员文章站 2022-09-05 15:32:33
1、用户名、密码登陆方式import paramikoparamiko.util.log_to_file('paramiko.log') # 记录日志文件ssh = paramiko.sshclien...

1、用户名、密码登陆方式

import paramiko
paramiko.util.log_to_file('paramiko.log') # 记录日志文件
ssh = paramiko.sshclient()
try:
  ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
  ssh.connect('139.xx.xx.xx', username='work', password='***')
  cmd = 'ls' # 需要执行的linux命名
  stdin, stdout, stderr = ssh.exec_command(cmd) #执行命令后的结构
  print(stdout.readlines())
  print(stdout.read().decode())
except exception as e:
  print("%s:%s" % (e.__class__, e))
finally:
  # 关闭
  ssh.close()

2、免密登陆方式

import paramiko
ssh = paramiko.sshclient()
ssh_private_key ='/users/xueerhuan/.ssh/id_rsa' #本地密钥文件路径

try:
  key = paramiko.rsakey.from_private_key_file(ssh_private_key) # 无解密密码时
  #key = paramiko.rsakey.from_private_key_file(ssh_private_key, password='******') # 有解密密码时,

  ssh.load_system_host_keys() #通过known_hosts 方式进行认证可以用这个,如果known_hosts 文件未定义还需要定义 known_hosts
  #ssh.set_missing_host_key_policy(paramiko.autoaddpolicy()) # 通过公共方式进行认证 (不需要在known_hosts 文件中存在)
  ssh.connect(hostname='139.xx.xx.xx', port=22, username='root', pkey=key)
  stdin, stdout, stderr = ssh.exec_command("ps")
  # 获取命令结果
  result = stdout.read()
  # 打印输出
  print(result.decode())
except exception as e:
  print("%s:%s" % (e.__class__, e))
finally:
  # 关闭
  ssh.close()

注意:生成密码的方法

a、进入本地 ssh文件夹 cd .ssh/

b、使用ssh-keygen生产本地公钥和私钥 ssh-keygen

xueerhuan@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub

c、将生成的id_rsa.pub文件中的内容copy到目标机的.ssh/authorized_keys中就可以了,如果没有authorized_keys,自己创建。但是要注意authorized_keys的权限一般是600

或者直接在本地使用一条命令也可以实现公钥的复制,ssh-copy-id后面接入的用户就是要支持免密登录的用户。

morra@ubuntu:~/.ssh$ ssh-copy-id "morra@192.168.1.42"
/usr/bin/ssh-copy-id: info: source of key(s) to be installed: "/home/morra/.ssh/id_rsa.pub"
the authenticity of host '192.168.1.42 (192.168.1.42)' can't be established.
ecdsa key fingerprint is sha256:/ufx+/oltdsyy7vsdk4kdu9xjsbp6zhonraf2jjt0gi.
are you sure you want to continue connecting (yes/no)? n^h
please type 'yes' or 'no': yes
/usr/bin/ssh-copy-id: info: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
password:

number of key(s) added: 1

now try logging into the machine, with:  "ssh 'morra@192.168.1.42'"  and check to make sure that only the key(s) you wanted were added.

#去目标机器下,检查authorized_keys文件
localhost:.ssh morra$ cat authorized_keys 

3、密码上传文件

import os
import paramiko
ssh = paramiko.sshclient()
ssh_private_key ='/users/xueerhuan/.ssh/id_rsa' #本地密钥文件路径
key = paramiko.rsakey.from_private_key_file(ssh_private_key)
paramiko.util.log_to_file('paramiko.log')

ssh = paramiko.sshclient()
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
ssh.connect('139.xx.xx.xx', username='root', password='***')
t = ssh.get_transport()
sftp = paramiko.sftpclient.from_transport(t)
d = sftp.put("mm.txt", "/home/work/.ssh/mm.txt")
print(d)

4、免密上传文件

import os
import paramiko
ssh = paramiko.sshclient()
ssh_private_key ='/users/xueerhuan/.ssh/id_rsa' #本地密钥文件路径
key = paramiko.rsakey.from_private_key_file(ssh_private_key)
paramiko.util.log_to_file('paramiko.log')
ssh = paramiko.sshclient()
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
ssh.connect(hostname='139.xx.xx.xx', port=22, username='root', pkey=key)
t = ssh.get_transport()
sftp = paramiko.sftpclient.from_transport(t)
d = sftp.put("mm.txt", "/home/work/.ssh/mm.txt")
print(d)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: Python paramiko