APC UPS Data Center & Enterprise Solutions Forum
Schneider, APC support forum to share knowledge about installation and configuration for Data Center and Business Power UPSs, Accessories, Software, Services.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2023-08-29 08:08 AM
I can't reply to my old thread for some reason, so here goes again.
Has anyone had any success scripting SSH connections to AP963x or AP964x NMC's from Windows?
Basically use an input list of IP's, connect and issue commands to change settings with Python?
We have about 4000 NMC's to configure and need to script connecting via SSH and SCP using a Python script.
We can connect, but that's it.
The session connects without issue.
We get no console output at all, no menu and no apparent response to commands
The NMC event log shows successful logon
We're using the paramiko Python module in windows for SSH/SCP support.
Anyone been able to do this?
Regards,
Tom
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2023-08-30 11:22 AM
Update. Our script works with Gen3 NMC's but fails on Gen2.
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2023-08-31 03:20 AM . Last Modified: 2023-08-31 03:29 AM
Hi Tom,
For what you're actually trying to achieve, usually the easiest way is to upload a configuration directly rather than trying to script interactively. We have a kbase on this - in short, you can upload an .ini file with direct configuration, or a .csf file with commands to be executed.
Some tips on that method - the .ini file does not need to be complete, so you can push a file that only contains the settings you need to change. And it's also useful to know you can offer these from a DHCP server as an option-67 boot-file - this is the method I prefer in our lab environments as it doesn't require a user account to be configured before-hand, so it's the easiest way to bring an out-of-the-box / reset to defaults card back to an expected configuration.
So with the supported methods out the way - the problem you'll hit with Paramiko is that in the past, the NMCs haven't supported what SSH calls "stream 2" - this is the capability that's used when you do e.g. "ssh user@host your command here". We do support this in NMC3 (at least in 2.x.x.x firmwares, I haven't tested earlier), but not in NMC2.
This is the same capability which Paramiko is using in paramiko.client.SSHClient.exec_command(), so I'm assuming that's what you're trying to use (and why it's going wrong). Which is a shame because that is the easiest and most obvious method.
I've had more luck with NMCs with paramiko.client.SSHClient.invoke_shell(), but it leaves you to wrangle the input/output yourself. (the .split part below will make more sense once you see what the buffer looks like before it, and makes for a good example of what I mean by wrangling the output).
I'll offer an example on a "works for me" basis, being clear that the supported methods are in the linked kbase, and I'm tech support rather than a programmer, so I make no promises to my code quality.
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname,
username=username,
password=password,
allow_agent=False,
look_for_keys=False
)
shell = client.invoke_shell()
result = shell.recv(65535).decode('ascii') # empty the receive buffer first so the card moves on to its prompt
shell.send(bytes("help\n", 'ascii')) # important to send a newline with the command otherwise its still waiting
result = shell.recv(65535).decode('ascii') # empty the buffer again so I can capture the output
print("buffer contains:")
print(
str(result)
)
print("What I actually wanted was:")
print(
str(result).split("\r\napc>help\r\n")[-1]
)
client.close()
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
Posted: 2023-08-31 08:11 AM
Shaun,
Thanks for reply. There are several issues that need to be overcome.
Connecting to NMC's to get
Subnet Mask, Gateway and the Override values so the the TCP changes in the config.ini are applied correctly.
Setting the passwords on the card. (this has to be done every 90s days on approx 4000 NMC's)
SCPing the config.ini to the card.
I'll research your suggestions and let you know.
Regards,
Tom
Link copied. Please paste this link to share this article on your social media post.
Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.