LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to auto pass password in shell script ! (http://www.linuxquestions.org/questions/programming-9/how-to-auto-pass-password-in-shell-script-4175429066/)

cafecoc85 09-26-2012 03:45 AM

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 ?

firstfire 09-26-2012 06:43 AM

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.

pan64 09-26-2012 07:19 AM

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/

unSpawn 09-26-2012 08:47 AM

Even better: don't run the script until you need to and query the results remotely requiring nothing but community read access using SNMP...

cafecoc85 09-30-2012 10:04 PM

Quote:

Originally Posted by firstfire (Post 4789624)
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.

cafecoc85 09-30-2012 10:10 PM

Quote:

Originally Posted by pan64 (Post 4789651)
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.

nugat 10-01-2012 03:08 AM

Quote:

Originally Posted by cafecoc85 (Post 4793585)
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



All times are GMT -5. The time now is 11:47 AM.