SSH Batch login upload execute command download file

Source: Internet
Author: User
Tags create directory

Use:

Make the intranet of the Linux host, get the common account password, batch log in, upload the System Information collection script, pull the result of execution back to the local, for analysis.

Disadvantages:

Do not support multi-threaded password must be properly cut to connect port hard coded to 22 if not 22 please modify it yourself

Execution process:

Executing ssh.py root root automatically logs in to SSH, uploads 1.py files from the local file directory to the Linux Server TMP directory, executes/tmp/1.py files, deletes/tmp/1.py files, Download the generated test.txt of the 1.py executed file back to local. Note: The downloaded file name has been hard-coded in 1.py file 28 lines, if the downloaded files are other names please modify (download the file with the generated files are mutual two files need to change)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6C/E4/wKioL1VVlyzAg60IAAJAD5i8MRU486.jpg "title=" Qq20150515144325.jpg "alt=" Wkiol1vvlyzag60iaajad5i8mru486.jpg "/>

# -*- coding: utf_8 -*-# date: 2015/5/15# author:sanrimport paramiko, Time,sys,osdef readme ():p rint  ' + '  +  '-'  * 50 +  ' + ' print u ' \ t     python ssh Batch logon script ' Print u ' \t       IP please write iplist.txt ' print u ' \t        time:2015-5-15 ' print u ' \ t         author : sanr ' print  ' + '  +  '-'  *  50 +  ' + ' If len (sys.argv)  != 3:print  u ' [+] Usage: ' +os.path.basename ( Sys.argv[0])  +  " user pass" Print  u ' [+] instance: ' +os.path.basename (sys.argv[0])  +   " root root" Sys.exit () Def upload (HOST,USER,PSWD): Try:ssh = paramiko. Transport ((host,22)) Ssh.connect (USERNAME = USER, PASSWORD = PSWD) sftp =  Paramiko. Sftpclient.from_transport (SSH) sftp.put ("./1.py ","/tmp/1.py ") print  ' [+] ' +host,u ' ssh landed ',#  call execute command function SSH2 (HOST,USER,PSWD) #  Call Create directory function mkdir ( Host) #  download remote host file Sftp.get ("/tmp/test.txt",  "./" +host+ "/result.txt") Print u ' information collection results downloaded successfully ' Ssh.close ( ) Except:passdef ssh2 (HOST,USER,PSWD): Try:ssh = paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (host,22,user,pswd, timeout=5) #stdin, stdout, stderr =  Ssh.exec_command (' Ifconfig;free ') #print  stdout.read () #执行命令   Modify upload 1.py code permissions to execute 1.py scripts for 777      Delete 1.py script Stdin, stdout, stderr = ssh.exec_command (' Chmod 777 /tmp/1.py;/usr /bin/python /tmp/1.py;rm -rf /tmp/1.py ') Ssh.close () except:passdef mkdir (path):     #  Introducing Modules     import os    #  Determining if a path exists      #  exists      True    #  does not exist     false&nbSp;   isexists=os.path.exists (PATH)     #  Judging results      if not isexists:        #  Creating a directory operation function          os.makedirs (path) #  Create directory If it does not exist          print  u '   Create directory success ',        return true     else:        #  If the directory exists, do not create and prompt the directory already exists          print   u ' local directory already exists ',         return falseif __name__== ' __main__ ': README () start_time = time.time () with  Open ('./iplist.txt ',  ' R ')  as f:for ip in  f:host=ip.strip () user=sys.argv[1] Pswd=sys.argv[2]upload (host,user,pswd) print u ' [+]  report   information collected, spents:%d  seconds '  %  ( Time.time ()  - start_time) 


This article is from the "Sanr" blog, make sure to keep this source http://0x007.blog.51cto.com/6330498/1651615

SSH Batch login upload execute command download file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.