LinuxQuestions.org
Help answer threads with 0 replies.
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 09-26-2012, 02:45 AM   #1
cafecoc85
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Rep: Reputation: 0
Question How to auto pass password in shell script !


Hi all,

I have a simple script to check the CPU, Swap Memory and Hard Disk. But I can auto assign password in the script to automatic run it in crontab.

Quote:
#! /bin/bash

# Total memory space details

echo "Memory Space Details"
free -t -m | grep "Total" | awk '{ print "Total Memory space : "$2 " MB";}'
free -t -m | grep "Total" | awk '{ print "Memory Usage space : "$3 " MB";}'
total_mem=$(free -t -m | grep "Total" | awk '{ print $2 }')
mem_used=$(free -t -m | grep "Total" | awk '{ print $3 }')
percent1=$((($mem_used*100)/$total_mem))

echo "Memory Usage : $percent1%"


free -t -m | grep "Swap" | awk '{ print "Total Swap space : "$2 " MB";
print "Used Swap Space : "$3" MB";
print "Free Swap : "$4" MB";
}'
Everytime when I run this script, it require to insert password like the message below :

Quote:
test:~ # ssh root@192.168.1.1 'bash -s' < /home/scripts/memLinux.sh
root@192.168.1.1's password: xxxx
Memory Space Details
Total Memory space : 65207 MB
Memory Usage space : 31438 MB
Memory Usage : 48%
Total Swap space : 32773 MB
Used Swap Space : 2161 MB
Free Swap : 30611 MB
How can I solve this problem ?
 
Old 09-26-2012, 05:43 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 636

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Hi.

Welcome to LQ!
You may use public/private key mechanism to login via ssh without a password. There a lots of tutorials on the web, for example here.

Last edited by firstfire; 09-26-2012 at 05:44 AM.
 
1 members found this post helpful.
Old 09-26-2012, 06:19 AM   #3
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,831

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
do not use:
grep 'pattern' | awk ' { do something } '
but
awk ' /pattern/ { do something } '
it is more efficient.
You can use expect to solve your problem (if the previous post does not fit your needs)
http://bash.cyberciti.biz/security/e...-login-script/
 
1 members found this post helpful.
Old 09-26-2012, 07:47 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,369
Blog Entries: 54

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Even better: don't run the script until you need to and query the results remotely requiring nothing but community read access using SNMP...
 
1 members found this post helpful.
Old 09-30-2012, 09:04 PM   #5
cafecoc85
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by firstfire View Post
Hi.

Welcome to LQ!
You may use public/private key mechanism to login via ssh without a password. There a lots of tutorials on the web, for example here.
Thank for reply, I will try this now.
 
Old 09-30-2012, 09:10 PM   #6
cafecoc85
LQ Newbie
 
Registered: Feb 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
do not use:
grep 'pattern' | awk ' { do something } '
but
awk ' /pattern/ { do something } '
it is more efficient.
You can use expect to solve your problem (if the previous post does not fit your needs)
http://bash.cyberciti.biz/security/e...-login-script/
I'm reading this article, I think it useful to use "expect" command to auto assign password in shell script.
 
Old 10-01-2012, 02:08 AM   #7
nugat
Member
 
Registered: Sep 2012
Posts: 122

Rep: Reputation: 31
Quote:
Originally Posted by cafecoc85 View Post
I'm reading this article, I think it useful to use "expect" command to auto assign password in shell script.
Expect is awesome...but if you are using ssh to run commands remotely I definitely think public/privates keys is the way to go. as a bonus, it is all encrypted (if you care about that).

super simple to set up:

Code:
# generate the pub/priv key pair
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''

# copy the pubkey to the remote host
ssh-copy-id -i ~/.ssh/id_dsa.pub <user>@<remote_ip>

# run a simple test to see if you can ssh remotely w/o password
ssh -o PasswordAuthentication=no <user>@<remote_ip> hostname
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
how to pass MySQL user/pass securely in shell script? digity Linux - Newbie 5 01-07-2010 05:48 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 03:07 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration