小编典典

Python Paramiko-运行命令

python

我尝试运行此脚本:

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

现在的问题是:我怎样才能把这些变量ymd到变量command


阅读 168

收藏
2020-12-20

共1个答案

小编典典

Python有很多执行字符串格式化的方法。最简单的方法之一就是将字符串的各个部分简单地串联在一起:

#!/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()
2020-12-20