I’m try to run this script:
JavaScript
x
15
15
1
hostname = '192.168.3.4'
2
port = 22
3
username = 'username'
4
password = 'mypassword'
5
y = "2012"
6
m = "02"
7
d = "27"
8
9
if __name__ == "__main__":
10
s = paramiko.SSHClient()
11
s.load_system_host_keys()
12
s.connect(hostname, port, username, password)
13
command = 'ls /home/user/images/cappi/03000/y/m/d'
14
s.close
15
The question is:
how can I put the variables y
,m
,d
into the variable command
?
Advertisement
Answer
Python has lots of ways to perform string formatting. One of the simplest is to simply concatenate the parts of your string together:
JavaScript
1
24
24
1
#!/usr/bin/env python
2
import paramiko
3
4
hostname = "192.168.3.4"
5
port = 22
6
username = "username"
7
password = "mypassword"
8
y = "2012"
9
m = "02"
10
d = "27"
11
12
def do_it():
13
s = paramiko.SSHClient()
14
s.load_system_host_keys()
15
s.connect(hostname, port, username, password)
16
command = "ls /home/user/images/cappi/03000/" + y + "/" + m + "/" + d
17
stdin, stdout, stderr = s.exec_command(command)
18
for line in stdout.readlines():
19
print line
20
s.close()
21
22
if __name__ == "__main__":
23
do_it()
24