Overclock.net › Forums › Components › Power Supplies › Execute SSH script on Windows 7 upon a power failure triggered by UPS
New Posts  All Forums:Forum Nav:

Execute SSH script on Windows 7 upon a power failure triggered by UPS

post #1 of 5
Thread Starter 
I have a Linux NAS that I would like to execute a SSH script from my Windows 7 system upon a power failure triggered by my UPS. Is there a way to that?

UPS Model: CyberPower 1000AVR
Power Management Tool: CyberPower PowerPanel Personal Edition
OS: Windows 7

I already have Cygwin installed and I think I can easily execute a SSH command using Cygwin from my Windows 7 system to my Linux NAS to perform a graceful shutdown by following this online article. http://www.linuxquestions.org/questions/programming-9/how-to-execute-a-ssh-script-on-linux-server-from-windows-through-a-bat-script-581171/

The question is when my UPS triggers the shutdown sequence on my Windows 7 system, how do I add an additional hook so that it will also execute my SSH command to shutdown my NAS.

Anyone has any experiencing with this?

Thanks!
post #2 of 5
I wrote a program to do this with a SOCOMEC UPS - its a C# client with a Perl script that runs on the Linux box.

Here's the Linux portion:

conf.pl:
Code:
#!/usr/bin/perl
$CONF_LISTEN_IP = "172.16.0.2";
$CONF_LISTEN_PORT = 7070;
$CONF_SLEEP_INTERVAL = 480;

daemon.pl:
Code:
#!/usr/bin/perl
use IO::Socket;

# Get configuration
require "conf.pl";

# Output prelim information
print "Starting UPSd - {Sleep interval set to $CONF_SLEEP_INTERVAL seconds}\n";

# Register SIG handlers
$SIG{'INT'} = \&SigIntHandler;

# Set up socket
my $socket = new IO::Socket::INET(
        LocalHost       => $CONF_LISTEN_IP,
        LocalPort       => $CONF_LISTEN_PORT,
        Proto           => 'tcp',
        Listen          => 1,
        Reuse           => 1, );

die "Could not create socket: $!\n" unless $socket;

# Get incoming connections
while ( $connection = $socket->accept() )
{
        while ( <$connection> )
        {
                print "COMMAND: $_\n";

                if ( $_ =~ m/^SHUTDOWN/i )
                {
                        $connection->shutdown(0);
                        $connection->close();
                        $socket->close();

                        &ExecShutdown;
                }
                elsif ( $_ =~ m/^EXITD/i )
                {
                        $connection->shutdown(0);
                        $socket->close();
                        last;
                }
        }
}

print "Exiting synchronously...\n";

if ( $connection )
{
        $connection->close();
}

if ( $socket )
{
        $socket->close();
}

print "UPSd exited.\n";

#########################################################################
# SigIntHandler
#########################################################################
sub SigIntHandler
{
        print "Exiting from SIGINT...\n";
        if ( $connection )
        {
                $connection->close();
        }
}

#########################################################################
# ExecShutdown
#########################################################################
sub ExecShutdown
{
        print "Received SHUTDOWN command for processing...\n";

        #Wait for our configured sleep interval
        sleep( $CONF_SLEEP_INTERVAL );

        #Execute the poweroff command
        system( "poweroff" );

        print "Executed shutdown command.\n";
}

It listens on TCP port 7070 for a shutdown command from the C# client running on Windows.

Here is the source code for the client application:
Code:
     class Program
        {
                private static string _command;
                private static string _hostName;
                private static int _port;

                /* 0: host, 1: port, 2: command */
                static void Main( string[] args )
                {
#if DEBUG
                        Console.WriteLine( "COMMAND: " );
                        _command = Console.ReadLine();

                        Console.WriteLine( "HOST: " );
                        _hostName = Console.ReadLine();

                        Console.WriteLine( "PORT: " );

                        if ( !int.TryParse( Console.ReadLine(), out _port ) )
                        {
                                return;
                        }

                        _RunClient();
#else
                        if ( args.Length >= 3 )
                        {
                                _command  = args[ 2 ];
                                _hostName = args[ 0 ];

                                if ( !int.TryParse( args[ 1 ], out _port ) )
                                {
                                        return;
                                }

                                _RunClient();
                        }
#endif
                }

                private static void _RunClient()
                {
                        if ( string.Compare( _command, "MONITOR", StringComparison.OrdinalIgnoreCase )== 0 )
                        {
                                _MonitorRemoteDaemon();
                                return;
                        }

                        if ( string.Compare( _command, "SHUTDOWN", StringComparison.OrdinalIgnoreCase ) == 0 )
                        {
                                _SendExecShutdown();
                                return;
                        }

                        if ( string.Compare( _command, "EXITD", StringComparison.OrdinalIgnoreCase ) == 0 )
                        {
                                _SendExitServer();
                                //return;
                        }
                }

                private static void _MonitorRemoteDaemon()
                {
                        Thread thread = new Thread( _MonitorRemoteDaemonWorker );
                        thread.Start();
                        thread.Join();
                }

                private static void _SendExecShutdown()
                {
                        const string command = "SHUTDOWN";
                        _SendCommand( command );
                }

                private static void _SendExitServer()
                {
                        const string command = "EXITD";
                        _SendCommand( command );
                }

                private static void _SendCommand( string command )
                {
                        byte[] commandBytes = Encoding.ASCII.GetBytes( command );

                        if ( commandBytes != null && commandBytes.Length > 0 )
                        {
                                using ( TcpClient client = new TcpClient() )
                                {
                                        client.Connect( _hostName, _port );

                                        if ( client.Connected )
                                        {
                                                using ( NetworkStream stream = client.GetStream() )
                                                {
                                                        stream.Write( commandBytes, 0, commandBytes.Length );
                                                }
                                        }
                                }
                        }
                }

                private static void _MonitorRemoteDaemonWorker()
                {
                        try
                        {
                                using ( TcpClient client = new TcpClient() )
                                {
                                        client.Connect( _hostName, _port );

                                        while ( true )
                                        {
                                                if ( !client.Client.Poll( 0, SelectMode.SelectWrite ) || !client.Connected )
                                                {
                                                        _MonitorError( "The socket has been closed." );
                                                }

                                                client.Client.Send( new byte[ 1 ] );
                                                Thread.Sleep( 1000 );
                                        }
                                }
                        }
                        catch ( Exception e )
                        {
                                _MonitorError( e.Message );
                        }
                }

                private static void _MonitorError( string message )
                {
                        Console.WriteLine( "WARNING: " +message );
                        MessageBox.Show( "WARNING: " +message );
                }
        }

The difficult part is getting the shutdown sequence to run the client application. With SOCOMEC, this was easy - you give the UPS software a batch file to execute on shutdown. Let me have a look at CyberPower for you ...
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #3 of 5
OK you can do it. Head over to C:\Program Files (x86)\CyberPower PowerPanel Personal Edition

Edit default.cmd to contain your own commands. Either use my Perl daemon and Windows client, or hack your own solution with the SSH method smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #4 of 5
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

OK you can do it. Head over to C:\Program Files (x86)\CyberPower PowerPanel Personal Edition

Edit default.cmd to contain your own commands. Either use my Perl daemon and Windows client, or hack your own solution with the SSH method smile.gif

I didn't aware of there's a default.cmd under the PowerPanel directory. This is awesome!

It's kind of late right now (East Coast timezone). I'll give it a try tomorrow and report my status here. Thanks!
post #5 of 5
Quote:
Originally Posted by beyonddc View Post

I didn't aware of there's a default.cmd under the PowerPanel directory. This is awesome!

It's kind of late right now (East Coast timezone). I'll give it a try tomorrow and report my status here. Thanks!

smile.gif

Good luck!

If the default.cmd doesn't actually do as intended (I read some docco on CyberPower website that IS what its for, but who knows), then I will investigate hooking the shutdown APIs that the software calls for you.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Power Supplies
Overclock.net › Forums › Components › Power Supplies › Execute SSH script on Windows 7 upon a power failure triggered by UPS