Using python to remotely log on to the host and execute commands or upload and download files through sftp, there is a good module paramiko module to demonstrate these functions, which is very convenient to use, you can learn. I wrote several small programs to illustrate how to use this module.
1: connect to a remote linux host and execute the command
- #!/usr/bin/env python
- import paramiko
- hostname='192.168.0.102'
- username='root'
- password='abc'
- port=22
- paramiko.util.log_to_file('paramiko.log')
- s=paramiko.SSHClient()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(hostname = hostname,port=port,username=username, password=password)
- stdin,stdout,stderr=s.exec_command('free;df -h')
- print stdout.read()
- s.close()
The execution result is as follows:
- total used free shared buffers cached
- Mem: 2074940 2057420 17520 0 42416 1867968
- -/+ buffers/cache: 147036 1927904
- Swap: 2096472 240 2096232
- Filesystem Size Used Avail Use% Mounted on
- /dev/sda1 30G 12G 17G 42% /
- none 1014M 0 1014M 0% /dev/shm
- /dev/sda3 2.0G 289M 1.6G 16% /var
- /dev/sdb1 135G 14G 115G 11% /data
- /dev/sdc1 135G 127G 880M 100% /data1
- /dev/sdd1 135G 99G 30G 78% /data2
2: connecting to a remote linux host to upload and download the file paramiko module is implemented using SFTP protocol)
- #! /Usr/bin/env python
- Import paramiko, datetime, OS
- Hostname = '1970. 168.0.102'
- Username = 'root'
- Password = 'abc123'
- Port = 22
- Local_dir = '/tmp /'
- Remote_dir = '/tmp/test /'
- Try:
- T = paramiko. Transport (hostname, port ))
- T. connect (username = username, password = password)
- Sftp = paramiko. SFTPClient. from_transport (t)
- # Files = sftp. listdir (dir_path)
- Files = sftp. listdir (remote_dir)
- For f in files:
- Print''
- Print '###################################### ###'
- Print 'ininning to download file from % s % s' % (hostname, datetime. datetime. now ())
- Print 'downloading file: ', OS. path. join (remote_dir, f)
- Sftp. get (OS. path. join (remote_dir, f), OS. path. join (local_dir, f) # download
- # Sftp. put (OS. path. join (local_dir, f), OS. path. join (remote_dir, f) # upload
- Print 'Download file success % s' % datetime. datetime. now ()
- Print''
- Print '###################################### ####'
- T. close ()
- Failed t Exception:
- Print "connect error! "
Execution result:
- #########################################
- Beginning to download file from 192.168.0.102 2012-11-05 15:49:01.334686
- Downloading file: /tmp/test/wgetrc Download file success 2012-11-05 15:49:05.955184
- ##########################################
-
- #########################################
- Beginning to download file from 192.168.0.102 2012-11-05 15:49:05.955342
- Downloading file: /tmp/test/xinetd.conf Download file success 2012-11-05 15:49:10.929568
- ##########################################
-
- #########################################
- Beginning to download file from 192.168.0.102 2012-11-05 15:49:10.929740
- Downloading file: /tmp/test/warnquota.conf Download file success 2011-12-05 15:49:14.213570
- ##########################################
There are a lot of usage, specific can see the official documentation: http://www.lag.net/paramiko/docs/
This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1058726