// 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
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->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.
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.