<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
  <title>Srijan Choudhary, all posts tagged: scripts</title>
  <link>https://srijan.ch/feed/all/tag:scripts</link>
  <lastBuildDate>Mon, 21 Jul 2025 16:45:00 +0000</lastBuildDate>
  <image>
    <url>https://srijan.ch/assets/favicon/favicon-32x32.png</url>
    <title>Srijan Choudhary, all posts tagged: scripts</title>
    <link>https://srijan.ch/feed/all/tag:scripts</link>
  </image>
  <sy:updatePeriod>daily</sy:updatePeriod>
  <sy:updateFrequency>1</sy:updateFrequency>
  <generator>Kirby</generator>
  <atom:link href="https://srijan.ch/feed/all.xml/tag:scripts" rel="self" type="application/rss+xml" />
  <description>Srijan Choudhary&#039;s Articles and Notes Feed for tag: scripts</description>
  <item>
    <title>gcloud_ssh</title>
    <description><![CDATA[gcloud_ssh A simple script that finds a google cloud compute VM by IP address across all projects of an organization and runs gcloud ssh to it. #!/bin/bash GCLOUD_SSH_FLAGS="--internal-ip" # Get organization ID dynamically get_org_id() { gcloud organizations list --format="value(name)" --limit=1 2>/dev/null | sed 's|organizations/||' } search_and_connect() { local ip_address=$1 echo "Searching …]]></description>
    <link>https://srijan.ch/notes/2025-07-21-001</link>
    <guid isPermaLink="false">tag:srijan.ch:/notes/2025-07-21-001</guid>
    <category><![CDATA[scripts]]></category>
    <dc:creator>Srijan Choudhary</dc:creator>
    <pubDate>Mon, 21 Jul 2025 16:45:00 +0000</pubDate>
    <content:encoded><![CDATA[<h2>gcloud_ssh</h2><p>A simple script that finds a google cloud compute VM by IP address across all projects of an organization and runs gcloud ssh to it.</p>
<pre><code class="language-bash">#!/bin/bash

GCLOUD_SSH_FLAGS="--internal-ip"

# Get organization ID dynamically
get_org_id() {
    gcloud organizations list --format="value(name)" --limit=1 2&gt;/dev/null | sed 's|organizations/||'
}

search_and_connect() {
    local ip_address=$1

    echo "Searching for IP: $ip_address across organization..."

    # Get organization ID
    ORG_ID=$(get_org_id)
    if [ -z "$ORG_ID" ]; then
        echo "Failed to get organization ID. Make sure you have organization-level access."
        exit 1
    fi

    # Search for instance with this IP address
    RESULT=$(gcloud asset search-all-resources \
        --scope=organizations/$ORG_ID \
        --query="$ip_address" \
        --asset-types='compute.googleapis.com/Instance' \
        --format=json 2&gt;/dev/null)

    if [ -z "$RESULT" ] || [ "$RESULT" = "[]" ]; then
        echo "IP address $ip_address not found in organization."
        exit 1
    fi

    # Parse JSON to extract instance details
    INSTANCE_NAME=$(echo "$RESULT" | jq -r '.[0].name' | sed 's|.*/||')
    PROJECT=$(echo "$RESULT" | jq -r '.[0].parentFullResourceName' | sed 's|.*/||')
    ZONE=$(echo "$RESULT" | jq -r '.[0].location' | sed 's|.*/||')
    STATE=$(echo "$RESULT" | jq -r '.[0].state')

    if [ "$INSTANCE_NAME" = "null" ] || [ "$PROJECT" = "null" ] || [ "$ZONE" = "null" ]; then
        echo "Failed to parse instance details from search result."
        echo "Raw result: $RESULT"
        exit 1
    fi

    # Check if instance is running
    if [ "$STATE" != "RUNNING" ]; then
        echo "Instance $INSTANCE_NAME is not running (state: $STATE)."
        echo "Cannot connect to a non-running instance."
        exit 1
    fi

    echo "Found instance: $INSTANCE_NAME in zone: $ZONE (project: $PROJECT)"

    # Generate and display the SSH command
    SSH_COMMAND="gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --project=$PROJECT ${GCLOUD_SSH_FLAGS}"
    echo "SSH command: $SSH_COMMAND"

    # Execute the SSH command
    echo "Connecting to $INSTANCE_NAME..."
    exec $SSH_COMMAND
}

# Main script logic
case "${1:-}" in
    "")
        echo "Usage: $0 &lt;IP_ADDRESS&gt;"
        echo "  &lt;IP_ADDRESS&gt;  - Connect to instance with this IP"
        exit 1
        ;;
    *)
        search_and_connect "$1"
        ;;
esac
</code></pre>]]></content:encoded>
    <comments>https://srijan.ch/notes/2025-07-21-001#comments</comments>
    <slash:comments></slash:comments>
  </item></channel>
</rss>
