1.with open write files to the local
2.paramiko SFTPClient salt will push the files to the server
3.paramiko SSHClient by salt-cp will be distributed to the target file server
. 1 . With Open (filePath, ' W ' ) AS F: # filePath local file path, w write mode f.write (Content) return filePath
2 . Import paramiko # . 1 connected to client transport = paramiko.Transport ((ssh_url, ssh_port)) transport.connect (username = ssh_user, password = ssh_pwd) # 2 defines the end of the interaction with the customer as just defined parameters passed to transport him = SFTP paramiko.SFTPClient.from_transport (Transport) # sftp.put (R & lt '/ the Users / mnt / {}'. the format (SCRIPT_NAME), '/mnt/{}'.format(script_name)) sftp.put (local path + ' {} ' .format (file name), the target path + ' {} ' .format (file name)) # 5 closes the connection transport.close ()
3 .
# Instantiate a SSH SSH = paramiko.SSHClient () # Set the host can not connect khost_key ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # create a connection ssh.connect (hostname = ssh_url , Port = ssh_port, username = ssh_user , password = ssh_pwd ) # Run, Salt salt_minion_hostname local_url -L-CP / filename TARGET_URL stdin, stdout, stderror = ssh.exec_command ( ' Salt localhost.localdomain -L-CP / mnt / ' + SCRIPT_NAME + ' / mnt ' ) # printout Print (stdout.read ()) # Close the connection ssh.close ()
4. The control terminal via the remote server cmd paramiko module execution command git
Import paramiko # DEF parmiko_con (): # instantiate a SSH SSH = paramiko.SSHClient () # Set the host can not connect khost_key ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # create a connection ssh.connect (hostname = ssh_url , = ssh_port Port, ssh_user = username, password = ssh_pwd ) # Run # cmd = 'cd / mnt / test_pro / opsmanage / && git shortlog -2' # View recently submitted two versions of executed cmd there is no problem, but in the function does not run out in response, git log command to exit required by the q # cmd = 'CD / mnt / test_pro / opsmanage / && Git Rev-Master List' Master # commit record flashback branch arranged cmd = 'cd / mnt / test_pro / opsmanage / && git log --since 1.weeks = '# see nearly a week of submission, only a list of pieces of data need to be processed, need venue: HTTPS: //www.cnblogs. COM / Lutt / P / 12037454.html # cmd = 'CD / mnt / test_pro / opsmanage / Rev && Git --all 2 -n-List' # historical version to reverse only two records (ID show only the commit) # cmd = 'cd / mnt / opsManage / && git branch -a' # view all current branch, git branch view the current branch # cmd = 'CD / mnt / test_pro / opsmanage / && Git Tag -l' # view all current Tag stdin, stdout, stderror = ssh.exec_command (cmd) # printout List2 = [] RET = stdout.read (). decode ( ' UTF-. 8 ' ) List2 = RET.split ( ' \ n ') Print (List2) # close the connection ssh.close () IF the __name__ == ' __main__ ' : parmiko_con () Print ( ' OK ' )