Use python to update ssh remote code to operate remote server implementation code, pythonssh
Use the python paramiko ssh server and pull the script corresponding to the directory code
Pull. py
Import paramikoimport sysdef sshclient_execmd (hostname, port, username, password, execmd): paramiko. util. log_to_file ("paramiko. log ") s = paramiko. SSHClient () s. set_missing_host_key_policy (paramiko. autoAddPolicy () if (port = 0): s. connect (hostname = hostname, username = username, password = password) else: s. connect (hostname = hostname, port = port, username = username, password = password) stdin, stdout, stderr = s.exe c_command (execmd) stdin. write ("Y") # Generally speaking, the first connection, need a simple interaction. print stdout. read () s. close () def main (server, project): # def main (): server_list = {'000000': {'hostname': '2017. 22.22.22 ', 'username': 'root', 'Password': '123', 'Port': 123456}, '11': {'hostname': '123. 168.1.11 ', 'username': 'root', 'Password': '000000', 'Port': 0} if (server = '000000 '): execmd = "cd/workspace/" + project + "/& git pull" info = OS. popen (execmd ). read () # update local, print info up_list = server_list [server] hostname = up_list ['hostname'] port = up_list ['Port'] username = up_list ['username'] password = up_list ['Password'] execmd = "cd/workspace/" + project + "/& git pull" sshclient_execmd (hostname, port, username, password, execmd) if _ name _ = "_ main _": server = str (sys. argv [1]) project = str (sys. argv [2]) main (server, project)
The above is the source code for updating the project directory pull on the remote server.
/workspace/" + project + "/ && git pull
For example, run'python pull.py 2108 web
'Will be used paramiko.SSHClient
To connect tomain
Functionserver_list list
2108hostname
,username
,password
,port
Parameter. After connecting to the server, runexecmd
. I usedargv
Obtain the input parameters to control the path of the project to be updated. This is a script that uses python ssh remote server and updates the corresponding directory code.
Here I have configured two servers. The 11 server is not used.port
So I made a decision to control whether the connection contains port
Parameter. Otherwise, an error is returned.
if(port==0):
Note that for the first execution, you need to accept the author_key cache and check whether you have the update permission.
Python connects to the remote server using ssh and runs the command code
The following code uses pexpect to generate an ssh process, connect to the remote server, and run the command.
Before using the following program, install the pexpect program through easy_install pexpect.
#!/usr/bin/env python# -*- coding: utf-8 -*-import pexpectdef ssh_cmd(ip, passwd, cmd): ret = -1 ssh = pexpect.spawn('ssh root@%s "%s"' % (ip, cmd)) try: i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5) if i == 0 : ssh.sendline(passwd) elif i == 1: ssh.sendline('yes\n') ssh.expect('password: ') ssh.sendline(passwd) ssh.sendline(cmd) r = ssh.read() print r ret = 0 except pexpect.EOF: print "EOF" ssh.close() ret = -1 except pexpect.TIMEOUT: print "TIMEOUT" ssh.close() ret = -2 return ret
This is the end. You can refer to the following methods: