文章
2022/1/25
Python3打开新shell并进行多次交互
这时候有人要说了,执行shell用os.system()就行了嘛
需要返回值用subprocess.Popen().communicate()嘛
但是如果需要多次交互,就需要使用下面这种方法
import subprocessimport osssh = subprocess.Popen('ssh root@192.168.100.52', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)os.read(ssh.stdout.fileno(), 10240)ssh.stdin.write('ls\')ssh.stdin.flush()print(os.read(ssh.stdout.fileno(), 10240))ssh.stdin.write('pwd\')ssh.stdin.flush()print(os.read(ssh.stdout.fileno(), 10240))ssh.stdin.write('whoami\')ssh.stdin.flush()print(os.read(ssh.stdout.fileno(), 10240))ssh.stdin.write('exit\')ssh.stdin.flush()如果咩咩的文章对您有帮助,您可以 请我喝牛奶