#!/usr/bin/env python # coding=utf-8 import paramiko import os,sys,time # define baolei blip = "188.188.22.225" bluser = "root" blpassword = "1233434" # define server info hostname = "112.74.96.65" username = "root" password = "ddddddd" port = 22 passinfo = ‘\‘s password: ‘ paramiko.util.log_to_file(‘syslogin.log‘) # login blhost ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=blip,username=bluser,password=blpassword) # Create session ,Open a channel channel = ssh.invoke_shell() channel.settimeout(60) buff = ‘‘ resp = ‘‘ # start login in channel.send(‘ssh ‘ + username + ‘@‘ +hostname + ‘\n‘) while not buff.endswith(passinfo): try: resp = channel.recv(9999) except Exception, e: print ‘Error info:%s connection time.‘ %(str(e)) channel.close() ssh.close() sys.exit() buff += resp if not buff.find(‘yes/no‘) == -1 : channel.send(‘yes\n‘) buff = ‘‘ channel.send( password+‘\n‘ ) buff = ‘‘ while not buff.endswith(‘# ‘): resp = channel.recv(9999) if not resp.find(passinfo) == -1: print ‘Error info: Authentication failed.‘ channel.close() sys.exit() buff += resp channel.send( ‘ifconfig\n free -m \n df -TH \n ‘) buff = ‘‘ try: while buff.find( ‘# ‘) == -1: resp = channel.recv(9999) buff += resp except Exception, e: print "error info:"+str(e) print buff channel.close() ssh.close
本文出自 “Smurf Linux运维” 博客,请务必保留此出处http://jin771998569.blog.51cto.com/2147853/1658562
原文地址:http://jin771998569.blog.51cto.com/2147853/1658562