运行的指令模板
python test.py start 1 2 3
test.py
def start(*kwargs):
print('start')
print(kwargs)
def run():
print('run')
def end():
print('start')
import importlib
def dynamic_import(module):
return importlib.import_module(module)
if __name__ == '__main__':
module = dynamic_import('ppexpect_test')
getattr(module, sys.argv[1])(sys.argv[2:])
import traceback
import pexpect
from pexpect.popen_spawn import PopenSpawn
try:
child = pexpect.popen_spawn.PopenSpawn('ssh pi@192.168.178.34')
a = child.expect(['password:', 'The authenticity of host'], timeout=300, async=True)
if a == 0:
child.sendline('123456789')
print('this ip has exists in know_host files!')
if a == 1:
print('this ip will be added to know_host files!')
child.sendline('yes')
child.expect('password:')
child.sendline('raspberry')
# send test
child.sendline("echo TestMessage")
child.interact()
except pexpect.EOF:
traceback.print_exc()