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
- 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:查找某文件
...
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()