LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2011, 11:56 PM   #1
hasmeet.singh337
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Rep: Reputation: 0
Unhappy shell script error...


Hello everybody,
I am trying to make a shell script which will access a server present in my local network whose ip is 172.26.31.2 through ssh and restarts the network service. Its port no. is 1952

My own ip is: 172.16.31.176


For accessing it i use:-

ssh -p 1952 172.16.31.2

But i am unable to write the part of script which will supply the password whenever it prompts for password.

***********************

For restarting the service i can use

service network restart

Shell used by me is : /bin/sh

I will be thankful to u all....if u help me for this..

Regards
Hasmeet Singh
 
Old 03-09-2011, 12:19 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Is there a reason for not using keys so you don't need the password?
 
Old 03-09-2011, 01:19 AM   #3
hasmeet.singh337
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Cool

Quote:
Originally Posted by catkin View Post
Is there a reason for not using keys so you don't need the password?

Sir i have the password with me, i just want to make my task easy to login in the server,restart the network service and logout
 
Old 03-09-2011, 01:25 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

You can use sshpass to pass the password onto the SSH session and execute the command. Also if you want to restart the network service be sure that the user you use to connect through SSH has permissions to do so. With sshpass you can open an ssh session like this:
Code:
sshpass -p 'yourpassword' ssh youruser@yourserver.com 'service network restart'
Kind regards,

Eric
 
Old 03-09-2011, 05:01 AM   #5
hasmeet.singh337
LQ Newbie
 
Registered: Jan 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Talking

Quote:
Originally Posted by EricTRA View Post
Hello,

You can use sshpass to pass the password onto the SSH session and execute the command. Also if you want to restart the network service be sure that the user you use to connect through SSH has permissions to do so. With sshpass you can open an ssh session like this:
Code:
sshpass -p 'yourpassword' ssh youruser@yourserver.com 'service network restart'
Kind regards,

Eric
Thanks for your valueable answer sir,

By using this i am able to login to the server automatically after executing script, but i doesn't restarts the network service on server...When i manually logout from the server then it restarts the network service..

Script is written as :-



sshpass -p mypassword ssh -p portno 172.16.31.2
service network restart


Thanks in advance
Linuxlover
 
Old 03-09-2011, 05:44 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You might want to look into using Expect for this. Google for some tutorials.
 
Old 03-10-2011, 12:52 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

What you posted as script isn't complete. Please post the complete content of your script if you don't mind and put it between code tags for readability. Or did you just run the command from the terminal?

Kind regards,

Eric
 
Old 03-11-2011, 12:16 AM   #8
cam34
Member
 
Registered: Aug 2003
Distribution: Fedora 22, Debian 8, Centos 6/7 for servers
Posts: 101

Rep: Reputation: 16
why dont you just enter: ssh -p 1952 172.16.31.2 service network restart
It will prompt you for your password, run the command, return the output and then disconnect.

All with one command....
 
Old 03-11-2011, 02:17 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
What about using ssh-keys with an ssh-agent?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in shell script hasmeet.singh337 Programming 2 03-07-2011 02:39 AM
[SOLVED] Help with shell script error thebombzen Linux - Software 1 02-14-2011 09:21 PM
shell script error _Linux_Learner Linux - Newbie 11 02-21-2010 11:21 AM
Help With A Shell Script Error PhantomFX Linux - Newbie 3 02-17-2005 04:42 AM
Shell Script Error Gerardoj Linux - General 2 12-03-2003 09:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration