target: There are two machines, A and B, and I want to operate the script on machine A on B.
Workaround: Use Paramiko to operate the remote machine
1. Installing Paramiko Install the third-party package "PIP3 install Paramiko" If the PIP accesses the foreign site total timeout, you can use the domestic mirror, using the-I designation,--trusted-host pypi.douban.com Trust the Mirror sourcepip3 Install paramiko-i http://pypi.douban.com/--trusted-host pypi.douban.com pipy Domestic Mirror currently has: http://pypi.douban.com/ watercress http://pypi.hustunique.com/ Huazhong University of Technology http://pypi.sdutlinux.org/ Shandong University of Technology http://pypi.mirrors.ustc.edu.cn/ China University of Science and Technology
2.paramiko UsageReference Documentation:http://blog.csdn.net/kellyseeme/article/details/51352305
First step: Connect via account password
def Connect (Host,username,password): = Paramiko. Sshclient () ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) try: ssh.connect (host,username=username,password=password, allow_agent=True) return ssh except: return None
host for the machine to be connected ip,username and password for the login machine account password
Step Two: Execute command on remote machine (shell command)
def exec_commands (conn,cmd): = Conn.exec_command (cmd) results=stdout.read () +stderr.read () print( Results) return results
stdin: Standard inputstdout: Standard correct outputstderr: standard error Outputget the corresponding output via Stdout.read ()
Step Three: Transfer files between and remote machines
defFtpssh (frompath,topath,method="Get"): T=Paramiko. Transport (remote machine IP) t.connect (username= Remote machine login user name, password =remote machine login password) sftp=Paramiko. Sftpclient.from_transport (t)ifmethod = ="Get":#copy a remote file to a localSftp.get (Frompath, Topath)elifmethod = ="put":#Copy the local file to the remote machinesftp.put (Frompath, Topath) t.close ()
method=get, download the remote machine file to the local pathmethod=put, upload the local file to the remote machine corresponding path
Other Notes:1. Operate the Python3 file of the remote machine via Paramiko, python3 the script to read and write files.Obviously Python3 result has coding problem, so changed the remote machine script, read and write the file when the strong specified encoding format is Utf-8with open(
"file","W",encoding=' Utf-8 ') as F: 2. I execute the command through the second step, not reading the results of the console output, so by redirecting the output to a local file, and then passing the file through the remote machine to implement the file to localThis will directly read the local file. For example: I execute the appium test script, I will output some operation results in the console, I want to obtain these results
cmd="source ~/.bash_profile;/usr/local/bin/python3./test.py >>./log.txt 2>&1" "/testsuit/temptestcaseslist.txt" "./ Temptestcaseslist.txt")
3. When executing the remote machine script, I found that some environment variables were not read, and I loaded the environment variable file of the remote machine in the script again.Source ~/.bash_profile
4. Each remote command execution is handled separately and will go back to the initial directory after processing. so there is the operation of the CD directory, it is better to put multiple statements in the same command, different commands separated by semicolons
Python3 using Paramiko to operate the remote machine