使用python paramiko的exec_command无法执行延迟得到结果的命令
时间:2011-10-28
来源:互联网
远程登录成功,一般的如"ls -ltr"之类也可以得到命令相应的结果,但是想要执行延迟得到结果的命令,比如sipp。
无法触发对应的结果。
下面例子中是sipp的主叫侧发起呼叫命令,在被叫侧的sipp进程已经人工开启,理论上只要主叫侧命令执行,被叫侧
至少能得到一条sip消息。
#!/usr/bin/env python
import paramiko
import time
hostname = '10.170.9.35'
port = 22
username = 'root'
password = 'mgcroot'
sipp_command_uac = './sipp.3.1.src/sipp 192.168.55.4:5060 -sf ./scenario/uac_video_amr.xml -s 1411420123456 -i 192.168.55.242 -p 5071 -mi 192.168.55.242 -mp 21310 -aa -r 20 -d 10000 -t u1 -m 1'
ls = 'ls -ltr *.py'
if __name__ == '__main__':
paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
print type(sipp_command_uac)
stdin, stdout, stderr = s.exec_command('cd /home/ezhuhao ;' + sipp_command_uac + '; sleep 12')
print stdout.read()
s.close()
无法触发对应的结果。
下面例子中是sipp的主叫侧发起呼叫命令,在被叫侧的sipp进程已经人工开启,理论上只要主叫侧命令执行,被叫侧
至少能得到一条sip消息。
#!/usr/bin/env python
import paramiko
import time
hostname = '10.170.9.35'
port = 22
username = 'root'
password = 'mgcroot'
sipp_command_uac = './sipp.3.1.src/sipp 192.168.55.4:5060 -sf ./scenario/uac_video_amr.xml -s 1411420123456 -i 192.168.55.242 -p 5071 -mi 192.168.55.242 -mp 21310 -aa -r 20 -d 10000 -t u1 -m 1'
ls = 'ls -ltr *.py'
if __name__ == '__main__':
paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
print type(sipp_command_uac)
stdin, stdout, stderr = s.exec_command('cd /home/ezhuhao ;' + sipp_command_uac + '; sleep 12')
print stdout.read()
s.close()
作者: paul_26piggy 发布时间: 2011-10-28
我也有相同的苦恼,python似乎不能实现,你看我发过的帖子,我用python3.3的subprocess.Popen()操作,它可以传递子进程参数,并可以用PIPE读取子进程输出。可能我的理解有偏差,你试试用下,我也期待结果。
PS:我觉得你应该用list来做命令,不要用带一大堆空格的字符串做命令参数。
PS:我觉得你应该用list来做命令,不要用带一大堆空格的字符串做命令参数。
作者: JacksonLv 发布时间: 2011-10-28
os.system,subprocess.Popen 可以实现本地的延时输出
下面是我写的,不过不知道怎么和远程命令整合到一起
#!/usr/bin/python
#import pexpect
import string, os, sys
import subprocess
import time
queue = True
def displayfile():
myfile = open("/home/ezhuhao/display.log")
input=myfile.readline()
while (input != ''):
print input
input=myfile.readline()
myfile.close()
def dis_screen(filename):
# subprocess.Popen('grep %s /home/ezhuhao/myscreen.txt >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "period" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Peak" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Successful" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Failed" %s >> /home/ezhuhao/display.log' %filename , shell=True)
myfile = open('/home/ezhuhao/display.log' ,'wa')
myfile.write('++++++++++Here is the screen log++++++++++')
myfile.close()
time.sleep(1)
subprocess.Popen('grep %s /home/ezhuhao/myscreen.txt >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "period" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Peak" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Successful" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Failed" %s >> /home/ezhuhao/display.log' %filename , shell=True)
def current_process():
current_sipp_process = os.popen('ps -ef|grep sipp').readlines()
print current_sipp_process
myfile = open('testit.txt', 'w')
for line in current_sipp_process:
myfile.write(line)
myfile.close()
def current_screen():
current_sipp_screen = os.popen('ls -ltr /home/ezhuhao/scenario/*_screen.log').readlines()
myscreen = open('myscreen.txt','w')
for line in current_sipp_screen:
myscreen.write(line)
myscreen.close()
while queue:
print('Please input the SCENARIO you want:search/kill/starttraffic/bye')
traffic_scenario = raw_input()
print traffic_scenario+(':')
if traffic_scenario == "search":
# current_sipp_process = os.popen('ps -ef|grep sipp').readlines()
#print current_sipp_process
# myfile = open('/home/ezhuhao/testit.txt', 'w')
# for line in current_sipp_process:
# myfile.write(line)
current_screen()
outfile = open("myscreen.txt")
line_list=[]
number_of_line=0
for line in outfile:
number_of_line=number_of_line+1
for theline in line.split():
line_list.append(theline)
if (len(line_list)==0):
print 'it\'s a empty line'
if (line_list[4] == '0'):
print 'it\'s a empty file'
if (line_list[8] != ''):
dis_screen(line_list[8])
displayfile()
empty_list=[]
line_list=empty_list
outfile.close()
# displayfile()
#os.system("rm -rf /home/ezhuhao/display.log")
#os.system("rm -rf /home/ezhuhao/myscreen.txt")
#command for stop traffic
if traffic_scenario == "kill":
current_process()
myfile = open("testit.txt")
line_list=[]
number_of_line=0
for line in myfile:
number_of_line=number_of_line+1
print ('this time get :')+line
for theline in line.split():
line_list.append(theline)
print line_list
if (len(line_list)==0):
print 'it\'s a empty line'
if(line_list[0] == 'root'):
su1 = "-USR1"
pid = su1 + " " + line_list[1]
print pid
# subprocess.Popen('kill %s' %pid , shell=True , close_fds=True)
os.system('kill %s' %pid)
empty_list=[]
line_list=empty_list
myfile.close()
print ('i find ')
print number_of_line
print ('lines')
#command for start traffic
if traffic_scenario == "start":
os.system('rm -rf /home/ezhuhao/scenario/*_screen.log')
print('Please input the traffic you want:voice')
scenario = raw_input()
print scenario+(':')
if scenario == "voice":
os.system("./sipp-3.0.src/sipp 192.168.51.131:5060 -sf ./scenario/uas_amr.xml -i 192.168.51.242 -p 5262 -mi 192.168.51.242 -mp 20002 -aa -trace_screen -bg")
os.system("./sipp-3.0.src/sipp 192.168.51.130:5060 -sf ./scenario/uac_amr.xml -s 4177771234567890 -i 192.168.51.242 -p 5261 -mi 192.168.51.242 -mp 21300 -aa -r 1 -d 10000 -t u1 -trace_screen -bg ")
if traffic_scenario == "bye":
os.system("rm -rf /home/ezhuhao/display.log")
os.system("rm -rf /home/ezhuhao/myscreen.txt")
queue = False
if traffic_scenario == "test":
current_screen()
#os_output = os.system("./sipp-3.0.src/sipp 192.168.51.131:5060 -sf ./scenario/uas_amr.xml -i 192.168.51.242 -p 5299 -mi 192.168.51.242 -mp 20002 -aa -bg -trace_screen -bg").readlines()
#aaaa=str(os_output)
#print ('+++++++++++++++++++++')
#print os_output
下面是我写的,不过不知道怎么和远程命令整合到一起
#!/usr/bin/python
#import pexpect
import string, os, sys
import subprocess
import time
queue = True
def displayfile():
myfile = open("/home/ezhuhao/display.log")
input=myfile.readline()
while (input != ''):
print input
input=myfile.readline()
myfile.close()
def dis_screen(filename):
# subprocess.Popen('grep %s /home/ezhuhao/myscreen.txt >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "period" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Peak" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Successful" %s >> /home/ezhuhao/display.log' %filename , shell=True)
# subprocess.Popen('grep "Failed" %s >> /home/ezhuhao/display.log' %filename , shell=True)
myfile = open('/home/ezhuhao/display.log' ,'wa')
myfile.write('++++++++++Here is the screen log++++++++++')
myfile.close()
time.sleep(1)
subprocess.Popen('grep %s /home/ezhuhao/myscreen.txt >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "period" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Peak" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Successful" %s >> /home/ezhuhao/display.log' %filename , shell=True)
time.sleep(1)
subprocess.Popen('grep "Failed" %s >> /home/ezhuhao/display.log' %filename , shell=True)
def current_process():
current_sipp_process = os.popen('ps -ef|grep sipp').readlines()
print current_sipp_process
myfile = open('testit.txt', 'w')
for line in current_sipp_process:
myfile.write(line)
myfile.close()
def current_screen():
current_sipp_screen = os.popen('ls -ltr /home/ezhuhao/scenario/*_screen.log').readlines()
myscreen = open('myscreen.txt','w')
for line in current_sipp_screen:
myscreen.write(line)
myscreen.close()
while queue:
print('Please input the SCENARIO you want:search/kill/starttraffic/bye')
traffic_scenario = raw_input()
print traffic_scenario+(':')
if traffic_scenario == "search":
# current_sipp_process = os.popen('ps -ef|grep sipp').readlines()
#print current_sipp_process
# myfile = open('/home/ezhuhao/testit.txt', 'w')
# for line in current_sipp_process:
# myfile.write(line)
current_screen()
outfile = open("myscreen.txt")
line_list=[]
number_of_line=0
for line in outfile:
number_of_line=number_of_line+1
for theline in line.split():
line_list.append(theline)
if (len(line_list)==0):
print 'it\'s a empty line'
if (line_list[4] == '0'):
print 'it\'s a empty file'
if (line_list[8] != ''):
dis_screen(line_list[8])
displayfile()
empty_list=[]
line_list=empty_list
outfile.close()
# displayfile()
#os.system("rm -rf /home/ezhuhao/display.log")
#os.system("rm -rf /home/ezhuhao/myscreen.txt")
#command for stop traffic
if traffic_scenario == "kill":
current_process()
myfile = open("testit.txt")
line_list=[]
number_of_line=0
for line in myfile:
number_of_line=number_of_line+1
print ('this time get :')+line
for theline in line.split():
line_list.append(theline)
print line_list
if (len(line_list)==0):
print 'it\'s a empty line'
if(line_list[0] == 'root'):
su1 = "-USR1"
pid = su1 + " " + line_list[1]
print pid
# subprocess.Popen('kill %s' %pid , shell=True , close_fds=True)
os.system('kill %s' %pid)
empty_list=[]
line_list=empty_list
myfile.close()
print ('i find ')
print number_of_line
print ('lines')
#command for start traffic
if traffic_scenario == "start":
os.system('rm -rf /home/ezhuhao/scenario/*_screen.log')
print('Please input the traffic you want:voice')
scenario = raw_input()
print scenario+(':')
if scenario == "voice":
os.system("./sipp-3.0.src/sipp 192.168.51.131:5060 -sf ./scenario/uas_amr.xml -i 192.168.51.242 -p 5262 -mi 192.168.51.242 -mp 20002 -aa -trace_screen -bg")
os.system("./sipp-3.0.src/sipp 192.168.51.130:5060 -sf ./scenario/uac_amr.xml -s 4177771234567890 -i 192.168.51.242 -p 5261 -mi 192.168.51.242 -mp 21300 -aa -r 1 -d 10000 -t u1 -trace_screen -bg ")
if traffic_scenario == "bye":
os.system("rm -rf /home/ezhuhao/display.log")
os.system("rm -rf /home/ezhuhao/myscreen.txt")
queue = False
if traffic_scenario == "test":
current_screen()
#os_output = os.system("./sipp-3.0.src/sipp 192.168.51.131:5060 -sf ./scenario/uas_amr.xml -i 192.168.51.242 -p 5299 -mi 192.168.51.242 -mp 20002 -aa -bg -trace_screen -bg").readlines()
#aaaa=str(os_output)
#print ('+++++++++++++++++++++')
#print os_output
作者: paul_26piggy 发布时间: 2011-10-28
s.exec_command()不接受list作为参数
TypeError: must be string or read-only character buffer, not list
TypeError: must be string or read-only character buffer, not list
作者: paul_26piggy 发布时间: 2011-10-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28