- 论坛徽章:
- 0
|
import paramiko, threading, sys, time
host = 'host'
port = 22
usr = 'username'
pwd = 'password'
timeout = 3
trans = paramiko.Transport((host, port))
trans.connect(username=usr, password=pwd)
chan = trans.open_session()
chan.get_pty()
chan.invoke_shell()
def func(channel, f):
while True:
txt = channel.recv(256)
if not txt:
f.flush()
break
f.write(txt)
f.flush()
def cmd(cmd_str):
chan.sendall(cmd_str + '\n')
time.sleep(timeout)
t = threading.Thread(target=func, args=(chan, sys.stdout))
t.setDaemon(1)
t.start()
cmd('pwd')
cmd('ls -lt')
cmd('cd some_dir)
cmd('pwd')
cmd('ls -lt')
cmd('more some_script.py')
cmd(' ')
trans.close() |
|