PYTHON:paramikoリモート操作の使用方法
4927 ワード
1.Paramiko is a python's modules for link remote server.The following two methods are about how to use Paramiko link remote servers
The first method
The second line code useage allow connecting unknow hosts in know-hosts file in above.
2.Example,Next code show the function about login server and excute a command and print into client.
The usual command:
So we nearly do nothing face to more servers if you use the python code
3.Realize download file
4.Realize upload file
The first method
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(" IP ",22," ", " ")
The second line code useage allow connecting unknow hosts in know-hosts file in above.
2.Example,Next code show the function about login server and excute a command and print into client.
- #!/usr/bin/python
- import paramiko
-
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(" IP ",22," ", " ")
- stdin, stdout, stderr = ssh.exec_command(" ")
- print stdout.readlines()
- ssh.close()
The usual command:
df:
uptime:
cat:
mv/cp/mkdir/rmdir:
/sbin/service/ xxxservice start/stop/restart: 、 、
netstat -ntl |grep 8080: 8080
nc -zv localhost :
find / -name XXX:
...
So we nearly do nothing face to more servers if you use the python code
3.Realize download file
- #!/usr/bin/python
- import paramiko
-
- t = paramiko.Transport((“ ”,” ”))
- t.connect(username = “ ”, password = “ ”)
- sftp = paramiko.SFTPClient.from_transport(t)
- remotepath=’/var/log/system.log’
- localpath=’/tmp/system.log’
- sftp.get(remotepath, localpath)
- t.close()
4.Realize upload file
- #!/usr/bin/python
- import paramiko
-
- t = paramiko.Transport((“ ”,” ”))
- t.connect(username = “ ”, password = “ ”)
- sftp = paramiko.SFTPClient.from_transport(t)
- remotepath=’/var/log/system.log’
- localpath=’/tmp/system.log’
- sftp.put(localpath,remotepath)
- t.close()