Welcome to the new Schneider Electric Community

It's your place to connect with experts and peers, get continuous support, and share knowledge.

  • Explore the new navigation for even easier access to your community.
  • Bookmark and use our new, easy-to-remember address (community.se.com).
  • Get ready for more content and an improved experience.

Contact SchneiderCommunity.Support@se.com if you have any questions.

Close
Invite a Co-worker
Send a co-worker an invite to the Exchange portal.Just enter their email address and we’ll connect them to register. After joining, they will belong to the same company.
Send Invite Cancel
84773members
354213posts

Automated scripting on Switched Rack PDU

APC UPS Data Center & Enterprise Solutions Forum

Schneider Electric support forum for our Data Center and Business Power UPS, UPS Accessories, Software, Services, and associated commercial products designed to share knowledge, installation, and configuration.

Solved
pedriyoo_apc
Crewman
Crewman
0 Likes
6
1181

Automated scripting on Switched Rack PDU

This was originally posted on APC forums on 2/9/2012


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


Accepted Solutions
pedriyoo_apc
Crewman
Crewman
1
1181

Re: Automated scripting on Switched Rack PDU

This was originally posted on APC forums on 2/13/2012


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

See Answer In Context

6 Replies 6
BillP
Administrator Administrator
Administrator
0 Likes
0
1181

Re: Automated scripting on Switched Rack PDU

This reply was originally posted by Angela on APC forums on 2/9/2012


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

pedriyoo_apc
Crewman
Crewman
0 Likes
0
1181

Re: Automated scripting on Switched Rack PDU

This was originally posted on APC forums on 2/9/2012


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.

BillP
Administrator Administrator
Administrator
0 Likes
0
1181

Re: Automated scripting on Switched Rack PDU

This reply was originally posted by Angela on APC forums on 2/9/2012


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 😕

pedriyoo_apc
Crewman
Crewman
1
1182

Re: Automated scripting on Switched Rack PDU

This was originally posted on APC forums on 2/13/2012


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

smallfish01
Crewman
Crewman
0 Likes
0
718

Re: Automated scripting on Switched Rack PDU

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.

Tags (1)
MyElectrons
Crewman
Crewman
0 Likes
0
236

Re: Automated scripting on Switched Rack PDU

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