Python实现SSH远程登陆,并执行命令的方法(分享)
在自动化测试过程中,比较常用的操作就是对远程主机进行操作,如何操作呢?使用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()
相关推荐
projava 2020-11-14
WanKaShing 2020-11-12
airfish000 2020-09-11
tryfind 2020-09-14
yegen00 2020-09-10
kkaazz 2020-09-03
风语者 2020-09-02
BraveWangDev 2020-08-19
lichuanlong00 2020-08-15
gsl 2020-08-15
pandaphinex 2020-08-09
yhuihon 2020-08-09
CheNorton 2020-08-02
xiangqiao 2020-07-28
hpujsj 2020-07-26
hpujsj 2020-07-26
sshong 2020-07-19
BraveWangDev 2020-07-19
annan 2020-07-18