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