How to do Terminal scripts?

Posted:
in macOS edited January 2014
I do a procedur everyday and I want to automate it



First I ssh to a remote machine, cd a bit, then ftp to another machine and cd a bit again.



This must be easy to do with some kind of terminal-scripts (do you call them that?), but how?



What I want to do one more time:

ssh name.server.com

password

cd /Folder

ls

ftp name.server.com

username

password

cd /Folder

ls



Thanks!

Comments

  • Reply 1 of 15
    thuh freakthuh freak Posts: 2,664member
    well, to make a shell script, you make a text file. the first line has to be specially formatted though.

    [code]# /path/to/executable</pre><hr></blockquote>

    where '/path/to/executable' is '/bin/sh' or '/bin/tcsh' [or other shell, or other exectuable, like '/usr/bin/perl']. each line after that can then be any command from the respective shell [or program]. lines starting with '#' [hash] are usually comments.



    unfortunately, in the situations u presented, i dont think a shell script will really do. first off, when a command is executed, the script waits for it to finish, before moving on [unless its ended with a '&']. and in either case, you can't throw ur password at it very easily. you could try piping you password to ssh's stdin, but i think ssh clears stdin before reading the pass [for extra protection from these kinds of scripts ]. and, assuming u can get ur password to the ssh program, u'd then be at name.server.com, and not at [your place], so ur script would just be waiting for ssh to exit. u could have a script, server side, that would do the end of the job [cd /Folder && ls], but now its 2 scripts, and we haven't gotten to the ftp yet.



    ftp is actually pretty easy to do. actually, i cheated, and used 'curl'. with ftp its not quite easy [or possible as i see it], but with curl its easy enough. heres the line for ftp:

    [code]curl <a href="ftp://userName:[email protected]/Folder/[/CODE"; target="_blank">]ftp://userName:[email protected]/Folder/</a></pre><hr></blockquote>;

    if u dont put the password, curl will prompt for it, but either way, it ls's the directory. [curl is a great lil program that can download any url (http, ftp, https, ..) in 1 line].



    edit: ubb keeps ass-fargin me



    [ 01-06-2003: Message edited by: thuh Freak ]</p>
  • Reply 2 of 15
    I don't have the time to look this up right now but I believe that you can bypass the password in ssh by generating some encryption key files. This means you only have to log in manually once per machine.



    NB It also means that anyone who can log into your account on that machine can log into the remote machine!



    Hopefully someone else (or google) can provide the details.



    edit: <a href="http://www.openbsd.org/cgi-bin/man.cgi?query=ssh&sektion=1&arch=&apropos=0&manpat h=OpenBSD+Current" target="_blank">the ssh man page</a> covers it but possibly not in enough detail. Search for 'hostbased'.



    another edit: slightly easier to read

    <a href="http://www.snailbook.com/faq/trusted-host-howto.auto.html"; target="_blank">O'reilly SSH book online FAQ</a>



    [ 01-06-2003: Message edited by: stupider...likeafox ]</p>
  • Reply 3 of 15
    pyr3pyr3 Posts: 946member
    The only way to automate this would be to make a startup script on the machine that you are ssh'ing into that runs everytime you log into the machine. There is no way that I know of off hand that will pipe commands into ssh from the client machine. A shell script that tried to do something like :

    #!/bin/bash

    ssh server.com

    cd /etc

    This would wait until the ssh process ended before running the cd command. It's a sequential list of commands to run, basically. You can make loops and if-else type stuff, but for the most part it's sequential.



    You COULD make one of those scripts on the server so that when you log into server.com it executes a script like:

    #!/bin/bash

    cd /Folder

    ls

    ftp ftp.server.com



    But I don't know how to pipe commands to ftp. That would be your problem. Search google for something like 'unix pipe commands ftp term' or replace unix with linux. Something along those lines should help you.
  • Reply 4 of 15
    scottscott Posts: 7,431member
    Hate to be a jerk but ...





    RTFM
  • Reply 5 of 15
    zadakzadak Posts: 50member
    [quote]Originally posted by Scott:

    <strong>Hate to be a jerk but ...





    RTFM</strong><hr></blockquote>



    If you can answer the question, do it. Else, get lost. Thank you.
  • Reply 6 of 15
    Well in the real UNIX world one just uses rsh to run a script on a remote system and uses rcp to remote copy files.
  • Reply 7 of 15
    zadakzadak Posts: 50member
    [quote]Originally posted by MrBillData:

    <strong>Well in the real UNIX world one just uses rsh to run a script on a remote system and uses rcp to remote copy files. </strong><hr></blockquote>



    Two problems for me...

    1. I want to make the hole process automatic, shall I first make rsh on my machine that starts the rsh on the remote machine?

    2. The file i different everyday so I just want to end up in the folder and then use get file.ext so sadly remote copy doesn't do it for me.



  • Reply 8 of 15
    pyr3pyr3 Posts: 946member
    [quote]Originally posted by Zadak:

    <strong>



    Two problems for me...

    1. I want to make the hole process automatic, shall I first make rsh on my machine that starts the rsh on the remote machine?

    2. The file i different everyday so I just want to end up in the folder and then use get file.ext so sadly remote copy doesn't do it for me.



    </strong><hr></blockquote>



    Do you know what the file will be everyday though? Do you know what the new name will be beforehand? If so you can still automate it. Or else you could still make an automated script that would prompt you with a list of files in that folder you want to be in and ask you to choose which one. If rsh and rcp do what I think they do then this would be insanely easy. Read up on rsh and rcp, they are probably what you are looking for.
  • Reply 9 of 15
    scottscott Posts: 7,431member
    [quote]Originally posted by Zadak:

    <strong>



    If you can answer the question, do it. Else, get lost. Thank you.</strong><hr></blockquote>



    I did. RTFM. If you're not willing to crack open a man page and read up you're not worth helping. Scripts are complex. You can just go online and ask, "How do I do this".
  • Reply 10 of 15
    [quote]Originally posted by Scott:

    <strong>Hate to be a jerk but ...



    RTFM</strong><hr></blockquote>



    He did and it said to post a question on AI. If you would have RTFM you would know this.
  • Reply 11 of 15
    I agree. I hate when people reply RTFM. These are forums where people can go for help. Yes there are things that go bryond the scope of online help, but at least make an attempt to help, or shut the h*ll up.



    Now on to an answer:

    rsh is the way to go. rsh will allow you to run a command at the local machine( or you can use cron to schedule it) for example if you wanted to get the contents of a log file /usr/local/log.log you could create a script like this on your local machine:

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

    #/bin/sh

    $var=`rsh remotemachine remotescript`

    echo "$var" &gt;&gt; /usr/local/log.log



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



    Your remote script would need to be written to do what ever function you were looking for, in this case read the remote /usr/local/log.log file.

    Then the above script, calls the remote script and sets the result to the variable $var, which it then puts into a log file on th elocal machine.



    You would need to set up rsh on both machines with a .rhosts file in the user directory of bith computers.



    For more info on setting up rsh check the man page or e-mail me: [email protected].



    Hope this helps



    -Quantum

    MCSE

    <a href="http://www.osxscripts.com"; target="_blank">http://www.osxscripts.com</a>;



    [ 01-09-2003: Message edited by: Quantum ]



    [ 01-09-2003: Message edited by: Quantum ]</p>
  • Reply 12 of 15
    [quote]Originally posted by Quantum:

    <strong>...

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

    #/bin/sh

    $var=`rsh remotemachine remotescript`

    echo "$var" &gt;&gt; /usr/local/log.log

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

    ...</strong><hr></blockquote>



    FYI:



    #!/bin/sh

    # Writes log to local system

    rsh remotemachine remotescript &gt; locallog



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



    #!/bin/sh

    # Writes log to remote system

    rsh remotemachine remotescript "&gt;" remotelog



    Note that rcp and rsh need to be available in the systems services in order for them to work. If ftp is the only available file transfer service, you are stuck jumping manually from system to system. If this is true, I suggest you create a few aliases/scripts to speed up your task on each of the systems that you will be using.



    Remember...

    There are always at least 10 ways to do the same thing in UNIX.

    There are usually 5 or 6 good ways to do it.

    There are 3 or 4 efficient ways to do it.

    <img src="graemlins/lol.gif" border="0" alt="[Laughing]" /> And 1 or 2 ways that only a handful of people would ever figure out how and why it works. <img src="graemlins/lol.gif" border="0" alt="[Laughing]" />



    [ 01-09-2003: Message edited by: MrBillData ]</p>
  • Reply 13 of 15
    zadakzadak Posts: 50member
    [quote]Originally posted by Scott:

    <strong>



    I did. RTFM. If you're not willing to crack open a man page and read up you're not worth helping. Scripts are complex. You can just go online and ask, "How do I do this".</strong><hr></blockquote>



    I diden't ask for a script, I did ask for a way to do it.

    Quantum gave me an excellent answer.
  • Reply 14 of 15
    defiantdefiant Posts: 4,876member
    I would get <a href="http://perso.club-internet.fr/phupe/english/YKIndex.html"; target="_blank">Youpi Key</a> to better automate the scripts and run them with a single button.



    [ 01-12-2003: Message edited by: Defiant ]</p>
  • Reply 15 of 15
    nevynnevyn Posts: 360member
    There's also a unix tool called 'expect' that actually parses incoming data to try to do whatever it is you want automated. Like you could have it look for "ogin:" (as a piece of Login or login) and have answers for the various questions embedded in your script.



    expect is overkill if it is always exactly the same text/filename etc.



    There's easily an entire library worth of books on this one subject - which is why answers often come back 'RTFM'. People that write scripts _well_ are very well paid. man sed, awk, perl, tcsh, bash - each has a way to reuse things you've typed before on Mac OS X.
Sign In or Register to comment.