Shell script using expect to login to couple of remote servers and read "crontab -l"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script using expect to login to couple of remote servers and read "crontab -l"
# 1  
Old 03-15-2013
Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP address. it should also create a summary at the end with the status if any server unable to login with error.

Please help.
# 2  
Old 03-15-2013
Using key authentication would probably be a better idea than expect as it's easier and much more secure.

Something along these lines:
Code:
for server in server1 server2 server3 
do
     ssh username@$server -C "crontab -l someuser" >> cron.log
     ssh username@$server -C "cat /etc/rc.local" >> rc.local.log
     ssh username@$server -C "other commands" >> stuff.log
done

You'll be creating three connections per server however, but there's no way that I'm aware of to redirect the output to three different files locally within a single connection, unless you redirect the whole output to a single file and then do some post-processing work with awk/sed/perl/etc.
# 3  
Old 03-15-2013
If you run some of it from the opposite end, say as a cron, then you can pick up the files at your leisure even if net is down, scp *, or have the files pushed scp * in one session.

You could have one ssh create the three files there and then cpio -i them down the pipe to a cpio -o.

If you NFS mount a little spot, you can skip the ssh and scp!
# 4  
Old 03-16-2013
I tried writing the script & outputs as file hostname.crontab & hostname.rc.local & disk.status and summary of success and failure. I provided a input as list of hostname from a file.
Code:
for i in `cat hostname`
do
expect -c "
spawn ssh -t username@$i \"cat /etc/rc.local\" >> $i.rc.local
spawn ssh -t username@$i \"crontab -l -u root\" >> $i.crontab
spawn ssh -t username@$i \"df -h\" >> $i.disk.status
expect {
-re \".*Are.*.*yes.*no.*\" {
send \"yes\n\"
exp_continue
}
}
expect {
"?assword:" { send -- \"password\r\"; interact }
}
"
done

if someone can help me fine tuning.

Last edited by Franklin52; 03-17-2013 at 06:06 AM.. Reason: Code tags
# 5  
Old 03-20-2013
You can do them in one pass per host, since it is all text, if you:
Code:
expect -c "
spawn ssh -t username@$i 'echo "'"'"cat >$i.rc.local
 <<magic_eof"'"'"
cat /etc/rc.local
echo magic_eof
echo "'"'"cat >$i.crontab <<magic_eof"'"'"
crontab -l -u root
echo magic_eof
echo "'"'"cat >$i.disk.status< <magic_eof"'"'"
df -h
echo magic_eof
' | bash
...
"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

Hi All, i am trying to ssh to a remote machine and execute certain command to remote machine through script. i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as expect: spawn id exp5 not open while executing "expect "$" {... (3 Replies)
Discussion started by: Siddharth shivh
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

4. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

5. Shell Programming and Scripting

ssh through "expect" in shell script

Hi, I am trying to use "Expect" in shell script to ssh and do some work in remote server but I am unable to connect. Here is the code I am using. #save as test.sh set ip "10.10.10.10" set username "uname" set password "upass" spawn ssh $username@$ip expect "Password:" send... (8 Replies)
Discussion started by: shekhar2010us
8 Replies

6. Shell Programming and Scripting

Remote script skips "read" command

This script is supposed to display a file ( crontab ), ask the user if they wish to update the file, then it goes through an update routine. #!/bin/bash FILE=/etc/crontab tail -5 $FILE echo -n "Does crontab need updating" read HOURS ...routines ....etc... Runs locally... (8 Replies)
Discussion started by: Bubnoff
8 Replies

7. Shell Programming and Scripting

read -n1 -r -p "Press..." key / produces error in bash shell script

Hello! Sorry, for my not so perfect english! I want to stop bash shell script execution until any key is pressed. This line in a bash shell script read -n1 -r -p "Press any key to continue..." key produces this error When I run this from the command line usera@lynx:~$ read... (4 Replies)
Discussion started by: linuxinho
4 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. Shell Programming and Scripting

ksh script as a login shell return "no controlling terminal"

I have created a ksh shell script and used it as a login shell for a user. </etc/passwd> lramirev:x:111:200:Luis:/export/home/menush:/usr/local/menush/menush My shell script is like this: </usr/local/menush/menush> #!/bin/ksh # if ] then . $HOME/.profile fi ... (8 Replies)
Discussion started by: lramirev
8 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question