#############################################################安裝paramikopython
[root@LVS1 ~]# yum install gcc
[root@LVS1 ~]#tar -zxvf pycrypto-2.6.1.tar.gz#https://pypi.python.org/pypi/pycrypto服務器
[root@LVS1 ~]#cd pycrypto-2.6.1ssh
[root@LVS1 pycrypto-2.6.1]#python setup.py installide
[root@LVS1 ~]# yum install python-devel測試
[root@LVS1 ~]#tar -zxvf paramiko-1.10.1.tar.gz#https://pypi.python.org/pypi/paramikoui
[root@LVS1 ~]#cd paramiko-1.10.1spa
[root@LVS1 paramiko-1.10.1]# python setup.py install日誌
[root@LVS1 demos]# python demo.py 192.168.1.10#測試utf-8
##################################################################使用密碼登錄input
#!/usr/bin/env python
#coding=utf-8
import paramiko
#遠程服務器
hostname ='192.168.1.10'
#端口
port=22
#用戶名
username='root'
#密碼?
password = '123456'
#建立SSH鏈接日誌文件(只保留前一次鏈接的詳細日誌,之前的日誌會自動被覆蓋
#paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
#容許鏈接不在know_hosts文件中的主機
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#創建SSH鏈接
s.connect(hostname,port,username,password)
stdin,stdout,stderr=s.exec_command('top -bn 1')
#打印標準輸出
print stdout.read()
s.close()
#########################################################使用key登錄
#!/usr/bin/env python
#coding=utf-8
import paramiko
hostname ='192.168.1.11'
port=22
username='root'
key_file='/root/.ssh/id_rsa'
key=paramiko.RSAKey.from_private_key_file(key_file)
#paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys()
s.connect(hostname,port,username,pkey=key)
stdin,stdout,stderr=s.exec_command('df -m')
print stdout.read()
print stderr.read()
s.close()
########################################手動輸入命令屢次執行
#!/usr/bin/env python
#coding=utf-8
import paramiko
hostname ='192.168.1.11'
port=22
username='root'
key_file='/root/.ssh/id_rsa'
key=paramiko.RSAKey.from_private_key_file(key_file)
#paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
#s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys()
s.connect(hostname,port,username,pkey=key)
while True:
cmd=raw_input('cmd:')
if cmd=='quit':break
stdin,stdout,stderr=s.exec_command(cmd)
print stdout.read()
print stderr.read()
s.close()