LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script that turns off a list of servers in network (https://www.linuxquestions.org/questions/programming-9/script-that-turns-off-a-list-of-servers-in-network-725352/)

Ajit Gunge 05-12-2009 01:34 AM

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

repo 05-12-2009 01:38 AM

you can use nmap
Code:

nmap -sP -PI -PT 10.0.0.0/24

ghostdog74 05-12-2009 02:08 AM

what do you mean by "turn off". do you want to do a remote shutdown ?

Ajit Gunge 05-12-2009 02:33 AM

Quote:

Originally Posted by ghostdog74 (Post 3537908)
what do you mean by "turn off". do you want to do a remote shutdown ?

Yes you are correct I want to do is a remote shutdown.

Ajit

Ajit Gunge 05-12-2009 02:50 AM

Quote:

Originally Posted by repo (Post 3537885)
you can use nmap
Code:

nmap -sP -PI -PT 10.0.0.0/24

IS there an exe for nmap available for a windows machine that I can readily use.

Ajit

ghostdog74 05-12-2009 03:21 AM

you don't do remote shutdown using nmap.

Ajit Gunge 05-12-2009 04:38 AM

Quote:

Originally Posted by ghostdog74 (Post 3537967)
you don't do remote shutdown using nmap.

So is there any way that I can run the shutdown command remotely.

Ajit

Sergei Steshenko 05-12-2009 05:38 AM

Quote:

Originally Posted by Ajit Gunge (Post 3538024)
So is there any way that I can run the shutdown command remotely.

Ajit

I think you are missing the point.

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.

Ajit Gunge 05-12-2009 07:26 AM

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

jim80net 05-12-2009 07:41 AM

install a ssh key for an admin account on each of the servers you want shutdown, then call shutdown -whatever whenever from your script.

Ajit Gunge 05-12-2009 08:59 AM

ok.Still I am not able to to whatever is required.Can someone help me out with the script.

Ajit

Sergei Steshenko 05-12-2009 09:06 AM

Quote:

Originally Posted by Ajit Gunge (Post 3538248)
ok.Still I am not able to to whatever is required.Can someone help me out with the script.

Ajit

You do not describe what you exactly did and what the problems were - error messages, etc.

Or you want us to write a script for you ?

jim80net 05-12-2009 03:46 PM

http://www.freeos.com/guides/lsst/ <-- Great BASH tutorial.

Ajit Gunge 05-13-2009 01:12 AM

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

Sergei Steshenko 05-13-2009 06:54 AM

Quote:

Originally Posted by Ajit Gunge (Post 3539016)
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

I am sorry, but it appears you lack basic understanding of how things work.
  1. what is 'spawn' and why are you calling it ?
  2. why 'telnet' and 'shutdown' have different IP addresses as their arguments ?
  3. why 'shutdown' has IP address as its argument ?

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 ?

abs_77 05-14-2009 05:16 AM

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

schneidz 05-14-2009 09:21 AM

um, would:
Code:

ssh user@server sudo /sbin/shutdown -h now
not work ?

Ajit Gunge 05-14-2009 09:31 AM

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

Sergei Steshenko 05-14-2009 09:47 AM

Quote:

Originally Posted by schneidz (Post 3540583)
um, would:
Code:

ssh user@server sudo /sbin/shutdown -h now
not work ?

The OP hasn't stated he tried it this way.

Ajit Gunge 05-14-2009 10:30 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3540614)
The OP hasn't stated he tried it this way.

I tried this one but what it does is asks me for an input once it logins to the switch the output is

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

chrism01 05-14-2009 11:10 PM

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.

Ajit Gunge 05-15-2009 01:28 AM

Script still failing.....
 
Quote:

Originally Posted by chrism01 (Post 3541347)
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.

yeah you are right.It asks only once.Now my problem is I have to change the user to root to run the shutdown command on the list of servers.I think I will have to use expect for this purpose.
The error I receive is
Sorry, user init is not allowed to execute '/sbin/shutdown -h now' as root on S.

Thanks,
Ajit

chrism01 05-15-2009 01:36 AM

Add init to sudoers file and give him ability to run that cmd.

Ajit Gunge 05-15-2009 02:22 AM

Quote:

Originally Posted by chrism01 (Post 3541456)
Add init to sudoers file and give him ability to run that cmd.

Hmmmm...I will try to explain what I did and than you can tell me what I did wrong.

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

schneidz 05-15-2009 09:06 AM

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 ?

Sergei Steshenko 05-15-2009 09:38 AM

Quote:

Originally Posted by Ajit Gunge (Post 3541483)
Hmmmm...I will try to explain what I did and than you can tell me what I did wrong.

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

Do you want anybody on the network to be able to shut down any server whenever he/she wants ?

Sergei Steshenko 05-15-2009 09:40 AM

Quote:

Originally Posted by Ajit Gunge (Post 3541483)
Hmmmm...I will try to explain what I did and than you can tell me what I did wrong.

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

Please stop waisting our time.
  1. publish thew script source (copy-paste);
  2. publish the exact command line (copy-paste);
  3. publish the script screen output (copy-paste).

schneidz 05-15-2009 09:47 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3541861)
Do you want anybody on the network to be able to shout down any server whenever he/she wants ?

i tried shouting down my server but it just sits there blinking at me moronically.

Sergei Steshenko 05-15-2009 10:01 AM

Quote:

Originally Posted by schneidz (Post 3541879)
i tried shouting down my server but it just sits there blinking at me moronically.

Fixed.

schneidz 05-15-2009 10:36 AM

^ but now my previous post isnt funny anymore...

Ajit Gunge 05-16-2009 05:25 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3541864)
Please stop waisting our time.
  1. publish thew script source (copy-paste);
  2. publish the exact command line (copy-paste);
  3. publish the script screen output (copy-paste).

I am sorry if I am wasting your time.I am a newbie and still picking up things.The details are

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

Sergei Steshenko 05-16-2009 06:35 AM

Quote:

Originally Posted by Ajit Gunge (Post 3542647)
I am sorry if I am wasting your time.I am a newbie and still picking up things.The details are

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

'man ssh' says you need to give a command, which is supposed to be the last argument - pay attention to the singular, not plural form of the "argument".

So, first try to change your command line to:

Code:

ssh -n init@135.27.162.214 'sudo /sbin/shutdown -h now'
- pay attention to the single (in this case could also be double) quotes I've added.

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 ?

Ajit Gunge 05-16-2009 07:18 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3542686)
'man ssh' says you need to give a command, which is supposed to be the last argument - pay attention to the singular, not plural form of the "argument".

So, first try to change your command line to:

Code:

ssh -n init@135.27.162.214 'sudo /sbin/shutdown -h now'
- pay attention to the single (in this case could also be double) quotes I've added.

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 ?

Ok...Now my script doesnt give any error as earlier but it doesnt do whats required,that is shutting down the server.

Ajit

Sergei Steshenko 05-16-2009 07:53 AM

Quote:

Originally Posted by Ajit Gunge (Post 3542705)
Ok...Now my script doesnt give any error as earlier but it doesnt do whats required,that is shutting down the server.

Ajit

So, what is happening in the server to be shut down ? I.e. are there any messages from it ? Are there any messages in /var/log/messages in the server to be shut down ?

And did you verify you need of '-n' ? Justify your answer.

schneidz 05-17-2009 02:22 PM

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
[fedora@localhost System]$


Ajit Gunge 05-18-2009 02:35 AM

Quote:

Originally Posted by schneidz (Post 3543875)
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
[fedora@localhost System]$


It does login into the server,but asks for the password.
It doesnt return any error but also doesnt turn off the server.

Ajit

Sergei Steshenko 05-18-2009 08:26 AM

Quote:

Originally Posted by Ajit Gunge (Post 3544290)
It does login into the server,but asks for the password.
It doesnt return any error but also doesnt turn off the server.

Ajit


Again, you are waisting our time.

I wrote you earlier:

Quote:

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.
, and you haven't performed the requested action.

To start performing the requested action you need to do

Code:

man ssh
and find the paragraph related to '-n' switch. In my case the paragraph is just 4 (four) lines long.

Read the paragraph and make sure you understand what it says/means, if something is not clear, ask further questions here.

schneidz 05-18-2009 08:57 AM

Quote:

Originally Posted by Ajit Gunge (Post 3544290)
1- It does login into the server,but asks for the password.
2- It doesnt return any error but also doesnt turn off the server.

Ajit

1- seems like your passwordless login isnt setup correctly:
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.

Ajit Gunge 05-18-2009 10:19 AM

Quote:

Originally Posted by schneidz (Post 3544670)
1- seems like your passwordless login isnt setup correctly:
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.

I am sorry but after following the steps to configure the passwordless_logins
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

schneidz 05-18-2009 11:40 AM

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:

Originally Posted by schneidz (Post 3543875)
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 ?


Quote:

Originally Posted by Ajit Gunge (Post 3544290)
...
It doesnt return any error but also doesnt turn off the server.

Ajit

3- i dont know about the syntax of visudo (i usually just run visudo while being logged in as root) but look thru the comments of that file and see if there is some group or someplace you can add a user that will allow them to run "sudo shutdown" without prompting for the root passwd.

itz2000 05-18-2009 12:14 PM

Quote:

Originally Posted by Ajit Gunge (Post 3541483)
Hmmmm...I will try to explain what I did and than you can tell me what I did wrong.

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

login as root once and do :

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.

schneidz 05-18-2009 01:18 PM

^ 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...

itz2000 05-18-2009 02:22 PM

Quote:

Originally Posted by schneidz (Post 3544907)
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...

You're truly genius man! where's this sentence is taken from?!

schneidz 05-18-2009 03:10 PM

^ 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.

schneidz 05-18-2009 06:05 PM

Quote:

Originally Posted by Ajit Gunge (Post 3542647)
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

oh wait there is an error message; it seems like init is not a sudoer.

Sergei Steshenko 05-18-2009 06:27 PM

Quote:

Originally Posted by Ajit Gunge (Post 3544760)
I am sorry but after following the steps to configure the passwordless_logins
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.

...

Ajit


Have you restarted 'sshd' after setting up keys ?

Ajit Gunge 05-19-2009 01:15 AM

Quote:

Originally Posted by itz2000 (Post 3544851)
login as root once and do :

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.

Yes this has worked for me I mean after following your instructions I was able to run the shutdown command using the init login on my server.

Ajit

Ajit Gunge 05-19-2009 01:20 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3545176)
Have you restarted 'sshd' after setting up keys ?

I restarted the service and ran all the comands for the init user but it is again asking me the password.:scratch:

Ajit


All times are GMT -5. The time now is 11:19 PM.