Use of expect in shell script

hi all ,
can anyone explain me this script particularly why expect is used here.
set timeout -1
spawn sftp [email protected]
match_max 100000
expect -exact "Connecting to 11.11.1.34...\r
sftp> "
send -- "cd /opt/app/policies/data\r"
expect -exact "cd /opt/app/policies/data\r
sftp> "
send -- "get potcontrol.dat\r"

"expect" is a particularly useful scripting tool when you need to make a script that performs an interactive, multi-command dialog with another utility, like FTP or SFTP (or in the olden days, I used it a lot with Kermit).
In your example, "expect" is being used to launch the sftp client to connect to some sftp server, switch to a certain directory and then fetch a copy of a certain datafile, all while expecting precise return strings coming back from the sftp client.
Certain tools and apps, like ftp, sftp and others, cannot be simply scripted via ordinary shell script methods to feed strings of commands to those tools and apps, because of unusual ways in which they open up their own separate /dev/tty to interact with the command line user, hence "expect" was created and is very useful for scripting dialogs with such tools and apps.
For instance, if you tried to make a typical "here document" type of shell script like this:
#!/usr/bin/sh
ftp <<END_CMDS
open some.ftp.host
myuser
mypassword
cd /some/dir/path
get somefile.dat
bye
END_CMDS
exit 0
You'd find that a typical ftp client will not read the commands from the shell script's /dev/tty and will still try to interactively prompt for user, password, etc. or it might just hang indefinitely waiting for input that will never come to it.
There are other shell scripting tricks to get a "here document" script to function with utilities like ftp/sftp/others, but if you have "expect" installed on your system, it's very simple to run "autoexpect" to record an interactive session of what you want to do, which will then automatically create the "expect script" for you.
"Expect" was created by our (USA) tax dollars, and is free for anyone to use: See the home page here for the whole story: http://expect.nist.gov/

Similar Messages

  • Trying to run program off network location using GPO with Power shell script.

    Hello All,
    Not much of a script writer. I am giving it a shot.  My issue is that I need to run a application update across our network and I am trying to do it with as little hands on as possible. So I was planning to push a GPO with a power shell script in it
    to run the program with elevated privileges. 
    Little background:
    We are running on a domain and end users do not have admin rights.
    The application is stored on a share on our network that is open to all domain users.
    The installer user name and password is a temp one and will only be valid for the 30 min window when everyone logs in at the beginning of the day.
    So this is what I have so far.
    $username = "USER"
    $password = "PASSWORD"
    $credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
    Start-Process PSQLv11Patch_Client_x86.msp -Credential ($credentials) -WorkingDirectory \\Server\Folder\Folder1\Folder2\filder3\PSQLv11sp3_x32\
    But for some reason I keep getting :
    Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
    At line:10 char:1
    + Start-Process PSQLv11Patch_Client_x86.msp -Credential ($credentials) -WorkingDir ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
    Any help you could give would be great.
    Thanks,
    jdfmonkey

    Hi jdfmonkey,
    Has anyone provided an answer to your original question?  I am trying to use Start-Process to launch a process using another logged in user's credentials, and am not able to get it working:
    $cred=Get-Credential
    start-process Process.exe-WorkingDirectoryC:\Scripts-Credential$cred
    I get the same error that you mentioned:
    start-process : This command cannot be run due to the error: The system cannot find the file specified.
    At C:\Scripts\Process.ps1:2 char:1
    + start-process Process.exe -WorkingDirectory C:\Scripts -Credential ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
    When I leave off the credentials:
    start-processProcess.exe-WorkingDirectoryC:\Scripts
    It works correctly.  Does anyone have a solution to make this work correctly?
    Please ask your own question.  You issue is nothing like the current thread.  You clearly are using a user account that has no access to the folder.  It is a permissions issue.  It is not a scripting issue.
    If you need further help please start your own question.
    ¯\_(ツ)_/¯

  • How to use encoded password in shell script ?

    Hi everybody,
    For make a load file in oracle table, I'm using a LKM File to Oracle updatding with a sqlldr (shell script).
    My problem is very simple : For security reason in this shell-script , I don't want to see in clear the password of the Oracle user for database connection. Is it possible and how do it ?
    Thanks in advance

    Hi-
    Yes it is possible by the way of KM custamization, You need to modify LKM call sqlldr via jython step to pass the user info and password as variable instead of ODI method. Try this command in your KM step:
    userid=#GLOBAL.user/#GLOBAL.psw
    Hope this will work for you.
    Thanks,
    Saravanan Rajavel

  • Using LIKE in a shell script

    I would like to use a 'Like' and wildcards in a shell script. Something like
    If $F1 like '*ABC*' then
    xxxx
    What would the correct syntax for this be?
    Thanks!

    You could do:
    if [ ! -z "`echo ${F1} | grep "ABC"`" ] then
    fi

  • Use Launchd to run shell script at boot

    Hello
    I'm trying to create a plist file that will run a shell script at bootup of the server.
    I have the following xml so far in the plist file:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>com.apple.mymount</string>
    <key>ProgramArguments</key>
    <array>
    <string>/mount-script/./mount.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    </dict>
    </plist>
    The shell script is just a mount command:
    #!bin/sh
    #MOUNT folder X ON server Y
    sudo -u _www mount_smbfs -f 777 -d 777 //username:password@server/folder /mnt/mount
    The shell script works fine from the terminal.
    I can see the plist has loaded after bootup but the script doesn't run.
    What am I doing wrong?
    Would need some help please!

    First problem:
    <string>com.apple.mymount</string>
    com.apple is (or at least should be) reserved for Apple scripts, not your own. This should be renamed to your own domain (or your own name if you don't have a domain).
    In addition the .plist file name should match (so if you rename the script Label to be 'com.my.mount' then the .plist should be com.my.mount.plist. You don't include the file name in your post to know if you're already doing that.
    Second:
    <string>/mount-script/./mount.sh</string>
    What? That looks wrong to me. Of course, it could be right, but the '/./ in the middle worries me, so I just can't think it is.
    Where is the script, really?
    Third:
    sudo -u _www mount_smbfs -f 777 -d 777 //username:password@server/folder /mnt/mount
    No. Never use 'sudo' within a launchd task. If you need the task to run as a different user then use the launchd keyword UserName to specify that username:
    <key>UserName</key>
    <string>_www</string>
    I can see the plist has loaded after bootup but the script doesn't run.
    If none of the above fixes it then look at the logs and tell us what it has to say. /var/log/system.log

  • Loading incomplete using sqlplus_exec_template.sql  in shell script

    Hi,
    I've tried to automate the dataloading by calling sqlplus_exec_template.sql for each mapping in the shell script bash. (I comment out the last exit line in the script). But the loading were always partially complete for those that take longer time, says more than 5 or 10min. I've also tried to call each mapping in different shell and got the same result. How do you work around this? Below is part of the lines in my shell script. Thanks.
    sqlplus -s test_rta/test_rta << EOF
    @sqlplus_exec_template.sql 'TEST_RTR' 'TEST_LOC' 'PLSQL' 'CONSOL_SUP_MAP' ',' ','
    @sqlplus_exec_template.sql 'TEST_RTR' 'TEST_LOC' 'PLSQL' 'CONSOL_PART_MAP' ',' ','
    EOF

    I 've tried to use :
    sqlplus username/[email protected] @C:\OWB\owb\rtp\sql\sqlplus_exec_template.sql
    rtschema OWBLOCATION SQL_LOADER CRD_APPL_MAPPING "," ","
    where OWBLOCATION I have used the deploy location of the mapping for my case, created within OWB. The value is CCS_DEPLOY.
    But the error is the same :
    ======================================================
    Stage 1: Decoding Parameters
    | location_name=CCS_DEPLOY
    | task_type=SQL_LOADER
    | task_name=CRD_APPL_MAPPING
    Stage 2: Opening Task
    declare
    ERROR at line 1:
    ORA-20001: Task not found - Please check the Task Type, Name and Location are
    correct.
    ORA-06512: at line 261
    ========================================================
    could you tell me the SQL used by WB_RT_API to get the task from runtime schema. So that I could check the correct location myself.
    Or how could I view the pl/sql of WB_RT_API_EXEC which is wrapped.
    Thx for your help.
    Rg.
    CH

  • Using fsch inside a shell script?

    I like the premise of using fsch because build times are
    greatly reduced. However, I want to integrate fsch better into a
    more automated build system, even Ant. Anyone know how to do this?
    BTW, I'm using a Unix based system: Mac OS X. I guress I'd like to
    be able to call something more like 'fsch mxmlc .....'
    I have played with the Flex apache module of Flex 3 but
    ultimately want any build error to appear back in my text editor
    inside of inside the browser. So I have the apache module working
    well but am now trying to get fcsh to work inside a shell script as
    a single call - how do I call out to the running process?
    Thanks,
    Phil

    You could do:
    if [ ! -z "`echo ${F1} | grep "ABC"`" ] then
    fi

  • How to use CONCSUB program in shell script program

    I have to write a shell script which will further call the concurrent program.
    My concurrent program is having 3 parameters. Plz send some suggestion
    R's
    Vijay

    Pl use the search feature to locate threads that have discussed this issue before. Some examples -
    Re: modify .out file
    Re: shell script using CONCSUB utility
    HTH
    Srini

  • I am trying to use automator to make a simple app that when I open it, it comes up with a dialog box that asks for a link that you want to download. and it will download it for you using the curl -O shell script in Terminal. How would I do it?

    I want this so I can just download anything I want by just having the link and I have made a few things with shell scripts in automator before, just never anything where i need to input a value into the shell script.

    Easy:

  • Shell Script to change filename.

    I'm need to use the Run Shell Script action to change a filename. Basically I need to drop the ".app" extension of an app on the desktop to get it to appear as a folder.
    I know I need to use:
    mv TemporaryApp.app TemporaryApp
    but am not sure what to use before this to get the shell script to recognize the file.
    Any help appreciated.
    Thanks,
    Kevin

    Thanks, John.
    But my requirement as follows,
    Is there a way to change user password using passwd command in shell script? I don't want to use expect. use the command passwd, it will ask for old password and then the new password twice.But I want to use it in a script without my interaction. I want to provide new password in a script a than run passwd to change user password. Problem of using passwd in shell script is that passwd requires user interaction to change password and script stops. My question was is there a way to use passwd without interaction or is there other way to change password using shell script.
    Thanks
    Ram

  • How to tell if item is a folder or volume with applescript or shell script?

    I am working on a service in Automator that will eject the selected drive when it is finished. Unfortunately automator can only tell the difference between files and folders for the service menu.
    Im wondering if if i can use an applescript or shell script that would check if the selected folder is a volume or folder and quit the action if its a folder. I figure the best way to do this is have it determine if the items path is in /Volumes/, but i am completely new to the process and have no idea how to do this.
    My automator workflow looks like:
    Ask for Confirmation
    Get Selected Finder Items
    Shell Script
    I have the path of the folder selected being passed into the shell script, but after its in there i dont know how to determine if its a volume or not.
    Thanks!

    K T,
    I completely missed that section of the forums, sorry
    Neil,
    I had that kind of script set up but it was like this:
    on run {input, parameters}
    tell application "Finder"
    kind of item parameters
    end tell
    end run
    And i just figured out that i had to change the first line to "on run {parameters}" and now it works great.
    Thanks for the help, it got me going in the right direction

  • Displaying an alert (Applescript or otherwise) from shell script?

    I have a point in a shell script where I'd like to put up an alert dialogue on a particular error condition. The script runs in the background and doesn't have a terminal window. I tried writing a little applescript that uses the applescript alert command and call it using osascript from my shell script, but it doesn't work. If I enter "osascript ~/myscript.scpt" in a terminal window, I get the error message "/Users/Ted/myscript.scpt: execution error:No user interaction allowed. (-1713)" (If I run myscript.scpt from the script editor it does what I want it to do.) Any ideas? I suppose I could have my shell script create a file in some folder and have my applescript be triggered as a folder action, but that seems pretty roundabout!

    Thanks -- I apologize for not doing a more thorough search! Yes, that comes very close to addressing my need, but I have run into one difficulty. If I run my shell script from the terminal (pasting it in) everything works fine. If I run it as a packaged app (with Platypus) everything works like it's supposed to, except that the alert applescript (it's just a one line script) briefly flashes the alert dialogue when it's supposed to and then dies, rather than waiting 10 seconds or until I click OK. I'll have to experiment and see if I can tell what's going on.

  • Unable to check Null condition in a Shell Script

    Hi,
    When I am using to check the Null Condition To check for a Running Java Process its failing to check.
    I am using variable in a shell script
    DPID=$(ps -ef |grep - i java|grep -v grep)
    if [ "$DPID"="" ' ];then
    else
    fi
    If the DPID has Null value then its getting executed but if the DPID has value its not getting into the else part of the loop
    Thanks and Regards

    You could try -c to make grep return a count of matches, then check if this count is zero or not.
    DPID=`ps -ef |grep - i java|grep -vc grep`
    if [[ $DBID -ne 0 ]]; then
    else
    fi

  • Update command in Shell script

    Hi friends
    sqlplus -s / <<END
    set feedback on;
    update tran2 set sno=1;
    exit;
    END
    when i am using update command in shell script like above
    it is updating the database well...but i just want to know how many rows it is updating and i dont want to commit
    for that
    sqlplus -s / <<END
    set feedback on;
    update tran2 set sno=1;
    set feedback off;
    rollback;
    exit;
    END
    It's working fine
    is there any other method to do the same

    Well what's exactly your requirement? The current requirement doesn't make a lot of sense.
    How many row is going to be updated depends on where clause, if you have no where clause that essentially updating whole table, the number of row updated is count of your rows.

  • Configuring Shell Script

    I am a newbie to Shell Scripting.
    I am trying to set up a Unix Resource on my IDM system.
    I have a Test Machine (which is Remote, of course). Naturally, the machine is Unix-based. Within this machine is a Unix Database, which is MySql-based.
    On this machine, I created a User (complete with full Administrator rights and privileges).
    And, to this user's Home Directory, I added certain a scripts which is meant for "*Creating a new User"* in the Unix Database. This script is written in PERL language.
    I created the Shell Resource, by using the IDM sample Shell Script files (the ones located in the IDM sample-folder). Naturally, I modified certain attributes, removed others, etc, etc.
    My problem is : the Get Resource Action and Get Result Handler.
    These two things are MANDATORY when configuring Shell Script. I have absolutely no idea how to write those scripts.
    I have searched everywhere online (sun docs, forums, even google).
    Could anyone please give me an idea of how to code/write the script for those Actions?
    Thanks

    Thanks for your replies.
    I was able to get the "Create User" process to work. What I did was design a custom-script which is suited to my own Unix environment.
    t worked (or, it seemed to work).
    In IDM, i got the following error :
    *Error detected: [Adding account to hosts and creating quotas]&amp;amp;amp;#xD;&amp;amp;amp;#xA;Default shell for netpasswd is /usr/alt/uadm2/bin/nologin. This can be changed later.&amp;amp;amp;#xD;&amp;amp;amp;#xA;Default shell for sui-dev is /usr/alt/uadm2/bin/nologin. This can be changed later.&amp;amp;amp;#xD;&amp;amp;amp;#xA;Default home path for sui-dev is /home. This can be changed later.Default shell for ssl3 is /usr/alt/uadm2/bin/nologin. This can be changed later.&amp;amp;amp;#xD;&amp;amp;amp;#xA;Default shell for sui-test is /usr/alt/uadm2/bin/nologin. This can be changed later.&amp;amp;amp;#xD;&amp;amp;amp;#xA;Default home path for sui-test is /home. This can be changed later.[Adding user to groups]&amp;amp;amp;#xD;&amp;amp;amp;#xA;[Creating home directory]&amp;amp;amp;#xD;&amp;amp;amp;#xA;DUMMY - ssh -2 -l root sui-dev "/usr/alt/uadm2/libexec/mkhome sui-dev /users1/u1/mjerome 44444 500 550 mjerome"&amp;amp;amp;#xD;&amp;amp;amp;#xA;DUMMY - ssh -2 -l root sui-test "/usr/alt/uadm2/libexec/mkhome sui-test /users1/u1/mjerome 44444 500 550 mjerome"&amp;amp;amp;#xD;&amp;amp;amp;#xA;[all done].*
    Result Code = 120.
    Error
    *com.waveset.util.WavesetException: An error occurred adding user 'mjerome' to resource 'Unix Administration'. com.waveset.util.WavesetException: Error detected: . com.waveset.util.WavesetException: Error detected: [Adding account to hosts and creating quotas] Default shell for netpasswd is /usr/alt/uadm2/bin/nologin. This can be changed later. Default shell for sui-dev is /usr/alt/uadm2/bin/nologin. This can be changed later. Default home path for sui-dev is /home. This can be changed later.Default shell for ssl3 is /usr/alt/uadm2/bin/nologin. This can be changed later. Default shell for sui-test is /usr/alt/uadm2/bin/nologin. This can be changed later. Default home path for sui-test is /home. This can be changed later.[Adding user to groups] [Creating home directory] DUMMY - ssh -2 -l root sui-dev "/usr/alt/uadm2/libexec/mkhome sui-dev /users1/u1/mjerome 44444 500 550 mjerome" DUMMY - ssh -2 -l root sui-test "/usr/alt/uadm2/libexec/mkhome sui-test /users1/u1/mjerome 44444 500 550 mjerome" [all done]. com.waveset.util.WavesetException: Result Code = 120.*
    From what is written there, I could see that the user was INDEED created. (Just to be sure, I checked my Unix machine. And, yes, the user was created. I also checked the MySQL database, and the user is there, too)
    However, I keep getting the above error in IDM.
    I can't figure out any explanation besides the fact that, perhaps, this is because I do not have a RESULT HANDLER script in place yet.
    And, this is where my problem is : some documentation on Shell Script say that Result Handlers are needed only for two Actions : GetUser and GetAllUsers.
    However, other documents claim that Result Handlers are needed for ALL resource actions. (In other words, if you have a Create User resource action, then you must have a Create-User result handler).
    I don't even know which of these claims is true.
    Any help, please?

Maybe you are looking for

  • Error while running ADF application in integrated weblogic server

    Hi, I'm new to Jdeveloper. I'm using Jdeveloper 11, when i try to run a sample ADF application in Jdeveloper integrated weblogic server i am getting following errors. Can anyone help me how to fix it? [06:44:04 PM] Redeploying Application... <AzUtil>

  • Not possible to reverse the document in financial accounting

    Not possible to reverse the document in financial accounting Message no. F5673 Diagnosis Document '5000082' in company code '1000' should be reversed.  However, this document was not posted in the Financial Accounting module, VBRK is, rather, of type

  • Modificatin in standard Purchase order smart forms

    Hi all,    i m facing a prob.plz give guidelines to solve this problem.    I m copying standard smartform /SMB11/MMPO_L  in a Prog. I want to add one colom in main window and in header window (WINHEADER and MAIN window) when ever i m adding one colum

  • Where can I purchase and download adobe acrobat x standard for Mac OSx?

    Where can I purchase and download adobe acrobat x standard for Mac OSx?

  • Find window behavior

    When I search using the Find Command (Command/F), I can't always expand the name field to read the entire name. Sometimes I can place the mouse at the right edge of the Name column and I get a double arrow which allows me to expand the field and read