Brent Dodson

Web Programming Page

eightball Register
Unregistered users click here to register registered users can post their website here
eightball Science Humor:
An egghead is one who stands firmly on both feet, in mid-air, on both
sides of an issue.
-- Homer Ferguson
eightball Chuck Norris Humor:
Chuck Norris has already been to Mars; that's why there are no signs of life there.
eightball Jobs:
Need a Job?
Check these out!!!!!
Jobs available
Password-less ssh login -
// description of your code here


http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html
Re-use existing ssh-agent -
// dot this script, don't just run it as a script (which would run it in its own sub-shell) because you need the variables to be set in the context of your current shell.


#!/bin/bash

for FILE in $(find /tmp/ssh-???*[0-9]* -type s -user ${LOGNAME} -name "agent.[0-9]*")
do

GOT_AGENT=0

SOCK_PID=${FILE##*.}

PID=$(ps -fu${LOGNAME}|awk '/ssh-agent/ &&( $2=='${SOCK_PID}' || $3=='${SOCK_PID}' || $2=='${SOCK_PID}' +1 ) {print $2}')

SOCK_FILE=${FILE}

SSH_AUTH_SOCK=${SOCK_FILE}; export SSH_AUTH_SOCK;
SSH_AGENT_PID=${PID}; export SSH_AGENT_PID;

ssh-add -l | grep : >/dev/null
if [ $? = 0 ]
then
GOT_AGENT=1
echo "Agent pid ${PID}"
break
fi
echo "Skipping pid ${PID}"

done

if [ $GOT_AGENT = 0 ]
then
ssh-add
fi


Before I used the find I was using this, but it's not as accurate, and the socket file is more important than the PID when you're trying to connect to an existing agent, so it makes sense to me to start with that.
(unless your find would be horribly slow for some reason)


for PID in $(ps -furoot | awk '/ssh-agent/{print $2}')
do
let SOCK_PID=PID-1
SOCK_FILE=$(ls -d /tmp/ssh-???*${SOCK_PID}/agent.${SOCK_PID})


Parallel SSH sessions -
Run the same command on lots of hosts in parallel via SSH.

Put the command(s) you want to run in a shell script called 'to_send.sh' in the current directory.
You need Parallel::ForkManager from http://search.cpan.org/dist/Parallel-ForkManager/
If you can't install it systemwide you can put ForkManager.pm in a directory called Parallel in the current directory.


#!/usr/bin/perl -w
#
#
#
use strict;
use Parallel::ForkManager;

my $max_procs = 30;
my @hosts;
my $timeout = 30;
my $remote_file = '/tmp/parallel-job.sh';
my $send_file = 'to_send.sh';

my $input = shift;
die "Usage: $0 FILE\n" if ! $input;
open (INPUT, $input) || die "Can't read '$input': $!\n";
while (defined (my $line = )) {

chomp $line;
push @hosts, $line;

}
close INPUT;

#
# hash to resolve PID's back to child specific information
#

my $pm = new Parallel::ForkManager($max_procs);

my $left = @hosts;

# Setup a callback for when a child finishes up so we can
# get it's exit code
$pm->run_on_finish(
sub { my ($pid, $exit_code, $ident) = @_;
print STDERR "$ident ->finished. PID:$pid exit:$exit_code\n";
$left--;
}
);

$pm->run_on_start(
sub { my ($pid,$ident)=@_;
print STDERR "$ident ->started. PID:$pid\n";
}
);

$pm->run_on_wait(
sub {
printf STDERR "Status: %d host%s left.\n", $left, $left == 1 ? "" : "s";
},
0.5
);

my $ssh_string = '/usr/bin/ssh -o StrictHostKeyChecking=no';

my $send_cmd = "cat <$remote_file\n";

open (FILE, $send_file) || die "Can't read '$send_file': $!\n";
while (defined (my $line = )) {

$line =~ s/\$/\\\$/g;
$send_cmd .= $line;

}
close FILE;

$send_cmd .= "rm $remote_file\nEOT\n/bin/sh $remote_file\n";

for my $child ( 0 .. $#hosts ) {

my $host = $hosts[$child];

my $pid = $pm->start($host) and next;

#
# This code is the child process
#

my @args = (qq~$ssh_string $host '$send_cmd'~);

my $return = eval {

print STDERR "Status: host:$host done:$child left:$left\n";
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
return system(@args);
alarm 0;

};

$return = 25443 if ! defined $return;

if ( $return != 0 ) {

my $exit_value = $return >>8;
my $signal_num = $return &127;
my $dumped_core = $return &128;

if ( $exit_value == 254 ) {
printf "%25s : Logins disabled\n", $host;
}
elsif ( $exit_value == 1 ) {
printf "%25s : Connection timed out\n", $host;
}
elsif ( $exit_value == 99 &&$signal_num == 99 ) {
#
# Normally due to a password prompt or very slow host
#
printf "%25s : Session timed out\n", $host;
}
elsif ( $exit_value == 255 &&$signal_num == 0 ) {
printf "%25s : Connection failed\n", $host;
}
else {
printf "%25s : SSH Returned error: [$exit_value] [$signal_num] [$dumped_core]\n", $host;
}

}
print STDERR "$host ->$child finishing...\n";

$pm->finish($child); # pass an exit code to finish

}

print STDERR "Waiting for last hosts...\n";
$pm->wait_all_children;
print STDERR "All hosts done.\n";

ssh-agent script -
// description of your code here


#!/bin/bash

SSH_ENV="$HOME/.ssh/environment.$HOSTNAME"

if [ -x /usr/bin/ssh-agent ]
then
SSH_AGENT=/usr/bin/ssh-agent
SSH_ADD=/usr/bin/ssh-add
else
echo "Can't find ssh-agent"
SSH_AGENT=/bin/false
SSH_ADD=/bin/false
fi

start_agent () {

printf "Starting new SSH agent... "
$SSH_AGENT >"${SSH_ENV}"
if [ $? = 0 ]
then
echo "OK"
printf "3s|^echo|#echo|\nw\n\q\n" | ed "${SSH_ENV}" >/dev/null 2>&1
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}"
else
echo "ERROR"
fi
$SSH_ADD -l | grep : || {
$SSH_ADD;
}

}
#
# Source SSH settings, if there
#
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}"
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -fu$LOGNAME | grep ${SSH_AGENT_PID}.*ssh-agent$ >/dev/null
if [ $? != 0 ]
then
start_agent;
else
$SSH_ADD -l | grep : || {
echo "Agent is running, but has no keys..."
$SSH_ADD
}
fi
else
start_agent;
fi


And add this to .bashrc / .kshrc


alias ssh_agent='. $HOME/.ssh/ssh_agent'
SSH_ENV="$HOME/.ssh/environment.$HOSTNAME
ssh pub key auth -
// description of your code here
http://snippets.dzone.com/posts/show/238

// insert code here..
Mirror a directory between my "old" and "new" web server/ftp -
On Local (new) server

rsync -zavurR --delete --links --rsh="ssh -p -l " old.server.tld:/remote/directory /
Reverse SSH Tunnel -
Create a Reverse SSH Tunnel back to you development application from a public facing server.

In config/tunnel.yml


development:
public_host_username: SSH_USERNAME
public_host_password: SSH_PASSWORD
public_host: SSH_SERVER_HOST_NAME
public_port: 8868
local_port: 3000




In lib/tasks/tunnel.rake


namespace :tunnel do
desc "Start a ssh tunnel"
task :start =>:environment do
SSH_TUNNEL = YAML.load_file("#{RAILS_ROOT}/config/tunnel.yml")[RAILS_ENV]

public_host_username = SSH_TUNNEL['public_host_username']
public_host = SSH_TUNNEL['public_host']
public_port = SSH_TUNNEL['public_port']

local_port = SSH_TUNNEL['local_port']

puts "Starting tunnel #{public_host}:#{public_port} \
to 0.0.0.0:#{local_port}"

exec "ssh -nNT -g -R *:#{public_port}:0.0.0.0:#{local_port} \
#{public_host_username}@#{public_host}"
end
end

Terminate a frozen SSH session -

And here's a quick key sequence to terminate those frozen sessions(or any session for that matter):

. [return] ~
connect to iphone via scp / winftp -
How do I connect to my iPhone or iPod Touch?

* IP address of your iPhone/iPod Touch into Host name (You will find the IP address, if you go to yours iPhone/iPod Touch menu Settings >Wi-Fi and choose the network you are using);
* root into User name;
* alpine or dottie (depends on firmware version) into Password;
* You may need to set longer Server response timeout on Connection tab.
Push your public key to a server -
If you've already created your ssh keys locally do this to push the public key to a server so you won't have to login to the server everytime you ssh or cap deploy.


cat .ssh/id_rsa.pub | ssh deploy@myserver.com "cat >>.ssh/authorized_keys2"

 Use OpenOffice.org        Spread Firefox Affiliate Button

For any questions or enquiries, i can be reached at my email
I look forward to hearing from you

Copyright © 2010 brentdodson.com