python控制伺服器

我的氣質發表於2019-01-28

SSH
安全的shell secure shell
一種協議:傳輸,使用者人認證,連線
連線伺服器
加密傳輸,遠端執行命令
win10 已經內建支援
使用公鑰+私鑰,安全登入

在這裡插入圖片描述
在終端中輸入ssh-keygen 預設不輸入密碼自動生成公鑰和私鑰

在這裡插入圖片描述
將id_rsa.pub 放到linux伺服器上
本地id_rsa.pub —>雲端authorized_keys

vim ~/.ssh/authorized_keys

在本地終端使用如下命令

ssh root@192.168.1.105

第一次登陸等待確認即可連線
第二次及以後會自動連線

使用subprocess操作 命令

兩個必回的方法

subprocess.run()
根據引數執行命令,返回completedProcess_instance
subprocess.Popen
下面是ipython語言

import os
os.system('pwd')

os.system(‘ls -l’)

cmd = "ls -l"
os.system(cmd)
import subprocess
subprocess.run('ls')
subprocess.run('pwd')
subprocess.run('df  -h')
subprocess.run(['df','-h'])
subprocess.run('df -h',shell=True)                                      
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G   11G   27G  28% /
devtmpfs        911M     0  911M   0% /dev
tmpfs           920M     0  920M   0% /dev/shm
tmpfs           920M  400K  920M   1% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
tmpfs           184M     0  184M   0% /run/user/0
Out[37]: CompletedProcess(args='df -h', returncode=0)

cp = subprocess.run(['df','-h'])
cp.stdout#無輸出
cp.returncode#輸出0
subprocess.Popen?
subprocess.Popen("ls")

In [47]: subprocess.Popen('ls')                                                  
1.txt				 gyz	     my      wmy
anaconda3			 hdvbncbhsc  python  這個是阿里雲伺服器
Anaconda3-5.3.1-Linux-x86_64.sh  index.jsp   tools
Out[47]: <subprocess.Popen at 0x7f5759e96cc0>
In [48]: subprocess.Popen(['ls','-l'])                                           
total 652364
-rw-r--r--  1 root root        48 Dec 31 17:52 1.txt
drwxr-xr-x 23 root root      4096 Dec 18 10:57 anaconda3
-rw-r--r--  1 root root 667976437 Nov 20 04:00 Anaconda3-5.3.1-Linux-x86_64.sh
drwxr-xr-x  2 root root      4096 Dec 27 13:38 gyz
-rw-r--r--  1 root root      1072 Dec 20 20:49 hdvbncbhsc
-rw-r--r--  1 root root        17 Nov  3 17:29 index.jsp
drwxr-xr-x  4 root root      4096 Dec 19 17:06 my
drwxr-xr-x  5 root root      4096 Jan 18 10:47 python
drwxr-xr-x  2 root root      4096 Nov  4 19:37 tools
drwxr-xr-x  2 root root      4096 Dec 31 16:47 wmy
-rw-r--r--  1 root root         0 Jan 28 12:02 這個是阿里雲伺服器
Out[48]: <subprocess.Popen at 0x7f575879e6a0>

cp.stdout
#無輸出結果
In [50]: cp = subprocess.run(['df','-h'],stdout=subprocess.PIPE)                 

In [51]: cp.stdout                                                               
Out[51]: b'Filesystem      Size  Used Avail Use% Mounted on\n/dev/vda1        40G   11G   27G  28% /\ndevtmpfs        911M     0  911M   0% /dev\ntmpfs           920M     0  920M   0% /dev/shm\ntmpfs           920M  400K  920M   1% /run\ntmpfs           920M     0  920M   0% /sys/fs/cgroup\ntmpfs           184M     0  184M   0% /run/user/0\n'
cp = subprocess.run(['df','-h'], stdout=subprocess.PIPE, encoding='utf8')
cp.stdout
cp = subprocess.run(['ls','-l'], stdout=subprocess.PIPE, encoding='utf8')
cp.stdout
cp.stdout.splitlines()
In [52]: cp.stdout.splitlines()                                                  
Out[52]: 
[b'Filesystem      Size  Used Avail Use% Mounted on',
 b'/dev/vda1        40G   11G   27G  28% /',
 b'devtmpfs        911M     0  911M   0% /dev',
 b'tmpfs           920M     0  920M   0% /dev/shm',
 b'tmpfs           920M  400K  920M   1% /run',
 b'tmpfs           920M     0  920M   0% /sys/fs/cgroup',
 b'tmpfs           184M     0  184M   0% /run/user/0']



In [53]: cp = subprocess.run('ls -l', stdout=subprocess.PIPE, encoding='utf8', shell=True)                                                               
In [56]: cp.stdout.splitlines()                                                  
Out[56]: 
['total 652364',
 '-rw-r--r--  1 root root        48 Dec 31 17:52 1.txt',
 'drwxr-xr-x 23 root root      4096 Dec 18 10:57 anaconda3',
 '-rw-r--r--  1 root root 667976437 Nov 20 04:00 Anaconda3-5.3.1-Linux-x86_64.sh',
 'drwxr-xr-x  2 root root      4096 Dec 27 13:38 gyz',
 '-rw-r--r--  1 root root      1072 Dec 20 20:49 hdvbncbhsc',
 '-rw-r--r--  1 root root        17 Nov  3 17:29 index.jsp',
 'drwxr-xr-x  4 root root      4096 Dec 19 17:06 my',
 'drwxr-xr-x  5 root root      4096 Jan 18 10:47 python',
 'drwxr-xr-x  2 root root      4096 Nov  4 19:37 tools',
 'drwxr-xr-x  2 root root      4096 Dec 31 16:47 wmy',
 '-rw-r--r--  1 root root         0 Jan 28 12:02 這個是阿里雲伺服器']


check=True


In [63]: cp = subprocess.run('df -dsfh', stdout=subprocess.PIPE, encoding='utf8',
    ...:  shell=True, check=True)                                                
df: invalid option -- 'd'
Try 'df --help' for more information.
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-63-abf39d784c7e> in <module>
----> 1 cp = subprocess.run('df -dsfh', stdout=subprocess.PIPE, encoding='utf8', shell=True, check=True)

~/anaconda3/envs/gyz/lib/python3.7/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    479         if check and retcode:
    480             raise CalledProcessError(retcode, process.args,
--> 481                                      output=stdout, stderr=stderr)
    482     return CompletedProcess(process.args, retcode, stdout, stderr)
    483 

CalledProcessError: Command 'df -dsfh' returned non-zero exit status 1.

重點
如下的命令 在執行6之後緊接著執行7,再一次執行8的時候結果就已經丟失了

In [6]: proc = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE)              
In [7]: proc.stdout.read().decode('utf8')                                        
Out[7]: 'total 652364\n-rw-r--r--  1 root root        48 Dec 31 17:52 1.txt\ndrwxr-xr-x 23 root root      4096 Dec 18 10:57 anaconda3\n-rw-r--r--  1 root root 667976437 Nov 20 04:00 Anaconda3-5.3.1-Linux-x86_64.sh\ndrwxr-xr-x  2 root root      4096 Dec 27 13:38 gyz\n-rw-r--r--  1 root root      1072 Dec 20 20:49 hdvbncbhsc\n-rw-r--r--  1 root root        17 Nov  3 17:29 index.jsp\ndrwxr-xr-x  4 root root      4096 Dec 19 17:06 my\ndrwxr-xr-x  5 root root      4096 Jan 18 10:47 python\ndrwxr-xr-x  2 root root      4096 Nov  4 19:37 tools\ndrwxr-xr-x  2 root root      4096 Dec 31 16:47 wmy\n-rw-r--r--  1 root root         0 Jan 28 12:02 這個是阿里雲伺服器\n'

In [8]: proc.stdout.read().decode('utf8')                                                                                                                    
Out[8]: ' '


###  幾個最重要的引數
###  三個選修方法
###  取得控制檯結果
###  協程裡的subprocess

paramiko連線伺服器

相關文章