LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-26-2016, 03:54 PM   #1
bravored
Member
 
Registered: Mar 2005
Distribution: RHEL, freeBSD, Solaris
Posts: 49

Rep: Reputation: 0
Question expect script or oneliner


Hey All

I like a one liner or script in expect to gather uptime on all other servers.

How to specify a host file to ssh too?

This is what I have for a bash
for sys in `cat redhat`; do echo "ssh to $sys ..."; ssh $sys; done;

For Expect I have :

#!/usr/bin/expect -f

spawn passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "password"
sent "$password\r"
expect "password:"
sent "$password\r"
expect eof


Where do you specify a file get the other hosts?

Thanks
Joe
 
Old 09-27-2016, 02:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,865
Blog Entries: 3

Rep: Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053
If these machines really are RHEL why aren't you using the support you've paid for?

That's not a rhetorical question. However, regardless of the answer, I'm writing to discourage the use of "expect". There must have been an article or podcast mentioning it, because there have been a lot of people bringing it up recently instead of using safer methods.

For safety, ease of use, and efficiency, I'd use special-purpose keys with or without an agent instead of "expect".

Code:
mkdir -m 700 redhat_keys
cd redhat_keys
for sys in $(cat redhat); do ssh-keygen -t rsa -b 2048 -f uptime_rsa_${sys} -N '' ; done
sed -i 's#^#command="/usr/bin/uptime -p" #' uptime*.pub
Then put the public keys on their respective target hosts using "ssh-copy-id" and verify that you can log in using the keys.

Thereafter all you have to do is log in using the keys to get the uptime:

Code:
cd redhat_keys
for sys in $(cat redhat); 
do echo "ssh to $sys ..."; 
   ssh -i ./uptime_rsa_${sys} -o IdentitiesOnly=yes $sys; 
done;

Last edited by Turbocapitalist; 09-27-2016 at 05:30 AM.
 
Old 09-27-2016, 03:03 PM   #3
bravored
Member
 
Registered: Mar 2005
Distribution: RHEL, freeBSD, Solaris
Posts: 49

Original Poster
Rep: Reputation: 0
Thanks

My boss wants expect and that is what he gonna get.

Anyone know how to get expect to get to the a file with other --
Can expect -c work?

Thanks
 
Old 09-27-2016, 04:20 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
You mean get a file from remote host through ssh?
 
Old 09-27-2016, 04:39 PM   #5
bravored
Member
 
Registered: Mar 2005
Distribution: RHEL, freeBSD, Solaris
Posts: 49

Original Poster
Rep: Reputation: 0
Well I guess in the future
Right now its using expect to get uptime from a list of server in a file called servers

I am able to pull just 1 server..
 
Old 09-27-2016, 07:32 PM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,177

Rep: Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436Reputation: 3436
Anyway. I'd say to use autoexpect to create the script.
 
Old 09-27-2016, 08:57 PM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,865
Blog Entries: 3

Rep: Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053Reputation: 3053
Quote:
Originally Posted by bravored View Post
My boss wants expect and that is what he gonna get.
It would be very wise to get that in writing from him, even if you have to be tricky about how you do it, because it will eventually bite someone.

If you're trying to read two files at once in bash, you can do it with additional file descriptors.

Code:
$ cat file1
a
b
c

$ cat file2
1
2
3

$ while read -u3 sys; read -u4 pass; do echo S=$sys P=$pass;done  3<file1 4<file2
S=a P=1
S=b P=2
S=c P=3
Not all shells can do that. But you mention bash above and bash can do that.
 
Old 09-27-2016, 09:41 PM   #8
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDo, tinycore, Q4OS,Manjaro
Posts: 4,009

Rep: Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857
There are interesting tools to automate some part of this process. I transfer keys to my servers so I can ssh without passwords, then us mpssh to generate the data for additional filtering. When using mpssh you have a hosts file ( in ~/.mpssh ) that lists the hosts you will work on, optionally separated into lists using labels.

There are other tools similar to mpssh (dsh, etc) that work on multiple hosts, either in parallel or sequentially).

This is not the kind of thing I would use expect to automate. That said, I can see how it might well work as an exercise used to get someone to LEARN expect! Could this be a teaching/mentoring issue?
 
Old 09-28-2016, 09:11 AM   #9
ogniemi
LQ Newbie
 
Registered: Sep 2016
Posts: 3

Rep: Reputation: Disabled
I am not really sure what is your goal but check for pssh utility
 
Old 10-04-2016, 09:39 AM   #10
bravored
Member
 
Registered: Mar 2005
Distribution: RHEL, freeBSD, Solaris
Posts: 49

Original Poster
Rep: Reputation: 0
Hi All
Thanks for the comments

Google searches finally found something that works more to what I want (for now -- {now I need to get it to ready encrypted password})
------------------------------
#! /usr//bin/expect

set timeout 20
set user [lindex $argv 0]
set password [lindex $argv 1]
set prompt "$ "

;# -- main activity

proc dostuff { currenthost} {

;# do something with currenthost

send -- "ls -lrt\r"
return

}

;# -- start of task

set fd [open ./hostlist r]
set hosts [read -nonewline $fd]
close $fd


foreach host [split $hosts "\n" ] {

spawn /usr/bin/ssh $user@$host

while (1) {
expect {
"no)? " {
send -- "yes\r"

}

"password: " {
send -- "$password\r"

}

"$prompt" {
dostuff { $host }

break
}
}
}


expect "$prompt"
send -- "exit\r"
}

expect eof

Replaced send -- "ls -lrt\r" with send -- "uptime\r" : to get the result I was looking for.

chmod 700 file
-----------------------

Replace hostlist with ip or ips you want

-------------------------

Run it as ./file userid password

---------------------------

Do it at your RISK!

Practice! TEST! Redefine
 
  


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
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 AM
Issue in running commands in expect script from shell script yadvinder Programming 0 05-31-2012 04:07 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:43 PM.

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