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.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:26 AM
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:26 AM
Hello all,
I am programming a little bash script that checks whether a machine is down or not, and if yes, it reboots the machine.
For doing so, it needs to connect to the PDU and launch the "reboot" command on the outlet to which the machine is connected.
The problem is that I want this to be automated but the ssh (and telnet) connection keeps asking me for the password... Isn't it possible to install a key certificate so that password is not required during my script execution?
I've read the manual but I can't figure out how to run automated scripts on the PDU.
Any ideas on this?
Thanks in advanced!!
Pedro
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
Hi,
After some trial and error, and a few coffees too, I've managed to program a simple script that does what I mentioned.
Unfortunately, a UNIX tool called "expect" is needed, which is not installed by default in all UNIX-based distribution. You can install it via apt-get or yum (depending on your distribution)
The script basically waits until a string similar to the pattern given to "expect" is written on the console prompt, and then it sends the passwords (or commands) as if they were written by the user. The script works both for telnet and ssh2 connections (can't guarantee ssh1 since my workstation hasn't got it installed).
The code is the following:
// USE ---> /ssh_command.sh user password ip
// set Variables
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set timeout -1
// now connect to remote PDU
// using TELNET---> spawn telnet $ipaddr
spawn ssh $user@$ipaddr
match_max 100000
// Using TELNET
//expect "User Name :"
//send "$user"
// Look for passwod prompt
expect "*?assword*"
// Send password aka $password
send "$password"
expect "APC>"
send "off 1"
expect "APC>"
sleep 3
send "on 1"
expect "APC>"
send "exit"
expect eof
close $spawn_id
I hope this helps anyone with the same problem.
PS:
I tried to change the fingerprint of the PDU, but that didn't work because the fingerprint identifies the PDU so when other clients connect to it, they can trust this host (the PDU). However, the PDU wasn't able to recognize clients.
Tried also to upload the fingerprint of my workstation to a directory in the PDU (specifically "~/.ssh" as it says this tutorial), but the ftp server that runs on the PDU isn't very flexible, and doesn't allow you to create folders or so.
Message was edited by: pedriyoo
Message was edited by: pedriyoo
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: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:26 AM
what model PDU is it? There is a scriping CLI on our two generations of PDUs outlined here: http://nam-en.apc.com/app/answers/detail/a_id/10853
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
It is a AP7920 device.
I can connect to the command line, but I'd like to connect to it using a certificate, so I can make an ssh connection in a script automatically, without giving out the password.
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: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
what SSH client are you using? i would think you could put the key in its keystore but i guess i have never tried. i am not sure if there is a way to get around typing the password completely 😕
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
Link copied. Please paste this link to share this article on your social media post.
Posted: 2021-06-28 06:22 AM . Last Modified: 2024-03-18 02:25 AM
Hi,
After some trial and error, and a few coffees too, I've managed to program a simple script that does what I mentioned.
Unfortunately, a UNIX tool called "expect" is needed, which is not installed by default in all UNIX-based distribution. You can install it via apt-get or yum (depending on your distribution)
The script basically waits until a string similar to the pattern given to "expect" is written on the console prompt, and then it sends the passwords (or commands) as if they were written by the user. The script works both for telnet and ssh2 connections (can't guarantee ssh1 since my workstation hasn't got it installed).
The code is the following:
// USE ---> /ssh_command.sh user password ip
// set Variables
set user [lrange $argv 0 0]
set password [lrange $argv 1 1]
set ipaddr [lrange $argv 2 2]
set timeout -1
// now connect to remote PDU
// using TELNET---> spawn telnet $ipaddr
spawn ssh $user@$ipaddr
match_max 100000
// Using TELNET
//expect "User Name :"
//send "$user"
// Look for passwod prompt
expect "*?assword*"
// Send password aka $password
send "$password"
expect "APC>"
send "off 1"
expect "APC>"
sleep 3
send "on 1"
expect "APC>"
send "exit"
expect eof
close $spawn_id
I hope this helps anyone with the same problem.
PS:
I tried to change the fingerprint of the PDU, but that didn't work because the fingerprint identifies the PDU so when other clients connect to it, they can trust this host (the PDU). However, the PDU wasn't able to recognize clients.
Tried also to upload the fingerprint of my workstation to a directory in the PDU (specifically "~/.ssh" as it says this tutorial), but the ftp server that runs on the PDU isn't very flexible, and doesn't allow you to create folders or so.
Message was edited by: pedriyoo
Message was edited by: pedriyoo
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: 2022-04-28 11:17 PM
Hello,
Thanks for your provided the script, I have tried and found when I running the script ssh to PDU, but hung when I running the script.
# ./apc.sh apc apc 192.168.1.7
spawn ssh apc@192.168.1.7
apc@192.168.1.7's password: apc
Do you know the reason?
Thank you.
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: 2022-08-16 06:55 PM
Hello to all happy APC Switched and Metered PDU owners and admins! 🙂
Last weekend I needed to automate outlet switching in my home lab. Here's what I've got for ap7901 & ap7902 PDUs:
https://github.com/MyElectrons/PDU-Commander
A sample command, executed from bash command line or a script:
./pdu-commander.py -a 192.168.7.242 -u device -p goodpassword on:1-2,8 delay:3 off:2 get:power
Script's output:
Address: 192.168.7.242
Unit ID: ap7902
Outlets: 16
('on', '1-2,8')
1: Outlet 1 : Outlet Turned On
2: Outlet 2 : Outlet Turned On
8: Outlet 8 : Outlet Turned On
('delay', '3')
('off', '2')
2: Outlet 2 : Outlet Turned Off
('get', 'power')
168 VA
168 Watts
There's logging, error handling, and a big potential for growth in that little project.
New features, improvement suggestions, and pull requests are very welcome!
Cheers,
- Serge
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.