SoFunction
Updated on 2024-11-14

Python implementation of the remote file automatically packaged and download function example

In this article, the example of the Python implementation of the remote file automatically packaged and download function. Shared for your reference, as follows:

fig. a point of reference

In a Linux cluster operation, it is often necessary to execute Linux commands remotely in batches and synchronize files in both directions.

This example accomplishes this by using the idea of executing ssh, scp commands using the spawn() method.

II Code

import pexpect
import sys
ip="192.168.0.104"
user="root"
passwd="123456"
target_file="/data/"
child = ('/usr/bin/ssh', [user+'@'+ip])
fout = file('','w')
 = fout
try:
  ('password: ')
  (passwd)
  ('#')
  ('tar -czf /data/ '+target_file)
  ('#')
  print 
  ('exit')
  ()
except :
  print "expect EOF1"
except :
  print "expect TIMEOUT1"
child = ('/usr/bin/scp', [user+'@'+ip+':/data/','/home'])
fout = file('','a')
 = fout
try:
  ('(?i)password')
  (passwd)
  ()
except :
  print "expect EOF2"
except :
  print "expect TIMEOUT2"

III. Operation

[root@localhost pymaintain]# python 5_3_3.py
tar -czf /data/ /data/
tar: Removing leading `/' from member names

IV Log output

[root@localhost pymaintain]# cat 
Kernel \r on an \m
[email protected]'s password: 123456
Last login: Sun Feb 24 16:20:25 2019 from 192.168.0.120
hello cakin24!
[root@slave2 ~]# tar -czf /data/ /data/
tar -czf /data/ /data/
tar: Removing leading `/' from member names
[root@slave2 ~]# exit
Kernel \r on an \m
[email protected]'s password: 123456
                  100% 115  40.3KB/s  00:00

Readers interested in more Python related content can check out this site's topic: theSummary of Python file and directory manipulation techniques》、《Python Socket Programming Tips Summary》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope the description of this article will help you in Python programming.