在脚本中多次使用 paramiko
Using paramiko more than one time in a script
我正在创建一个在不同服务器中使用不同命令的脚本...为此,我避免使用 sshpass 等工具,并尝试使用 paramiko(因为有了paramiko,我可以处理异常,我也可以退出ssh会话)。
我必须连接两台机器,要使用两个不同的命令,等等。
问题是,我不想重复我使用的代码两次,我的代码是这样的:
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(args.ip, username=args.user,
port=port,
password=args.pwd,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
except Exception, e:
print 'Failed to connect to %s: %s' % (args.ip, str(e))
sys.exit(1)
try:
stdin, stdout, stderr = ssh_client.exec_command(command)
commandStatus = stdout.channel.recv_exit_status()
output = stdout.read()
commandError = stderr.read()
except Exception, e:
print 'Failed to execute command "%s": %s' % (command, str(e))
if commandError:
print 'Command failed: %s' % (commandError)
sys.exit(2)
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(args.ip2, username=args.user2,
port=port,
password=args.pwd2,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
except Exception, e:
print 'Failed to connect to %s: %s' % (args.ip2, str(e))
sys.exit(1)
try:
stdin, stdout, stderr = ssh_client.exec_command(command2)
commandStatus = stdout.channel.recv_exit_status()
output = stdout.read()
commandError = stderr.read()
except Exception, e:
print 'Failed to execute command "%s": %s' % (command2, str(e))
if commandError:
print 'Command failed: %s' % (commandError)
sys.exit(2)
是否可以只使用该代码一次?
一般来说,代码的最小元素re-use是函数。因此,创建一个用于创建连接的函数,例如。
def connect(user, pass, hostname, port):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=user,
port=port,
password=pass,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
return ssh_client
# then use the code, for instance
try:
client_a = connect('admin', 'secret', 'myhost.com', 22)
client_b = connect('notadmin', 'notsecret', 'otherhost.com', 22222)
except SomeException: # it's a bad idea to use plain Except
print('message')
sys.exit(1)
我正在创建一个在不同服务器中使用不同命令的脚本...为此,我避免使用 sshpass 等工具,并尝试使用 paramiko(因为有了paramiko,我可以处理异常,我也可以退出ssh会话)。
我必须连接两台机器,要使用两个不同的命令,等等。
问题是,我不想重复我使用的代码两次,我的代码是这样的:
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(args.ip, username=args.user,
port=port,
password=args.pwd,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
except Exception, e:
print 'Failed to connect to %s: %s' % (args.ip, str(e))
sys.exit(1)
try:
stdin, stdout, stderr = ssh_client.exec_command(command)
commandStatus = stdout.channel.recv_exit_status()
output = stdout.read()
commandError = stderr.read()
except Exception, e:
print 'Failed to execute command "%s": %s' % (command, str(e))
if commandError:
print 'Command failed: %s' % (commandError)
sys.exit(2)
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(args.ip2, username=args.user2,
port=port,
password=args.pwd2,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
except Exception, e:
print 'Failed to connect to %s: %s' % (args.ip2, str(e))
sys.exit(1)
try:
stdin, stdout, stderr = ssh_client.exec_command(command2)
commandStatus = stdout.channel.recv_exit_status()
output = stdout.read()
commandError = stderr.read()
except Exception, e:
print 'Failed to execute command "%s": %s' % (command2, str(e))
if commandError:
print 'Command failed: %s' % (commandError)
sys.exit(2)
是否可以只使用该代码一次?
一般来说,代码的最小元素re-use是函数。因此,创建一个用于创建连接的函数,例如。
def connect(user, pass, hostname, port):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname, username=user,
port=port,
password=pass,
timeout=connect_timeout,
allow_agent=False, look_for_keys=False)
return ssh_client
# then use the code, for instance
try:
client_a = connect('admin', 'secret', 'myhost.com', 22)
client_b = connect('notadmin', 'notsecret', 'otherhost.com', 22222)
except SomeException: # it's a bad idea to use plain Except
print('message')
sys.exit(1)