- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- need info on expect script
Operating System - Linux
1819796
Members
3133
Online
109607
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2007 09:14 PM
тАО07-26-2007 09:14 PM
hi all,
i am trying to write a expect script.which doeas not ask 4 a password.The password shd be inside the expect script.
ex:i want to connect to root@34.343.4.3 pasword=xxxxx.
how to do it inside the script.give an example.thnks in advance
i am trying to write a expect script.which doeas not ask 4 a password.The password shd be inside the expect script.
ex:i want to connect to root@34.343.4.3 pasword=xxxxx.
how to do it inside the script.give an example.thnks in advance
Solved! Go to Solution.
- Tags:
- expect
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2007 10:07 PM
тАО07-26-2007 10:07 PM
Solution
It's pretty easy. The quick-whip-up to catch a few common things is as follows:
--- start ---
#!/usr/bin/expect
log_user 0
set my_passphrase "passphrase"
set my_password "password"
spawn "/usr/bin/ssh" "root@34.343.4.3"
while {1} {
expect {
"continue connecting (yes/no)?" {
send "yes\n"
}
"passphrase" {
send "$my_passphrase\n"
}
"password" {
send "$my_password\n"
}
-re "(#|\\\$|%) $" {
log_user 1
send_user "Shell prompt found. Dropping to shell:"
send "\n"
break
}
}
}
interact
--- end ---
This has separate variables for pass phrases and pass words. Passphrases are for local keys if used. Passwords are for remote authentication.
It also catches the havent-ssh'd-to-the-box-before thing too.
The last bit (the '-re' bit) is the line to see if it finds a shell prompt. If not, there's a built-in 10 second timeout which will dump you out.
The 'interact' at the end should be fairly self explanatory.
If you want to pass the password/passphrase, then you'll need to mess around with the '[lindex argv]' TCL variable stuff.
If all else fails, try using 'autoexpect'. It will generate a custom script for you to modify.
--- start ---
#!/usr/bin/expect
log_user 0
set my_passphrase "passphrase"
set my_password "password"
spawn "/usr/bin/ssh" "root@34.343.4.3"
while {1} {
expect {
"continue connecting (yes/no)?" {
send "yes\n"
}
"passphrase" {
send "$my_passphrase\n"
}
"password" {
send "$my_password\n"
}
-re "(#|\\\$|%) $" {
log_user 1
send_user "Shell prompt found. Dropping to shell:"
send "\n"
break
}
}
}
interact
--- end ---
This has separate variables for pass phrases and pass words. Passphrases are for local keys if used. Passwords are for remote authentication.
It also catches the havent-ssh'd-to-the-box-before thing too.
The last bit (the '-re' bit) is the line to see if it finds a shell prompt. If not, there's a built-in 10 second timeout which will dump you out.
The 'interact' at the end should be fairly self explanatory.
If you want to pass the password/passphrase, then you'll need to mess around with the '[lindex argv]' TCL variable stuff.
If all else fails, try using 'autoexpect'. It will generate a custom script for you to modify.
One long-haired git at your service...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2007 10:47 PM
тАО07-26-2007 10:47 PM
Re: need info on expect script
thnks the script really helped and its working.i have assigned the points.Can u explai a the above script in detail.Thnks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2007 12:13 AM
тАО07-27-2007 12:13 AM
Re: need info on expect script
The man page for 'expect' is pretty straight forward, but I'll explain away.
log_user 0
This tells expect to not show any of the spawn's output. Makes it looks neat 'n pretty :P
set
Simple variable assignment. Strictly speaking, the quotes aren't needed for plain text strings (either here, or in the expect strings), but it makes it easier to differentiate what's what.
spawn
This forks off the command, tieing STDIN, STDERR and STDOUT to expect for it to parse/manipulate.
The 'while {1} {' loop is so we don't have to repeat the expect strings over and over.
expect {
The simple form of 'expect' is to take the match string and the command all on the one line (i.e. 'expect "passphrase" send "$my_passphrase\n"').
But as we have multiple possible strings which might match, we use the more open form of the expect command. From here, we list the possible match texts:
"continue connecting (yes/no)?"
"passphrase"
"password"
-re "(#|\\\$|%) $"
The first three should be straight forward and easy to understand. The fourth does a 'regular expression' match on the STDOUT string from the spawned process.
The 'send' command is used to send a string to the spawned process's STDIN.
The 'send_user' is to send text to the user's TTY.
The 'break' is to break out of the while loop.
And finally, we come to the:
interact
As I think I mentioned before, this throws back interactive control to your TTY for you to do things with.
log_user 0
This tells expect to not show any of the spawn's output. Makes it looks neat 'n pretty :P
set
Simple variable assignment. Strictly speaking, the quotes aren't needed for plain text strings (either here, or in the expect strings), but it makes it easier to differentiate what's what.
spawn
This forks off the command, tieing STDIN, STDERR and STDOUT to expect for it to parse/manipulate.
The 'while {1} {' loop is so we don't have to repeat the expect strings over and over.
expect {
The simple form of 'expect' is to take the match string and the command all on the one line (i.e. 'expect "passphrase" send "$my_passphrase\n"').
But as we have multiple possible strings which might match, we use the more open form of the expect command. From here, we list the possible match texts:
"continue connecting (yes/no)?"
"passphrase"
"password"
-re "(#|\\\$|%) $"
The first three should be straight forward and easy to understand. The fourth does a 'regular expression' match on the STDOUT string from the spawned process.
The 'send' command is used to send a string to the spawned process's STDIN.
The 'send_user' is to send text to the user's TTY.
The 'break' is to break out of the while loop.
And finally, we come to the:
interact
As I think I mentioned before, this throws back interactive control to your TTY for you to do things with.
One long-haired git at your service...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP