Register
It is currently Sat Oct 25, 2014 1:45 am

Need a script to send code.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Dec 08, 2010 3:02 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
Hi everyone,

I built a script that connects to a machine via ssh, and then
executes a script looks for a valid directory in the home directory
out of a list of directories. The script that does the job of checking
directories must run on the server, so I copy it to the server, and
just ssh to it and run it.

But... I don't like having that hassle, because I might want to reuse it
in the future and will probably forget that I need to copy that script on the
remote machine. Is it possible to transfer this code somehow onto the server
to execute without actually making a file and copying it.

#!/usr/bin/env bash
dirs=$*
for dir in $dirs; do
if [[ -d $dir ]]; then
echo "$dir"
exit
fi
done

`ssh toMachine './findDir.sh $*'`


Top
 Profile  
 PostPosted: Mon Dec 20, 2010 10:29 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi afflictedd2!

There are a couple of ways that I like to handle cases like this, where I want to be able to centrally manage scripts that will be run on multiple machines.

Method #1: Shared directory on a file server.
have a machine dedicated as the file server, with a 'scripts' directory mounted on your remote machines via NFS or SSHFS. You can set it up in fstab to mount automatically. Then you only have to edit the script in one place, and the change will be there for all your machines.

Method #2: Place the script in a WEB accessible directory and run it like this:
wget -qO- /tmp/fin http://192.168.1.1/scripts/finDir.sh | sh

This way doesn't work well if you have to pass an argument to the script. Method #1 is probably the best and most consistent method, especially if you have to do this on multiple remote machines.

I hope this helps!
-Jeo


Top
 Profile YIM  
 PostPosted: Wed Jan 12, 2011 3:24 pm   

Joined: Tue Mar 31, 2009 3:10 pm
Posts: 7
You could do something like this

Use the add payload function to insert a payload into the script, when run the script will extract it into a variable and push it over the ssh commandline, then starting a shell. All in 1 connect ;-)
There is a size limit though, seems to be around 130KB
Don't feed it compressed files, the add_payload function will tar, lzma and encode it in base64

I use it to push a bash_profile, inputrc, vim folder and rc file, bunch of scripts etc.. to the remote system like this
CONFIG=" your bash profile, don't forget to escape quotes etc."
ssh $@ "perl -MMIME::Base64=decode_base64 -e 'print decode_base64 q#$PAYLOAD#' | lzma -d | tar x -f - -C /root;echo '$CONFIG' > /tmp/ssh_profile; exec bash --rcfile /tmp/ssh_profile"


start by:
./script.sh root@10.0.0.1

Will connect, extract the payload to root/, echo the bashrc in the CONFIG variable to a file on the remote box and start a bash session with you newly upload file as rcfile.


Hope this helps


Code:
#!/bin/bash

# max size is about 130K, if you get an error about argument list to long
# it means that the payload is to large

# you can specify a folder for the local extaction, if none provided the
# folder from where the script was run will be used
Get_payload() {
    tar="tar xv"
    if [ $1 ]; then
        tar="$tar -C $1"
    fi;
    if grep -q "^PAYLOAD:$" $0; then
        line=$(($(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1) + 1))
        tail -n +$line $0 | openssl base64 -d | lzma -d | $tar
        exit 0
    else
        echo 'PAYLOAD:' >> $0
        echo "No payload attached"
        exit 0
    fi;
}

# Globs don't work, point it to a folder or just give it multiple files (think tar commandline)
# .script.sh apl file1 folder1 file2 ...

Add_payload() {
    if grep -q "^PAYLOAD:$" $0; then
        line=$(($(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1) + 1))
        sed -i "$line,$ d" $0
        tar cv $@ | lzma -9 -e | openssl base64 >> $0
        exit 0
    else
        echo 'PAYLOAD:' >> $0
        tar cv $@ | lzma -9 -e | openssl base64 >> $0
        exit 0
    fi;
}

if [ "$1" = 'gpl' ]; then
    Get_payload $2
elif [ "$1" = 'apl' ]; then
    if [ "$2" = '' ]; then
        echo "Provide a payload";
        exit 1;
    fi
    shift
    Add_payload $@
fi;

# Extract our payload into a variable
match=$(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
payload_start=$((match + 1))
# By stripping the whitespace of the payload we can make it a little bit smaller (more data fits on the ssh commandline)
PAYLOAD=$(tail -n +$payload_start $0 | perl -pe 'tr/\x0A\x0D//d;' )

# Since our payload has been stripped of newlines and spaces wa cannot use openssl base64 to decode so we use perl.
exec ssh -A  -o CheckHostIP=no -o StrictHostKeyChecking=no -t -l root "$@" \
        "perl -MMIME::Base64=decode_base64 -e 'print decode_base64 q#$PAYLOAD#' | \
        lzma -d | tar x -f - -C /root; exec bash"


Last edited by tripkipke on Thu Jan 13, 2011 12:33 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Thu Jan 13, 2011 12:15 pm   

Joined: Tue Mar 31, 2009 3:10 pm
Posts: 7
Actually, you don't need the payload at all, you can just enter your script in a variable, echo it over the ssh commandline and start it.

If you have multiple scripts you will need to create multiple variables, echo all of them to files and run them. This could get very cumbersome (thats why you have the payload functions above)

do something like this, don't forget to escape quotes and dollarsigns inside the variable
Code:
script="#!/bin/bash
ls -al
"
exec ssh $@ "echo $script > /tmp/script.sh ; exec sh /tmp/script.sh"


Also keep in mind that there are size limitations this way. We are using the ssh commandline to get data across. I believe it is limited to around 130000 characters

enjoy


Top
 Profile  
 PostPosted: Tue Feb 22, 2011 3:26 pm   

Joined: Wed Nov 26, 2008 7:43 pm
Posts: 50
Location: Texas
awesome, thanks for the solutions.

Ted.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP