Question regarding Paramiko and SSH channel
Forgive me, I know nothing about network programming.
I have a group of servers I need to perform maintenance on. Sudo is not permitted. As such, I have to use "su -" to elevate to root and then carry out a list of tasks as root and grab the results from each command so I can view the logs later. I have a script currently working, but it feels hackish and I am thinking there must be a better way to do what I'm doing.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(<my connection details>)
channel = ssh.invoke_shell()
try:
channel.send("su -\n")
while not channel.recv_ready():
print "Waiting for root challenge..."
time.sleep(2)
results += channel.recv(1024)
channel.send("%s\n" % rootpass)
while not channel.recv_ready():
print "Authenticating..."
time.sleep(2)
results += channel.recv(1024)
channel.send("yum update <rpm>\n")
while not channel.recv_ready():
print "Working on yum update..."
time.sleep(10)
results += channel.recv(1024)
except Exception, e:
print e
return results
1) The time.sleep's feel hackish. Some of the commands, like a "yum update" return a lot of text and so I put a longer time interval on the recv_ready() check, because I wo开发者_Go百科uld prefer not to receive a partial result.
2) I do not understand blocking vs non-blocking. I think channels in Paramiko are by default in blocking mode, meaning (I think) there may be a better way to wait until the command finishes and to get the finished results, rather than putting sleeps in there?
精彩评论