Python Paramiko - 运行命令 [英] Python Paramiko - Run command
本文介绍了Python Paramiko - 运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试运行此脚本:
I'm try to run this script:
hostname = '192.168.3.4'
port = 22
username = 'username'
password = 'mypassword'
y = "2012"
m = "02"
d = "27"
if __name__ == "__main__":
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
command = 'ls /home/user/images/cappi/03000/y/m/d'
s.close
问题是:如何将变量 y
,m
,d
放入变量 command
中?
The question is:
how can I put the variables y
,m
,d
into the variable command
?
推荐答案
Python 有很多方法来执行字符串格式化.最简单的方法之一是简单地将字符串的各个部分连接在一起:
Python has lots of ways to perform string formatting. One of the simplest is to simply concatenate the parts of your string together:
#!/usr/bin/env python
hostname = '192.168.3.4'
port = 22
username = 'username'
password = 'mypassword'
y = "2012"
m = "02"
d = "27"
def do_it():
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
command = 'ls /home/user/images/cappi/03000/' + y + '/' + m + '/' + d
(stdin, stdout, stderr) = s.exec_command(command)
for line in stdout.readlines():
print line
s.close()
if __name__ == "main":
do_it()
这篇关于Python Paramiko - 运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文