Script that turns off a list of servers in network
Hi all,
Can anyone help me write a script(cron Job) that will automatically turn off a list of servers that are connected over a lan.Is there any way I can do this or anywhere I can find this one. Thanks, Ajit |
you can use nmap
Code:
nmap -sP -PI -PT 10.0.0.0/24 |
what do you mean by "turn off". do you want to do a remote shutdown ?
|
Quote:
Ajit |
Quote:
Ajit |
you don't do remote shutdown using nmap.
|
Quote:
Ajit |
Quote:
You have been suggested how to discover servers on your local network using 'nmap'. Then knowing the IP addresses, you can shut down the servers using the commands that are supposed to perform the shutdown and supplying the commands with IP addresses discovered by 'nmap'. Probably shutdown can be done through 'ssh' login to the server to be shut down, and then calling 'poweroff', but most likely you have to eventually become root on the server. |
I got U.
With nmap I can find the ip of the different servers. Thanks for that additional information.Here in my case I know the ip addresses of the servers but how do I proceed from here. I will tell you my requirement in short I have to run a script actually that too should run automatically at some predefined time which does the following logins into the servers. Later can su as root and than run the shutdown command which will shutdown all the servers within that script. Thanks Ajit |
install a ssh key for an admin account on each of the servers you want shutdown, then call shutdown -whatever whenever from your script.
|
ok.Still I am not able to to whatever is required.Can someone help me out with the script.
Ajit |
Quote:
Or you want us to write a script for you ? |
http://www.freeos.com/guides/lsst/ <-- Great BASH tutorial.
|
I dont want you to write a script for me but if you people can help me out that will be great.
The script that I have written is as below spawn telnet 135.27.153.243 shutdown -t 180 135.27.153.201 And the error that I get when I run the script is invalid command name "shutdown" while executing "shutdown -t 180 135.27.153.201" (file "./shut_down" line 3) Ajit |
Quote:
Based on which manual pages did you choose your commands and arguments ? Specifically, can you show me the lines in 'man shutdown' which mention IP address ? Did you perform successful remote shutdown manually ? If yes, what did you exactly do ? If not, why do you start with a script in the first place ? The last, but not least: do you first read man pages or do you first bother other people ? |
One more workaround you can do using SSH
You would generate single key pair without passphrase using command ssh-keygen from where you would execute your script. Place Public key to all of the servers. Keep private key onto the system from where you would execute script using cronjob How the script is going to be? You would write the script from where you would execute the command. Bring all the servers(ips)in loop or array. Login on to server one by one using ssh command by using private key and execute shutdown command then exit For more detail check ssh manual at (http://www.openssh.org) another bash tutorial(http://www.linuxconfig.org/Bash_scripting_Tutorial) Ofcourse, You would be shown the way to reach destination but no one would lead you there. ABS |
um, would:
Code:
ssh user@server sudo /sbin/shutdown -h now |
Thanks All for all the important pointers.Its true,all one needs is pointer and I am on my way.I will definitely share this script ones its done.Maybe we can further enrich this one once we are through.
Thanks Ajit |
Quote:
|
Quote:
The authenticity of host '135.27.153.243 (135.27.153.243)' can't be established. RSA key fingerprint is c1:99:8c:2e:37:42:e5:0f:02:84:47:85:e2:1c:cd:29. Are you sure you want to continue connecting (yes/no)? And now I have to enter is yes But I dont want to input anything I want my script to do this. Ajit |
But it only asks that once for a new machine, then it memorizes it for future ref. Its a security feature, see man-in-the-middle attack.
|
Script still failing.....
Quote:
The error I receive is Sorry, user init is not allowed to execute '/sbin/shutdown -h now' as root on S. Thanks, Ajit |
Add init to sudoers file and give him ability to run that cmd.
|
Quote:
I added this entry in the sudoers file vi /etc/sudoers. init ALL=/sbin/shutdown. And I run my script but it still gives me the same error. Obviously I did something wrong but what is it? Ajit |
this works in fedora:
http://www.linuxquestions.org/questi...1/#post3441308 is init some sort of special system user that has to do with the init command ? |
Quote:
|
Quote:
|
Quote:
|
Quote:
|
^ but now my previous post isnt funny anymore...
|
Quote:
Script source #!/bin/bash ssh init@135.27.162.214 sudo /sbin/shutdown -h now The script has been given the execute permissions -rwxr-xr-x 1 root root 64 May 16 15:52 test.sh and the command line I use is ./test.sh The script screen output is as below Sorry, user init is not allowed to execute '/sbin/shutdown -h now' as root on S8720_2_1. Ajit |
Quote:
So, first try to change your command line to: Code:
ssh -n init@135.27.162.214 'sudo /sbin/shutdown -h now' Also pay attention to '-n' and look up in the manual what it means. Make sure you do need it, and have taken measures to be able to use it - otherwise you won't be able to run the script unattended. Bow, don't just grab what I've given you, but try to understand why it didn't work - do you understand how shells deal with command line arguments ? I.e. do you understand how command line items are split ? |
Quote:
Ajit |
Quote:
And did you verify you need of '-n' ? Justify your answer. |
are you able to do:
ssh init@135.27.162.214 without it asking you for a password ? once in 135.27.162.214 are you able to do sudo /sbin/shutdown -h now to shut it down while being logged in as init ? ____________________ works for me (fc-9 to rh-9): Code:
[fedora@localhost System]$ ssh schneidz@hyper sudo /sbin/shutdown -h now |
Quote:
It doesnt return any error but also doesnt turn off the server. Ajit |
Quote:
Again, you are waisting our time. I wrote you earlier: Quote:
To start performing the requested action you need to do Code:
man ssh Read the paragraph and make sure you understand what it says/means, if something is not clear, ask further questions here. |
Quote:
http://wiki.linuxquestions.org/wiki/PassWordLess_LogIns 2- aside from pulling the plug, how does one shutdown that computer ? maybe a look inside /var/log/messages will clue us into what is going on ? im surprised no error message (like 'user 'init' not in sudoers file). maybe if you do visudo as root and make sure init is in there. copy and paste those lines in here so we can see. |
Quote:
Here are the steps I did for the same.I have two servers namely S8300B_6(135.27.153.201) and the other is S8300_8(135.27.153.206) On S8300B_6 I am doing the following steps as root root@S8300B_6> ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/root/.ssh/id_dsa):{I enter} /root/.ssh/id_dsa already exists. Overwrite (y/n)?{y} Enter passphrase (empty for no passphrase):{I enter} Enter same passphrase again:{I enter} The key fingerprint is: 28:07:aa:29:2a:e3:19:21:a8:cc:a0:f2:7d:2a:78:66 root@S8300B_6 Now I run scp .ssh/id_dsa.pub init@135.27.153.206:~/.ssh/authorized_keys Than I run the command ssh 135.27.153.206 but again it asks me password. 2- aside from pulling the plug, how does one shutdown that computer ? Using the shutdown command I can shut the machine.Correct me if I am wrong. I checked the /var/log/messages file but there were no new entries there. The lines added by me in sudoers files is as below and also the set of commands that I used visudo -f /etc/sudoers This opens a /etc/sudoers.tmp file and at the last of the file I have added these lines init ALL=/sbin/shutdown Ajit |
1- i wouldnt do it as root, the link was just an example.
it seems like root's passwordless login is ok but init's is not. (i'll spell it out for ya'; run ssh-keygen -t dsa as init). 2- previousely you said the shutdown command didnt work for init even after you logged into a terminal session on that machine. Quote:
Quote:
|
Quote:
chmod u+s /sbin/shutdown now, when you'll do: /sbin/shutdown (args) it will work as a user. sticky-bit will be easier in this case. |
^ but shouldnt it have thrown a 'user not authorized' error ?
something here is fishy... and i dont like fish. except one time i was in a restaurant in martha's vineyard and had some crab cakes that was ok. i mean if i liked fish i wouldve loved it, but i didnt hate it... |
Quote:
|
^ just my own personnal ramblings from working through a mid-afternoon sugar drought in my cubicle.
now that i read it again it seems kinda' simpsons, family guy, athf inspired. maybe i took it from them subconsciously in my half-sleep state. |
Quote:
|
Quote:
Have you restarted 'sshd' after setting up keys ? |
Quote:
Ajit |
Quote:
Ajit |
All times are GMT -5. The time now is 11:19 PM. |