Go to content

geek_stuff/server & linux

여러 장비에서 자동으로 슬립모드로 들어가는 bash shell script

한참 머리 굴리다가 만들었다. ㅋㅋ


windows에 ssh 서버를 설치해야 하고, (cygwin으로 하면 편한듯)

각 ssh서버들에 id_rsa.pub을 .ssh/authorized_key로 복사해 넣어야 하는 단점이 있긴 하지만...

#!/bin/bash
if [[ -z $1 ]]; then
        RUN_IN_SEC=30
else
        RUN_IN_SEC=$1
fi
PC_ADDR=( "root@172.20.31.27,linux" "ikko@172.20.31.26,windows" "root@localhost,linux" )

# Remote PC
function Run_Remote_Suspend()
{
        local variables=( `echo $@ | tr "," " "` )
        local id_address=${variables[0]}
        local pc_type=${variables[1]}

        if [[ $id_address == "root@localhost" ]]; then
                #let localhost enter suspend mode last
                sleep 10
        fi
        sleep $RUN_IN_SEC

        case $pc_type in
        [lL][iI][nN][uU][xX])
                ssh $id_address "pm-suspend"
                ;;
        [wW][iI][nN][dD][oO][wW][sS])
                # This command disables hibernation feature in windows.
                # Windows requres to install ssh server (tested with cygwin sshd)
                ssh $id_address "powercfg -hibernate off && rundll32 powrprof.dll,SetSuspendState 0,1,0"
                ;;
        esac
}

function Ask_Cancel()
{
        /usr/bin/xmessage -display 127.0.0.1:0.0 -nearmouse -buttons "Cancel:1" "System will enter sleep mode in $RUN_IN_SEC sec"
        # Yes=true, No=False=1
        if [[ $? -eq 1 ]]; then
                kill ${remote_pid[*]} > /dev/null
                notify-send -t 2 -i "system" "Canceled entering Sleep Mode" "Entering sleepmode canceled by user"
        fi
}

notify-send -t 2 -i "system" "Entering Sleep Mode" "System will enter sleep mode in $RUN_IN_SEC seconds."
for each_pc in ${PC_ADDR[*]};
do
        Run_Remote_Suspend $each_pc &
        remote_pid=( ${remote_pid[*]} $! )
done

Ask_Cancel&
let xmessage_sleep=$RUN_IN_SEC-1
sleep $xmessage_sleep && killall xmessage


'geek_stuff > server & linux' 카테고리의 다른 글

ubuntu repository mirroring  (0) 2012.08.06
GIT 이전  (0) 2012.08.02
시스템 sleep 관련 event 발생시 자동실행  (0) 2012.07.19
일반적인 리눅스 bash command  (0) 2012.07.19
ssh 접속이 느릴경우  (0) 2011.08.10