发布时间:2019-09-23 17:07:32编辑:auto阅读(2145)
在自动化测试过程中,比较常用的操作就是对远程主机进行操作,如何操作呢?使用SSH远程登陆到主机,然后执行相应的command即可。
使用Python来实现这些操作就相当简单了。下面是测试code。
代码如下:(code运行环境:python27+eclipse+pydev)
import paramiko
def sshclient_execmd(hostname, port, username, password, execmd):
paramiko.util.log_to_file("paramiko.log")
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname=hostname, port=port, username=username, password=password)
stdin, stdout, stderr = s.exec_command (execmd)
stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.
print stdout.read()
s.close()
def main():
hostname = '10.***.***.**'
port = 22
username = 'root'
password = '******'
execmd = "free"
sshclient_execmd(hostname, port, username, password, execmd)
if __name__ == "__main__":
main()
上一篇: Python中的哈希常识小结
下一篇: Ubuntu更改默认python版本的两
51039
50432
41033
37899
32367
29260
28141
22982
22945
21272
1146°
1850°
1510°
1438°
1718°
1527°
2185°
3777°
3690°
2598°