#!/usr/bin/env python#coding:utf-8 "paramiko install one, install, download 1, download and install pycrypto-2.6.1.tar.gz (apt-get install python-dev) Unzip, enter, Python setup.py build "compile", Python setup.py install Install ----import crypto 2, download and install paramiko-1.10.1.tar.gz extract, enter, python setup.py build "Compiling",python setup.py install "Installation"---" import paramiko" # Specifies the user name password to log import paramiko# instantiate an object Ssh = paramiko. Sshclient () #验证ssh登陆时的yes/nossh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #指定主机的ip Port user name password ssh.connect (' 192.168.1.108 ', 22, ' Alex ', ' 123 ' ) #执行命令stdin, stdout, stderr = ssh.exec_command (' df ') #读取执行结果print stdout.read () # Disconnect Ssh.close () #使用密钥方式登录, private key Login "' Execute python before now do some preparatory work on Linux generate key ssh-keygen -t RSA remote copy its own public key to the other machine ssh-copy-id -i ~/ssh/id_rsa.pub [email protected] "' import paramiko# Specifies the private key path Private_key_path = '/home/tom/.ssh/id_rsa ' #取出私钥key = paramiko. Rsakey.from_private_key_file (Private_key_path) #下面和使用密码登录步骤一样ssh = paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) ssh.connect (' 182.92.219.96 ', 22, ' Wupeiqi ', pkey=key) stdin, stdout, stderr = ssh.exec_command (' DF ') Print stdout.read () Ssh.close ();
This article comes from the "Python&linux's blog" blog, please be sure to keep this source http://topping.blog.51cto.com/12388701/1934468
Using the Python Management Server